Thursday, 20 March 2014

How to use registerclientscriptblock in asp.net

Attaching the client scripts to the events on UI forms and can be added dynamically from server side.
 In registerclientscriptblock, script is rendered before any other element of the form is instantiated.  Because of this, it might not be able to reference all the elements on the page.
Contaning the two overload method


RegisterClientScriptBlock(Type type, String key, String script)

 RegisterClientScriptBlock(Type type, String key, String script Boolean addScriptTags)


 Before adding a script with RegisterClientScriptBlock  we can actually check if it is already been added somewhere else in our code by using a method, IsClientScriptBlockRegistered
How to use it

ClientScriptManager csManager = Page.ClientScript;  
Type vartype = this.GetType();  
if (!csManager.IsClientScriptBlockRegistered(vartype ,"buttonClick")) 
{
StringBuilder msgText = new StringBuilder();
msgText.Append(""); // Forming string
csManager.RegisterClientScriptBlock(vartype, "buttonClick", msgText.ToString());
}

No comments:

Post a Comment