Registering a script multiple times
In the code behind, I am registering the script to run like below:
string strFunctionName = "ShouldAdd";
sb.Append(strFunctionName + @"((blnShouldAdd ? "true" : "false") + ", true);");
ScriptManager.RegisterStartupScript(this, this.GetType(), "shouldAdd", sb.ToString(), true);
This piece of code is called twice, once on Page Load when blnShouldAdd
evaluating as false
and in the button's event handler when blnShouldAdd
evaluating as true
.
Oddly enough, when I debug the code and enter the ShouldAdd JS function, the value is always false. I would assume this is true, since the second call in the event handler overrides the first.
Any ideas?
a source to share
The page load is also done when you click the button, as part of the asp.net page lifecycle, and is triggered before the button's event handler. So, when you get to the button event handler, you've already registered the script with Page Load, so the request to register another script with the same (type, key) pair is ignored.
Perhaps you should move the call from the Load page to a later point in the pag lifecycle, perhaps PreRender in a better place.
a source to share