Thursday, August 7, 2008

ClientScriptManager

ClientScriptManager Methods

Example:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ClientScriptManager.aspx.cs" Inherits="Demo4_ClientScriptManager" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>ClientScriptManager</title>

</head>

<body>

<form id="form1" runat="server">

<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />

</form>

</body>

</html>


using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

public partial class Demo4_ClientScriptManager : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

}

protected void Button1_Click(object sender, EventArgs e)

{

ClientScriptManager cs = this.ClientScript;

cs.RegisterArrayDeclaration("NewArray", "1, 2, 3");


if (!cs.IsClientScriptIncludeRegistered(this.GetType(), "IncludeFile"))

{

cs.RegisterClientScriptInclude(this.GetType(), "IncludeFile", ResolveClientUrl("~/HelloWorld.js"));

}


cs.RegisterExpandoAttribute(this.Button1.ClientID, "value", "Submit");


cs.RegisterHiddenField("MyHiddenField", "HiddenValue");


ClientScriptManagerHelper.RegisterStartupScript("HelloWorld", "<script>alert('Calling RegisterStartupScript')</script>");


cs.RegisterClientScriptBlock(this.GetType(), "HelloWorld", "alert('Calling RegisterClientScriptBlock');", true);

}

}


You can also use a helper class:

using System;

using System.Data;

using System.Configuration;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;

Public Class ClientScriptManagerHelper

{

public static void RegisterStartupScript(string key, string script)

{

Page p = HttpContext.Current.Handler as Page;

if (p != null)

{

ClientScriptManager cs = p.ClientScript;

if (!cs.IsStartupScriptRegistered(p.GetType(), key))

{

cs.RegisterStartupScript(p.GetType(), key, script);

}

}

}

}


Result:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title>ClientScriptManager </title>

</head>

<body>

<form name="form1" method="post" action="ClientScriptManager.aspx" id="form1">

<div>

<input type="hidden" name="MyHiddenField" id="MyHiddenField" value="HiddenValue" />

<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKMTQ2OTkzNDMyMWRkvBqH01KLDoGJ1Xt5N2w3nQeGoRM=" />

</div>

<script src="../HelloWorld.js" type="text/javascript"></script>

<script type="text/javascript">

//<![CDATA[

alert('Calling RegisterClientScriptBlock');

//]]>

</script>

<input type="submit" name="Button1" value="Button" id="Button1" />

<script type="text/javascript">

//<![CDATA[

var NewArray = new Array(1, 2, 3);

//]]>

</script>

<script type="text/javascript">

//<![CDATA[

var Button1 = document.all ? document.all["Button1"] : document.getElementById("Button1");

Button1.value = "Submit";

//]]>

</script>

<div>

<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWAgLGgea2CAKM54rGBgJqlPv4pQBR4CG20xV8nc5Nkpaa" />

</div>

<script>alert('Calling RegisterStartupScript')</script>

</form>

</body>

</html>


Note: Even though RegisterStartupScript is calling before RegisterClientScriptBlock in the above code, but at run time the alert will show RegisterStartupScript last. Also you can see the html result, the RegisterStartupScript script was added to the page last.

Reference:
ClientScriptManager Methods
blog comments powered by Disqus