how to use server side variable in javascript in
  • 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.


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

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

    <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 );
        <form id="form1" runat="server">
         <asp:Button id="btnDelete" runat="server" Text="OK"  onclientclick='ShowMyName()'/>


    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


Popular Posts of the Week

    Optimum Creative