简体   繁体   English

从JS变量获取数据并在ASP.NET变量中进行设置

[英]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. 我想将数据从一个用户控件传递到另一个用户控件,但是我已经尝试了一些方法来做到这一点,但它们都不起作用,例如JS中的sessionStorage,ASPX中的Session,两者中的cookie。 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). 我什至尝试将aspx代码放入javascript函数中(然后,当我单击按钮时,它可能触发代码,但效果不佳)。 This button i refereed above is written in a literal control. 我上面提到的这个按钮是用文字控件编写的。

JavaScript Functions JavaScript函数

this function is the LoadUsers UserControl 此功能是LoadUsers UserControl

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

this function is in the Access UserControl 此功能在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 如何在不使用Page_Load的情况下访问JavaScript函数中的ID并将其应用

To pass information between the server side code and the client side code (JavaScript) use ajax 要在服务器端代码和客户端代码(JavaScript)之间传递信息,请使用ajax

Ajax 阿贾克斯

So using jquery, have something like this function: 因此,使用jquery时,应具有以下功能:

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

then in the code behind getuserid.aspx.cs 然后在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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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