Request.Form獲取HTML頁面內容
阿新 • • 發佈:2019-01-08
在.aspx頁面中使用html的<form>標籤 ,將整個頁面的內容送至action屬性所指向的url,注意method屬性為post。
注意Request.Form是接受post方法的物件。
例如:
在Name.aspx中的前臺程式碼:
<form action="Default.aspx" method="post">
<p>First name: <input type="text" name="fname" /></p> <p>Last name: <input type="text" name="lname" /></p> <input type="submit" value="Submit" /></form>
在Default.aspx的.cs檔案後臺程式碼Page_Load中可以使用下面這段程式碼來檢視從Name.aspx送來的內容的Form陣列內的內容,通過鍵值來檢視所儲存位置的下標記:
執行後可以檢視到Form陣列中的各個欄位名以及下表(也可以新增斷點進行除錯在監視視窗新增Request.Form檢視AllKeys)。int loop1; NameValueCollection coll; //Load Form variables into NameValueCollection variable. coll=Request.Form; // Get names of all forms into a string array. String[] arr1 = coll.AllKeys; for (loop1 = 0; loop1 < arr1.Length; loop1++) { Response.Write("Form: " + arr1[loop1] + "<br>"); }
想要獲取First name 和Last name。
可以使用Request.Form.GetValues(0).GetValue(0);,改變GetValues(0)的下標即可訪問不同的欄位值。
Response.Write(Request.Form.GetValues(0).GetValue(0)) ;
Response.Write(Request.Form.GetValues(1).GetValue(0));