2017-05-12 36 views
0

我在網上搜索了很多,但沒有回答我的問題。從Angular/asp項目的母版頁獲取ASHX輸入值

我需要在我的主頁中獲取/設置我的api.ashx中輸入文本的值。

Main.Master.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 
} 
+0

嗯......什麼是你想?對ashx和一個頁面的調用是兩個不同的請求。你也許可以通過'''context.Response.Redirect(newUrl)在ProcessRequest中重定向你的調用。 '''並傳遞一個URL參數... –

+0

我需要在我的母版頁的輸入文本中存儲一個標記 –

回答

1

下面是使用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>