Click here to Skip to main content
11,412,451 members (66,726 online)
Click here to Skip to main content

Use & Call RegisterStartUpScript, RegisterClientScript and Client-Side Script

, 5 Mar 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
This article helps a learner to know more about the different ways of calling client side JavaScript from server side code-behind.

Introduction

One can find this article useful during the development phase of a web application. One requires functionalities such as calling JavaScript client side code for validation, confirming popup, prompting user for unwanted action, etc. at server side code behind.

Implementation

So we start with an example through code.

RegisterStartupScript

Using RegisterStartupScript we can write a JavaScript function in code behind and call it from code-behind or from HTML. Look at the code below for reference.

Calling & Writing JavaScript Function from Server Side Code Behind

private void Page_Load(object sender, System.EventArgs e)
{
    string jScriptValidator;
    jScriptValidator="<script> function ReqFieldValidator()" + 
                " { if (document.forms[0].txtField.value == '') \n"; 
    jScriptValidator+="{ alert('TextBox cannot be empty') \n ";
    jScriptValidator+="return false; \n";
    jScriptValidator+="} \n";
    jScriptValidator+=" return true \n";
    jScriptValidator+=" } </script>";
    Page.RegisterStartupScript("regJSval",jScriptValidator);
    btnSubmit.Attributes.Add("onclick","return ReqFieldValidator()");
}

Writing JavaScript Function in Server Side Code Behind and Calling from HTML

//Server side

private void Page_Load(object sender, System.EventArgs e)
{
    string jScript;
    jScript="<script>function JavScriptFn(){alert" + 
      " ('Client Function in javascript is call')}</script>";
}
//HTML side
< A onclick="JavScriptFn()" >
< asp:Label id="Label1" runat="server" Width="281px" 
   ForeColor="#8080FF">Click to call Javascript function.
</asp:Label> >/A >

Writing JavaScript in HTML and Calling it from Code-behind

    <Head >
        <script>
        
        function ReqField1Validator()
        { 
         if (document.forms[0].txtField1.value == '') 
            {
                alert('TextBox cannot be empty') 
                return false
            } 
                return true 
        } 
                
        </script>
    </Head >
private void Page_Load(object sender, System.EventArgs e)
{
    btnOK.Attributes.Add("onclick","return ReqField1Validator()");
}

RegisterClientScriptBlock

Suppose we want JavaScript code to be executed but not a function. In that case, we make use of RegisterClientScriptBlock.RegisterClientScriptBlock which helps to make server side code as well as client side code inline to each other.

private void btnClientBlock_Click(object sender, System.EventArgs e)
{
    string jScript;
    jScript="<script>alert ('Javascript block of code executed')</script>";
    Page.RegisterClientScriptBlock("keyClientBlock",jScript);
    lblSequencial.Text="Remaining part of the code executed";
}

ASP.NET 2.0 RegisterClientScriptBlock/RegisterStartupScript

One can call JavaScript client side function directly from code behind using RegisterStartupScript. If one wants JavaScript to be embedded into codebehind, then make use of RegisterClientScriptBlock:

    <script>
    function fnShowMessage()
    {
        alert(" Invoke Javascript function from Server Side Code Behind ");
    }
    </script>
protected void Button1_Click(object sender, EventArgs e)
    {
        ClientScript.RegisterStartupScript
		(GetType(),"Javascript", "javascript: fnShowMessage(); ",true);
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        ClientScript.RegisterClientScriptBlock(GetType(), "Javascript", 
		"<script>alert('Record Added Successfully')</script>");
    }

Hope you find the above article helpful. Any suggestions are most welcome.

History

  • 25th July, 2005: Initial post
  • 4th March, 2009: Article updated

License

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

Share

About the Author

santosh poojari
Technical Lead
India India
Whatsup-->Exploring--> MVC/HTML5/Javascript & Virtualization.......!
www.santoshpoojari.blogspot.com

Comments and Discussions

 
QuestionSolution Pin
Alireza_1362 at 28-Mar-13 17:19
memberAlireza_136228-Mar-13 17:19 
GeneralMy vote of 5 Pin
Iris Star at 22-Aug-12 22:31
memberIris Star22-Aug-12 22:31 
GeneralMy vote of 5 Pin
P.Salini at 22-Jun-12 2:17
memberP.Salini22-Jun-12 2:17 
Nice Article. Useful to me.
GeneralMy vote of 5 Pin
anuj1784 at 10-Oct-11 4:23
memberanuj178410-Oct-11 4:23 
QuestionRegistring Client Script in ASP.NET Pin
awadhendra tiwari at 27-Aug-11 3:35
memberawadhendra tiwari27-Aug-11 3:35 
QuestionUse of 'calling' is misleading [modified] Pin
Gebbetje at 24-Jun-10 1:42
memberGebbetje24-Jun-10 1:42 
GeneralMy vote of 1 Pin
Miguel J at 23-Mar-10 15:18
memberMiguel J23-Mar-10 15:18 
GeneralRe: My vote of 1 Pin
valery possoz at 4-Jan-12 5:52
membervalery possoz4-Jan-12 5:52 
Generalvery helpful !! Pin
Louisa Chen at 28-Jun-07 16:10
memberLouisa Chen28-Jun-07 16:10 
GeneralRe: very helpful !! Pin
fletchsod at 13-Oct-08 5:53
memberfletchsod13-Oct-08 5:53 
GeneralRegistering a ClientScriptBlock from a code module or dll Pin
tony_trotter at 3-Apr-06 12:23
membertony_trotter3-Apr-06 12:23 
Questioncan i do the inverse Pin
marcoscavaleiro at 28-Nov-05 12:11
membermarcoscavaleiro28-Nov-05 12:11 
AnswerRe: can i do the inverse Pin
azam's at 14-Feb-07 22:01
memberazam's14-Feb-07 22:01 
GeneralJavascript files Pin
dudamir at 25-Jul-05 8:04
memberdudamir25-Jul-05 8:04 
GeneralRe: Javascript files Pin
santosh poojari at 25-Jul-05 20:07
membersantosh poojari25-Jul-05 20:07 
GeneralRe: Javascript files Pin
aprenot at 27-Jul-05 10:57
memberaprenot27-Jul-05 10:57 
GeneralRe: Javascript files Pin
santosh poojari at 28-Jul-05 20:45
membersantosh poojari28-Jul-05 20:45 
GeneralRe: Javascript files Pin
freesmile at 11-Sep-08 5:17
memberfreesmile11-Sep-08 5:17 
GeneralRe: Javascript files Pin
oguzhan.kahyaoglu at 22-Mar-12 23:53
memberoguzhan.kahyaoglu22-Mar-12 23:53 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150427.1 | Last Updated 5 Mar 2009
Article Copyright 2005 by santosh poojari
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid