Click here to Skip to main content
Licence CPOL
First Posted 5 Jul 2007
Views 72,158
Bookmarked 19 times

Register Client script in ASP.NET

By | 5 Jul 2007 | Article
using client Script methods

Introduction

Registering Client script from server itself useful when , you want certain java script to be executed when page finished loading or when user does a submit on the form .

Background

To get the complete idea about the clientside java script registration from server , u go through the URL http://msdn2.microsoft.com/en-us/library/ms178207.aspx

Using the code

You have a clientscriptmanager object on each aspx page you have . the object name is ClientScript , using ClientScript you can register the script in 4 modes

1. RegisterClientScriptBlock:-Adds Script to the top of the page

2.RegisterClientScriptInclude:- Refer to a external .js file

3.RegisterStartupScript:- script is registered before onload event of a page

4. RegisterOnSubmitStatement:- Script is registered on onSubmit event of page

Have a look at the following example below which uses startupscript in my project, I used this one because I needed script to be executed and load a activex object before page is loaded.

if (!ClientScript.IsClientScriptBlockRegistered("exampleScript"))
    ClientScript.RegisterStartupScript(this.GetType(), "exampleScript","
<script language = "'javascript'">
alert('you just registered the start up script')
</script>
");

 //you can also do the  Add script on the events of the controls , like button click
 //I wrote a generic method for adding confirm box to the web controls  as shown below

public static void ConfirmMessageBox(ref System.Web.UI.WebControls.WebControl webControl, string message, string webControlName, string eventName)
{

StringBuilder sb = new StringBuilder();
sb.AppendFormat("var str = trim(document.forms[0].{0}.value);", webControlName);
sb.Append("if(str != null)");
sb.Append("{");
sb.Append("if(str != '')");
sb.Append("{");
sb.AppendFormat("var bRet = confirm('{0}');", message);
sb.Append("if(!bRet)");
sb.AppendFormat("document.forms[0].{0}.value = '';", webControlName);
sb.Append("return bRet;");
sb.Append("}");
sb.Append("}");

webControl.Attributes.Add(eventName, sb.ToString());

}
//you can add this script to any textbox or button control  on page load as shown below
 
WebControl control = (WebControl)textBoxControl;
ConfirmMessageBox(ref control, "Are you sure , you want to create this domain",
textBoxControl.ClientID, "onblur");

//Hope this helps in getting better understanding of the client side script registration from server 



License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Mohan Pindyala

Software Developer (Senior)
Rovi Corporation
United States United States

Member

Working on web services , Web services security , SAML2.0,WSE, WCF and Asp.net Ajax Applications .

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionRegistring Client Script in ASP.NET Pinmemberawadhendra tiwari2:34 27 Aug '11  
GeneralMy vote of 5 Pinmembervarun carthick23:39 8 Jun '11  
GeneralMy vote of 5 Pinmembervarun carthick23:38 8 Jun '11  
GeneralMy vote of 5 PinmemberMohammad Abu-Ali1:27 16 Feb '11  
Generalthx Pinmemberw39511532314:35 17 Jan '10  
GeneralDont concatenate Javascript with a StringBuilder Pinmembermeaningoflights19:37 13 Oct '08  
GeneralRe: Dont concatenate Javascript with a StringBuilder PinmemberMember 333950712:53 24 Feb '10  
GeneralThanks PinmvpMark Nischalke9:40 5 Jul '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 5 Jul 2007
Article Copyright 2007 by Mohan Pindyala
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid