Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

A Client Script Helper Utility Class

0.00/5 (No votes)
19 Mar 2006 1  
The "ClientScriptHelper" utility class will help you by not requiring you to remember the client side codes.

Sample Image - clnt_Side_src.png

Introduction

Using the "Add" method of "Attributes" property of ASP.NET web controls you ca add client side functionalities (like showing message, print dialog, close browser, browser back, pop-up window etc) to your code. However, invoking the client side behaviors to the web controls, you have to remember the corresponding client script codes as well as each time you want to use that by adding the corresponding attribute. The "ClientScriptHelper" utility class will help you by not requiring you to remember the client side codes in this case.

 

Using the class

 

As the "Add" method of "Attributes" property of ASP.NET web controls requires to be invoked in the "Page Load" event of the ASP.NET, you need to call the helper static methods, in the corresponding event handler.

 

protected System.Web.UI.WebControls.LinkButton lbtPrint;
protected System.Web.UI.WebControls.LinkButton lbtClose;
protected System.Web.UI.WebControls.LinkButton lbtShowPopup;
protected System.Web.UI.WebControls.LinkButton lbtShowMessage;
 
private void Page_Load(object sender, System.EventArgs e)
{
    if ( !IsPostBack)
    {
    Ashraf.ClientScriptHelper.AddPrint(lbtPrint);
    Ashraf.ClientScriptHelper.AddCloseWindow(lbtClose);
    Ashraf.ClientScriptHelper.AddOpenPopupWindow(lbtShowPopup, "http://www.codeproject.com/">http://www.codeproject.com/");

    Ashraf.ClientScriptHelper.AddMessage(lbtShowMessage, "Hello World");
    }
}

 

 

In the provided sample page we have four ASP.NET LinkButton control, for each of them we have assigned different functionalities. Since, to add client side functionalities to the web controls only once is enough, we call the methods in the "!IsPostBack" code block.

 

The code below adds the 'Show Message box' functionality to a web control:

 

Ashraf.ClientScriptHelper.AddMessage(lbtShowMessage, "Hello World"); 

 

The code below adds the 'Pop-up' functionality to a web control:

 

Ashraf.ClientScriptHelper.AddOpenPopupWindow(lbtShowPopup, "http://www.codeproject.com/">http://www.codeproject.com/");

 

The code below adds the 'Print Dialog' functionality to a web control:

 

Ashraf.ClientScriptHelper.AddPrint(lbtPrint);

 

The code below adds the 'Browser Close' functionality to a web control:

 

Ashraf.ClientScriptHelper.AddCloseWindow(lbtClose);

 

Conclusion:

 

Any advice or correction for this class will be highly appreciated.

 

 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here