Unregistering a script block

R

RSH

I have a situation where I am Registering a Startup script with the
following code:

If Not IsPostBack Then

Dim sScript As StringBuilder = New StringBuilder

sScript.Append("<SCRIPT language=""javascript"">" & vbCrLf)

sScript.Append("document.all.Contact_txtLoginUserName.focus()")

sScript.Append("</SCRIPT>" & vbCrLf)

Page.RegisterStartupScript("Window.OnLoad", sScript.ToString)

End if



The problem is that I am using two panels...one which contains the textbox
that I am adding focus to, but one the user logs in I am setting the
visibility of the panel that contains the txtLoginUserName = false, so now
on postback the page errors out because the control is not there. Is there
a way to unregister the script if isPostBack = true?

I already have the registerStartUpScript in the if Not IsPostBack check.



Thanks,

Ron
 
T

Teemu Keiski

Hi,

a method to unregister directly doesn't exist but you can for example do so
that put code on Page's PreRender (or specific control's Prerender) and
output the script only if the control is Visible.

'This code in PreRender of Page or the control.

If myControl.Visible Then

Dim sScript As StringBuilder = New StringBuilder

sScript.Append("<SCRIPT language=""javascript"">" & vbCrLf)

sScript.Append("document.all.Contact_txtLoginUserName.focus()")

sScript.Append("</SCRIPT>" & vbCrLf)

Page.RegisterStartupScript("Window.OnLoad", sScript.ToString)

End If
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top