RegisterClientScriptBlock

bungpeng

Senior Contributor
Joined
Sep 10, 2002
Messages
906
Location
Malaysia
I found that I can't use "RegisterClientScriptBlock" in user control. So if my user control need javascript to run, then how do I send this javascript code from user control to the parent page (which use this user control)?
 
I used 'Page.IsClientScriptBlockRegistered()' and 'Page.RegisterClientScriptBlock()' within a UserControl and it appeared in the HTML.

Now all that remains for me is to figure out if I can call that script function from the code behind.
 
but note you can add your control more than one time to the page and if your js-Function is called jsDoSomething() it will be appear more times on the Page and then you get js errors!! Therefore i name my js-Functions like this:

string js = "jsMyJavascriptFunktion"+this.ID.ToString()+"()";
js += "{";
js += "}";

Page.RegisterClientScriptBlock(Guid.NewGuid().ToString(), js);

The Guid is for because of the "key" must be unique and a guid is in all time unique!
 
Last edited:
Back
Top