Question

I have a public string in my code behind file that I intend to call from javascript when I'm ready for it. However, since it's not really tied to a particular event, it just fires every time the page loads. I would set it to only fire if(IsPostBack) but the control I'm using (for many other reasons) is an input type=button so the page never really does postback meaning the code will never fire.

How do I have something like:

public string dontExecuteYet()
 {
  Do some server side stuff
  Then redirect on delay
  return string
  }

in my Code Behind such that it doesn't run until I tell it to?

OTHER TIPS

You can do this with a Page method and AJAX like this.

http://www.geekzilla.co.uk/View7B75C93E-C8C9-4576-972B-2C3138DFC671.htm

That code executes only when you call it with javascript

....

....

Actually you can also force a postback on the input button with adding onclick="javascript:__doPostBack('eventargument','eventvalue');" (if you have that control over it)

If you need to access the code, eventually (assuming after the initial Page_Load), can't you just do something like this:

if (!Page.IsPostBack)
{
    this.MyPublicString = "Hello World!";
}

And in your JavaScript:

var publicString = "<%= this.MyPublicString %>";

I agree you can use AJAX in this, but if you don't need this to be async, just use a query string, like so:

if(Request.QueryString["execute"] == "yes")
{
     dontExecuteYet();
}

and in the javascript, you just need for this code to fire when the button is pressed:

window.location = "yoursite.com?execute=yes";
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top