Click here to Skip to main content
5,785,816 members and growing! (18,168 online)
Email Password   helpLost your password?
Web Development » ASP.NET » Samples     Intermediate License: The Code Project Open License (CPOL)

Use & Call RegisterStartUpScript, RegisterClientScript and Client-Side Script

By santosh poojari

This article helps a learner to know more about the different ways to call client side JavaScript from server side code -behind.
C#, Javascript.NET 1.1, NT4, Win2K, WinXP, Windows, .NET, ASP.NET, Visual Studio, VS.NET2003, Dev

Posted: 24 Jul 2005
Updated: 24 Jul 2005
Views: 101,004
Bookmarked: 25 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
11 votes for this Article.
Popularity: 3.59 Rating: 3.44 out of 5
1 vote, 9.1%
1
2 votes, 18.2%
2
1 vote, 9.1%
3
4 votes, 36.4%
4
3 votes, 27.3%
5

Introduction

One can find this article useful during development phase of a web application. One requires functionalities such as calling JavaScript client side code for validation, confirm 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 JavaScript function in code behind and call it from code-behind or from HTML. Look at the below code 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";
}

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

License

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

About the Author

santosh poojari


He is a Team Lead currently working with a software company in India.He has overall 5.5 years of experience in .net technology. He is a B.E in computers from Mumbai University. His area of Interest is Microsoft Technology : Asp.Net,C#,Web services,SSIS,SSRS,Windows Workflow Foundation 3.0,State Machine Compiler,Regular Expression, Enterprise Library3.0,Spring.net,Design Patterns and Architecture Design.
He is MCPD-EA Certified.

He loves playing Cricket,Driving,Music and Drawing Sketches.
Occupation: Team Leader
Location: India India

Other popular ASP.NET articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 10 of 10 (Total in Forum: 10) (Refresh)FirstPrevNext
Generalvery helpful !!memberLouisa Chen16:10 28 Jun '07  
GeneralRe: very helpful !!memberfletchsod5:53 13 Oct '08  
GeneralRegistering a ClientScriptBlock from a code module or dllmembertony_trotter12:23 3 Apr '06  
Questioncan i do the inversemembermarcoscavaleiro12:11 28 Nov '05  
AnswerRe: can i do the inversememberazam's22:01 14 Feb '07  
GeneralJavascript filesmemberdudamir8:04 25 Jul '05  
GeneralRe: Javascript filesmembersantosh poojari20:07 25 Jul '05  
GeneralRe: Javascript filesmemberaprenot10:57 27 Jul '05  
GeneralRe: Javascript filesmembersantosh poojari20:45 28 Jul '05  
GeneralRe: Javascript filesmemberfreesmile5:17 11 Sep '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 24 Jul 2005
Editor: Smitha Vijayan
Copyright 2005 by santosh poojari
Everything else Copyright © CodeProject, 1999-2009
Web18 | Advertise on the Code Project