2010-07-24 196 views
0

我使用C#+ VS2008 +淨3.5 + ASP.Net + IIS 7.0 + ADO.Net + SQL Server 2008中我想開發它具有以下一個ASP.Net aspx頁面功能,Http請求問題

1它可以接受3網址參數參數1,參數2和參數3,以及請求是這樣的,

http://example.org/foo.aspx?parame1=abc&param2=def&param3=ghi

2當頁面responsed到客戶端瀏覽器,我想顯示一個文本輸入和結果html頁面附近的提交按鈕,文本輸入的值與param1相同,在本示例中,abc將顯示在文本框中,眉毛中呃地址吧,我想保留原來的長url http://example.org/foo.aspx?parame1=abc&param2=def&param3=ghi;

3當用戶改變了文本輸入的值,並點擊提交按鈕,我想再次發送該請求foo.aspx,並改變參數1的值到用戶在文字輸入輸入的值,並在同時,將參數2和參數3的值保持與上次請求的響應相同。例如,當用戶請求http://example.org/foo.aspx?parame1=abc&param2=def&param3=ghi,並顯示頁面時,當用戶將文本輸入從abc更改爲google時,新請求將爲http://example.org/foo.aspx?parame1=google&param2=def&param3=ghi

任何參考樣本?我的問題是我不知道如何在一個aspx頁面中實現如此多的功能。

+1

您是否嘗試過'<形式......方法= 「獲取」> ...'?當然命名你的輸入'param1'。 – 2010-07-24 07:08:28

+0

如何解決這個問題?你能告訴我一個完整簡單的樣品嗎? – George2 2010-07-24 07:21:05

回答

2

如果你想在瀏覽器地址欄中顯示的更新的URL可以讓按鈕點擊回發到服務器,然後你處理TextBox的TextChanged事件。

在TextChanged事件處理程序中建立與改變的查詢字符串參數的新的URL,並使用Response.Redirect將瀏覽器重定向到新的URL。

下面是一個簡單的例子髒。

給定一個文本框和一個按鈕,一個財產以後ASPX頁面這樣

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
</head> 
<body> 

    <script type="text/javascript"> 
    function requery() { 
     var query = location.search.substring(1); 
     var pairs = query.split("&"); 
     var param1Value = document.getElementById("txtParam1").value; 

     url = "/Default.aspx?param1=" + param1Value; 
     for (var i = 0; i < pairs.length; ++i) { 
     var pair = pairs[i]; 
     if ((pair.indexOf("param1") != 0) && (pair.length > 0)) { 
      url += "&" + pair; 
     } 
     } 
     location.href = url; 
    } 
    </script> 

    <form id="form1" runat="server"> 
    <div> 
    <asp:TextBox ID="txtParam1" runat="server" OnTextChanged="txtParam1_TextChanged"></asp:TextBox> 
    <asp:Button ID="Button1" runat="server" Text="Submit" /> 
    <input type="button" value="Client Action" onclick="requery()" /> 
    </div> 
    </form> 
</body> 
</html> 

後面的代碼來處理文本框的TextChanged事件可以做到以下幾點。

using System; 
using System.Text; 

namespace WebApplication1 
{ 
    public partial class _Default : System.Web.UI.Page 
    { 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!IsPostBack) 
     { 
     txtParam1.Text = Request.QueryString["param1"]; 
     } 
    } 

    protected void txtParam1_TextChanged(object sender, EventArgs e) 
    { 
     // Build the new URL with the changed value of TextBox1  
     StringBuilder url = new StringBuilder(); 
     url.AppendFormat("{0}?param1={1}", 
     Request.Path, txtParam1.Text); 

     // Append all the querystring values that where not param1 to the 
     // new URL 
     foreach (string key in Request.QueryString.AllKeys) 
     { 
     if (string.Compare(key, "param1", true) != 0) 
     { 
      url.AppendFormat("&{0}={1}", key, Request.QueryString[key]); 
     } 
     } 

     // Redirect the browser to request the new URL 
     Response.Redirect(url.ToString()); 
    } 
    } 
} 
+0

我發現的另一個問題是,當輸入字符串轉換成文本輸入,並再次提交,Page_Load中會被調用兩次,用的IsPostBack真實和其他時間的IsPostBack假一次,則是預期的行爲(客戶端發送兩個請求給服務器?) ?謝謝。 – George2 2010-07-24 08:06:33

+1

@ George2,你通過保留原來的兩個參數意味着什麼。我提供的示例應該在URL上保留param2和param3。至於這兩個Page_Load事件,這是預期的。第一個是響應於所述按鈕的回發事件,第二個是在響應於瀏覽器重定向到新的URL(這是一個GET這樣的IsPostBack ==假),這是使用新參數相同的目標頁面。 – 2010-07-24 08:27:54

+0

是的,你是對的。我錯了。 :-( 你能回答這個問題,請 - 「我發現的另一個問題是,當輸入字符串轉換成文本輸入,並再次提交,Page_Load中會被調用兩次,一次是用真實的IsPostBack和其他時間的IsPostBack假的,是預期的行爲(客戶端發送兩個請求到服務器?)?謝謝。「 – George2 2010-07-24 08:31:19