Question

I am developing a .NET ASP application with VB code behind. I have 2 textboxes and 1 button. The button when clicked, uses javascript to open a new window (let's call the new window x).

 button.OnClientClick = "ShowWindowX();return false;"

The ShowWindowX() function opens a new window with a URL which passes in both parameters from the textbox. When I insert new values into the textboxes and click away from the textboxes (but not on the button), the page does a postback because I have set it to whenever the textboxes lose focus, to do a postback which will post the new values back to the server.

The problem is that if I edit any of the textbox values such as changing 3 to 4 and immediately click the button, without clicking off the textbox first then the new values are not picked up, so i still get 3. I tried to force a postback when the button is click through the following:

 btnBBHistory.OnClientClick = "__doPostBack();ShowWindowX();return false;"

and although this does cause a postback, the postback does not complete until I close the window which opens due to the ShowWindowsX function.

Can anyone advise on a way to force the postback to fully complete before the ShowWindowX() function executes so that the new values can be brought through?

Thanks in advance

Was it helpful?

Solution

You have to change your approach completely, as far as it is not a ajax call, it would wipe out everything on the page, and the page would get refreshed.

So what can be done in order to achieve this? you can check it in on the backend and add the script if the page is being loaded in a postback call, like this:

if (IsPostBack)
    Page.ClientScript.RegisterStartupScript(this.GetType(), "ShowWindowX", 
    "ShowWindowX();", true);

Of course it can be done if you already registered the script which the ShowWindowX is defined in it.

But if you don't want it to show the window EVERY time a post back occurs, you can set a flag in a global storage like localStorage, then check it when you want to open the window:

btnBBHistory.OnClientClick = "__doPostBack();localStorage.callwin=true;return false;"

and in the server side page load do this:

if (IsPostBack)
   Page.ClientScript.RegisterStartupScript(this.GetType(), "ShowWindowX", 
   "if(Boolean(localStorage.callwin)){ShowWindowX();localStorage.callwin='';}",
   true);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top