2011-11-09 30 views
1

我在微軟網站上發佈了一些錯誤,雖然它們是真正的錯誤,但MSFT會將其作爲設計關閉它[我想大多數人都支持MSFT]。這是我確信他們會按設計分類的,但對我來說這是一個嚴重的錯誤。這應該是一個錯誤 - ASP.NET請求參數

這是我在ASPX頁面(NET 3.5)中所有的。

<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>   
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Submit" /><br /> 

<asp:Label ID="lblOutput" runat="server" Text="Label"></asp:Label> 

代碼隱藏

protected void Page_Load(object sender, EventArgs e) 
    { /* this works */ 
     if (IsPostBack) 
     { 
      string txt = string.Empty; 

      txt = Request.Params["TextBox1"]; 

      lblOutput.Text = "You entered : " + txt; 
     } 
    } 

    protected void Button1_Click(object sender, EventArgs e) 
    { /* this does not */ 
      string txt = string.Empty; 

      txt = Request.Params["TextBox1"]; 

      lblOutput.Text = "You entered : " + txt; 

    } 

現在,如果你有另外一個簡單的HTML文本(不ASP)這樣

<input type="text" id="mytextbox" name="mytextbox" /> // still it below the existing one 
txt = Request.Params["mytextbox"]; // change to this line instead of TextBox1 

然後它在這兩個地方。

回答

0
protected void Button1_Click(object sender, EventArgs e) 
{ /* Now this works which is weird but it does */ 
    If(IsPostback) 
    { 
     string txt = string.Empty; 

     txt = Request.Params["TextBox1"]; 

     lblOutput.Text = "You entered : " + txt; 
    } 
} 

因此,我應該關閉這個問題。

0

這是你在這個例子中做的最基本的東西,所以我非常懷疑這是一個錯誤。從例子中,它看起來像你要對糧食:

標記

<asp:PlaceHolder ID="PlaceHolder1" runat="server"> 
    <asp:TextBox ID="TextBox1" runat="server" /> 
    <asp:TextBox ID="TextBox2" runat="server" /> 
    <asp:TextBox ID="TextBox3" runat="server" /> 
    <asp:TextBox ID="TextBox4" runat="server" /> 
    ... 
</asp:PlaceHolder> 

<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Submit" /><br /> 
<asp:Label ID="lblOutput" runat="server" Text="Label"></asp:Label> 

代碼隱藏

protected void Button1_Click(object sender, EventArgs e) 
{ 
    foreach (TextBox txtCtrl in PlaceHolder1.Controls.OfType<TextBox>()) 
    { 
     //append the textbox value to the label 
     lblOutput.Text += String.Format("{0}<br/>", txtCtrl.Text); 
    } 
} 

protected void Button1_Click(object sender, EventArgs e) 
{ 
    List<TextBox> txtList = PlaceHolder1.Controls.OfType<TextBox>().ToList(); 

    for (int ctrlIndex = 0; ctrlIndex < txtList.Count; ctrlIndex++) 
    { 
     TextBox txtCtrl = txtList.ElementAt(ctrlIndex); 
     if (txtCtrl != null) 
     { 
      lblOutput.Text += String.Format("{0}<br/>", txtCtrl.Text); 
     } 
    } 
} 
+0

這只是一個測試案例。我的實際程序有大約30個參數,並且我遍歷所有使用Request.Param ['label1],label2等等。雖然我在測試代碼中使用它,但在實際代碼中它仍然不起作用,但是常規文本框確實可行!對我來說,ASP只是讓事情變得非常複雜,而它卻非常簡單。 –

+0

再次我正在使用現有的代碼,所以想要使用它,但我上面的代碼幾乎解釋了這個錯誤是什麼。以前的程序員使用普通的文本框問題,因爲他新的ASP文本框造成了問題。 –

+1

您不應該需要使用請求對象。該框架已經完成了所有的艱苦工作,所以你不必這樣做。對前一個程序員沒有冒犯性,但聽起來他對ASP.NET並不熟悉。看起來像一個傳統的ASP混合方法對我來說。 –

0

如果你被迫使用Request對象代替正常的asp:*控件,就這樣使用它:

txt = Request["TextBox1"]; 

它將檢查所有HttpRequest集合。

The QueryString, Form, Cookies, or ServerVariables collection member specified in the key parameter. If the specified key is not found, then null is returned.

+0

感謝您的提示。這在這個例子中起到了作用。 ---但沒有在實際的代碼中工作。絕對是一個很好的提示。 –

相關問題