Thursday, November 17, 2005

Unregister Startup Script, the Workaround !!!

Just like many others, I was also hung up as how do I unregister a startup script. Well, the solution doesn't seems to exists (or I didn't find one). But a workaround for this behaves is just like unregistering previously defined script and then registering the new one. Here is how it goes..

First you need to define a form level variable of type System.Web.UI.Control to your web page like

Private ctrlToFocus As System.Web.UI.Control = Nothing

Then add a Page PreRender event to your web page like

Private Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.PreRender
Dim script As String = "<script language="javascript">" _
& "document.getElementById('" & ctrlID.ClientID & "').focus();" _
& "document.getElementById('" & ctrlID.ClientID & "').scrollIntoView();" _
& "</script>
If Not Page.IsStartupScriptRegistered("focusKey") Then
Page.RegisterStartupScript("focusKey", script)
End If

Now, where ever you have defined your startup script to focus to a control, replace that with the following

ctrlToFocus = <yourControlName>

Hope this helped to solve your problem !!!


Anonymous said...


I am having some sort of data to come in the new window based on the button clicked on parent page. Now when i clcik on first button teh data comes fine and i chose the correct data from teh child page and then on click of ok it gets filled in the textbox corresponding to that button. Now the problem starts when i click on the next button to get the data for the next button, the data comes fine but the child page which is getting displayed is the old one only and doesn't come with the new data.
how can i achieve this?

Hemant said...

Hi MindGrill,
I 'm not able to follow what are you trying to. Can you explain with an example so that i can try my best to assist you.


paqogomez said...

Perhaps this answer can offer some insight as well: