2012-03-20 23 views
0

說我創建一個靜態.html頁面。接下來,我用一堆輸入框構建一個表單。然後我決定將所有表單數據發佈到另一個頁面,將其稱爲process-form-data.aspx。我的問題是,自從我將表單數據發佈到.aspx頁面後,如何在屏幕背後的代碼中使用C#來獲取所有數據?收集表格數據從任何頁面發佈

我嘗試以下第一:

NameValueCollection nvc = Request.Form; 
string valTextBox1; 

if (!string.IsNullOrEmpty(nvc["txtBox1"])) 
{ 
    valTextBox1 = nvc["txtBox1"]; 
    Response.Write(valTextBox1); 
} 

然後我想:

valTextBox1 = Request.Form["txtBox1"].ToString(); 
Response.Write(valTextBox1); 

但無論方法似乎工作。如果我使用.apsx頁面上的runat =「server」屬性提交表單,我只能得到這兩種方法的工作方式。

我想避免通過URL傳遞變量。

感謝您的任何幫助。

+0

'txtBox1'是您發佈的HTML輸入字段的名稱? – Brandon 2012-03-20 20:29:06

+3

如果您不想使用GET,請使用POST。 – Chriseyre2000 2012-03-20 20:29:13

+0

@Brandon是的,name屬性被設置爲name =「txtBox1」。 – 2012-03-20 20:32:49

回答

0

感謝您的所有建議!我認爲當表單被提交時,默認方法是POST數據;事實證明它是GET。 (http://www.w3.org/TR/html401/interact/forms.html

我剛剛添加了以下屬性到表單標籤:method =「post」。

<form action="process-form-data.aspx" method="post"> 
    <input type="text" name="txtBox1" /> 
</form> 

這兩種收集原始文章中提到的表單數據的方法都很好。