.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> ASP.NET Controls >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Use Server side variable on client side

Posted By:shankey       Posted Date: July 21, 2011    Points: 40    Category: ASP.NET Controls    URL: http://www.dotnetspark.com  

This article will explain how to use server side variable on client side and get the value of server side to the client side
 


In this article you will learn how to use server side variable on client side.

I came across a situation where i need to keep a common variable declared in a class file. This variable will hold list of comma separated file type.

This situation arose to keep maintanence as low as possible so that if we need to add/remove a file type there should be less overhead in change.

To use server side variable in client side or in javascript then you can write as shown below.


 function ValidateFileUpload() {
 alert("Only " + '<%= ViewStateAllowedFileType %>' + " files are allowed to upload");
}
In above code
ViewStateAllowedFileType
is a server side variable declared as shown below.


 public string ViewStateAllowedFileType
    {
        get
        {
            if (ViewState["AllowedFileType"] != null && ViewState["AllowedFileType"] != string.Empty)
            {
                return ViewState["AllowedFileType"].ToString();
            }
 
            return string.Empty;
        }
 
        set
        {
            ViewState["AllowedFileType"] = value;
        }
    }




     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend