how to use server side variable in javascript in asp.net
  • How can i call server side variable in client side javascript because i want to show user name on alert.
  • 2 Comments sorted by
  • Vote Up0Vote Down WaqasWaqas
    172.00 Karma Accepted Answer
    Hi make public variable on server side and call it in javascript use below code its for vs 2008.

    Default.aspx

    <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <head runat="server">
        <title>Untitled Page</title>
        <script language="javascript" type="text/javascript">
        function ShowMyName()
    {
      //assign value to JavaVar Variable from MyName Server side Variable
      var JavaVar = '<%= MyName %>';
      return confirm('Hello '+ JavaVar );
    }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
         <asp:Button id="btnDelete" runat="server" Text="OK"  onclientclick='ShowMyName()'/>
        </div>
        </form>
    </body>
    </html>

    Default.aspx.cs

    using System;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;

    public partial class _Default : System.Web.UI.Page
    {
        public static string MyName = "Waqas";
        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }


  • thanks its working.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, or you want to Ask a new Question, Please Login or Create a new Account by Clicking below

Login with Facebook

Tagged

Popular Posts of the Week

Optimum Creative