简体   繁体   中英

Get data from JS variable and set in a ASP.NET variable

I want to pass data from one user control to another one, but i've tried several things to do it, and non of them worked, such as sessionStorage in JS, Session in ASPX, cookies in both of them. This data is dynamic so I don't now how to transfer it, to the other user control.

I even tried to put aspx code in the javascript function (then when I click in the button it could trigger the code, but it doesn't work as well). This button i refereed above is written in a literal control.

JavaScript Functions

this function is the LoadUsers UserControl

  function getID(ID) {
        sessionStorage.setItem("userID", ID);
    }

this function is in the Access UserControl

 function catchIP() {
               var ID = sessionStorage.getItem("userID");
               $('#<%= value.ClientID %>').val(ID);
           }

UserControls

Load Users:

 ...
 string _str = "<a href'#lastAccess' css='btn btn-success' onclick='javascript:getID(" + _id[_contForeach] + "); catchID();'>Access</a>";
 _loadUsers.Controls.Add(new LiteralControl(_str));

Access:

How can I get access to the ID in the JavaScript function and apply it without using Page_Load

To pass information between the server side code and the client side code (JavaScript) use ajax

Ajax

So using jquery, have something like this function:

$.get('getuserid.aspx', function(response) {
 //Do something with return response
});

then in the code behind getuserid.aspx.cs

private void Page_Load(object sender, System.EventArgs e)
{
    Response.Expires = -1;
    //required to keep the page from being cached on the client's browser

   //set userid

    Response.ContentType = "text/plain";
    Response.Write(userid);
    Response.End();
}

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