Monday, April 5, 2010

Retrieve server side variables using JavaScript code in ASP .NET

Sometimes you have to use the value of server side variable in javascript.
You can not directly access the server side variable in javascript.
Here is a demo how you can use server variable value in javascript.
First take a hiddenfield in aspx side.
<asp:HiddenField ID="HiddenField1" runat="server" />

You can assign the value you want to use to that hiddenfield before you use the javascript.
public partial class LoginDemo : System.Web.UI.Page
{   
    private string str="hello";
    protected void Page_Load(object sender, EventArgs e)
    {
        HiddenField1.Value=str;
    }
}

Now our hiddenfield has the value we want to use, just get that value from hiddenfield and then use it
<script type="text/JavaScript">
Var tt=document.getElementByID(“HiddenField1”);
alert(tt.value);
script>

No comments:

Post a Comment