简体   繁体   中英

How to get value of application variable in JavaScript from asp.net?

I want to access the value of an application variable in JavaScript. How can I do that?

Declare a public property in codebehind.

public string firstName = "Sanju";

Access this in JavaScript like this.

    <script>
        var myName;
        function GetMyName()
        {
            myName = <%=this.firstName%>
        }
    </script>

To Access value in Session State, use this.

            myName = '<%=Session["firstName"]%>'

To Access value in Application State, use this.

            myName = '<%=Application["firstName"]%>'

You can make the variable public to access in javascript.

In code behind.

public string YourVar = "hello";

In javascript

alert("<%= YourVar  %>");

Note the value of the server side variable is substituted in generated html / javascript once the response is sent. If you want to access it from javascript after page is loaded then you might need to use ajax call to fetch value from server.

The following gets the ClientID of the rendered control

//markup
<asp:TextBox ID="aTextBox" runat="server" />
<asp:TextBox ID="bTextBox" runat="server" />
<asp:TextBox ID="cTextBox" runat="server" />

//script
var Members =
{
    aTextBox: $("#<%=aTextBox.ClientID %>"),
    bTextBox: $("#<%=bTextBox.ClientID %>"),
    cTextBox: $("#<%=cTextBox.ClientID %>")
}

Try the Sessions... Make a session in your class (I assume you have a General class)and use its reference to access it on any page you want. But remember you have to assign a value before using it.

eg,

below is a session of UserID.

public static int UserId
{
    get
    {
        if (HttpContext.Current.Session["UserId"] != null)
            return Convert.ToInt32(HttpContext.Current.Session["UserId"]);
        else
            return 0;
    }
    set
    {
        HttpContext.Current.Session["UserId"] = value;
    }
}

First you have to store value in your session as soon as you application starts.

User user = new user(); // consider you have a User class

protected void btnLogin_OnClick(object sender, EventArgs e)
{
    _user.Username = this.txtUserName.Text;
    _user.Password = this.txtPassword.Text;

    if (_user.Validate())
    {
       General.UserID = _user.UserID;  // here you are storing the id of logged in user
       Response.Redirect("~/HomePage.aspx");
    }
    else
    {
       this.labelNotice.Text = "Invalid Username or Password";
    }
}

To access the value of this session on any page in javascript,

<%@ Import Namespace="MyProject.Models" %>

<script type="text/javascript">
    var myID;
    function GetMyID() {
        myID= '<%=General.UserId%>';
    }
</script>

I imported my General class which is placed in the Models folder in MyProject solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM