RegisterClientScriptBlock problem

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have used RegisterClientScriptBlock in my ASP.NET page. it works well. Here I have problem. When Postback occurs, I dont want the script to be emitted. But It emits the Script even if I dont call the RegisterClientScriptBlock

How to prevent this?
 
if(!Page.IsPostback)
{
string strClientScriptIdent = "xyz";
if(!Page.IsClientScriptBlockRegistered(strClientScriptIdent)
{
string strClientScript = "blah blah blah";
Page.RegisterClientScriptBlock(strClientScriptIdent,
strClientScript);
}
}

--

Regards,

HD
Rajan said:
I have used RegisterClientScriptBlock in my ASP.NET page. it works well.
Here I have problem. When Postback occurs, I dont want the script to be
emitted. But It emits the Script even if I dont call the
RegisterClientScriptBlock.
 
Thanks for you reply. But my problem is little different
Let me explain the scenario to you
when I click on the button "A", I want the focus to be set to "TEXT1". So I call RegisterClientScriptBlock method to set the focus using javascript". it works fine
When I click on Button "B" in the same page I dont want to do anything regarding focus, so I didnt call RegisterClientScriptBlock methed this time
But still the Page persists the javascript which was previously emitted and set the focus to "TEXT1" which I dont want
How to remove the script which was emitted at the first tim

Thanks.
 
you dont need to mess about with the registerclient script.
instead of OnClick="javascriptfunction();"
use object.attibutes.add("onClick", "javascriptfunction();");
that way you only add the attributes for objects you want to have client
events.
 
Since I have to use this in lot of places (not only in button clicks) I have come up with the generic function like the one in below which emits javascript

public void SetFocusControl(string ControlName

string script = "<script language=\"javascript\"> var control = document.getElementById(\""
ControlName + "\");" + " if( control != null ){control.focus(); if(control.type == 'text') control.select();}" +
"</script>"
Page.RegisterStartupScript("Focus", script)


So whenever I want to set the focus to particular control I would call this fuction with that control name
when I dont want I dont call this, still the Page emits the javascript which was emitted at the last time
So I want to remove that script. is there any way to remove the registered script using the key or in some other way

Thanks
 
nope. i dont think there's a method to remove the registered script.
as a work around you could overwrite it to a emtpy javascript function or
something like that... or try just over writing it with ""
not sure whether it will work but its worth a try.
 
Rajan said:
Since I have to use this in lot of places (not only in button clicks) I have come up with the generic function like the one in below which emits javascript.

public void SetFocusControl(string ControlName)
{
string script = "<script language=\"javascript\"> var control = document.getElementById(\"" +
ControlName + "\");" + " if( control != null ){control.focus(); if(control.type == 'text') control.select();}" +
"</script>";
Page.RegisterStartupScript("Focus", script);
}

So whenever I want to set the focus to particular control I would call this fuction with that control name.
when I dont want I dont call this, still the Page emits the javascript which was emitted at the last time.
So I want to remove that script. is there any way to remove the registered script using the key or in some other way?

Thanks


You must be calling this even when you think you're not. The script
blocks are maintained in an instance member of the Page class, so when a
new Page instance is created (which happens with each HTTP request) it
starts with an empty script block collection.

Set a breakpoint on the method, and you'll find the culprit.
 

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

Back
Top