从 Angular/asp 项目中的母版页获取 ASHX 中输入的值



我在互联网上搜索了很多,但没有回答我的问题。

我需要在我的 api.ashx 中获取/设置母版页中输入文本的值。

主.硕士.cs

<input type="hidden" id="token" name="token" runat="server" />

api.ashx

public void ProcessRequest(HttpContext context)
{
      //here i need to get or set the "token" input in master page 
}
下面是

一个使用 jQuery 和 aspnet 控件的示例。它从 TextBox1 获取值并将其发送到处理程序。然后处理程序发回另一个值,并将其放入 TextBox1 中。您可能需要根据具体情况进行调整。

处理程序代码

public void ProcessRequest(HttpContext context)
{
    context.Response.ContentType = "text/plain";
    //check if the querystring with the token exists
    if (context.Request.QueryString["token"] != null)
    {
        //get the old token from the querystring (and do stuff with it)
        string oldToken = context.Request.QueryString["token"];
        //check if oldToken contains a value
        if (string.IsNullOrEmpty(oldToken))
        {
            return;
        }
        //generate a new token
        string newToken = Guid.NewGuid().ToString();
        //send it to the browser
        context.Response.Write(newToken);
    }
}

aspx

<asp:TextBox ID="TextBox1" runat="server" Width="250"></asp:TextBox>
<br /><br />
<asp:Button ID="Button1" runat="server" Text="Get Set Token" UseSubmitBehavior="false" OnClientClick="getSetData(); return;" />
<script type="text/javascript">
    function getSetData() {
        var control = "#<%= TextBox1.ClientID %>";
        var oldToken = $(control).val();
        $.get("/TokenHandler.ashx?token=" + oldToken, function (newToken) {
            $(control).val(newToken);
        });
    }
</script>

最新更新