Saturday, August 8, 2009

ClientScriptManager

The ClientScriptManager class can be used to invoke client callbacks in situations when it is desirable to run server code from the client without performing a postback.
ClientScriptManager class has a different useful methods which are :
1. RegisterClientScriptBlock() Method:
The RegisterClientScriptBlock() method allows you to place a JavaScript function at the top of the page and it gets executed on startup of the page when the page loads in the browser.
There are two overloads for this method:
a)RegisterClientScriptBlock(Type, String, String)
b) RegisterClientScriptBlock(Type, String, String, Boolean)
Eg. To display the message box before the controls get actually rendered:


protected void Page_Load(object sender, EventArgs e)
{
if (!Page.ClientScript.IsClientScriptBlockRegistered("HelloAlert"))
{
script.RegisterClientScriptBlock(this.GetType(), "Message", "alert('hi')",true);

}
}


Note: IsClientScriptBlockRegistered() will return true if a script block is already registered with the same key and so we can prevent the registration of duplicate script.
2. RegisterStartupScript() Method:
Using this method it is possible to access page controls in the script block.Only difference between RegisterStartupScript() Method and RegisterClientScriptBlock() method is that RegisterStartupScript() methods will execute the scripts after page controls but before form close tag whereas RegisterClientScriptBlock() method will fire before the page controls get rendered.
There are two overloads for this method:
a) RegisterStartupScript (Type, String, String)
b) RegisterStartupScript (Type, String, String, Boolean)
Eg. Suppose you want to display the messagebox with the textbox value:

txtMessage.Text = "You are learning javascript features.";
if (!Page.ClientScript.IsStartupScriptRegistered ("AlertMessage"))
{
script.RegisterStartupScript (this.GetType(), " AlertMessage ",
"alert(document.getElementById(txtMessage).value)",true);
}

3. RegisterOnSubmitStatement() Method: To get the confirmation from the user before submitting the form.
Eg. Suppose you want to ask the user using confirm dialog box before contuing to next page .

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.ClientScript.IsClientScriptBlockRegistered(this.GetType(), "ConfirmScript"))
{
script.RegisterOnSubmitStatement(this.GetType(), " ConfirmMessage ", "return
confirm('Are you sure you want to continue to next page.')");
}
}

It will show a confirm box having ok and cancel option.
4. RegisterClientScriptInclude() Method
This method allows you to register a JavaScript reference with the Page object .
There are two overloads for this method:
a) RegisterClientScriptInclude(String,String)
b) RegisterClientScriptInclude(Type,String,String)

1 comment:

Unknown said...


Given so much information in it. its very useful .Thanks for your valuable information. dot net training in chennai velachery | dot net training institute in velachery