然而從功能上來講此代碼有一個大的缺陷:不能使用ASP.NET編程模型來檢索用戶端表單action頁面上的提交資料。當編寫search.aspx時,對於用戶端表單的action頁面,不能對頁面的控制項藉助檢視狀態和提交資料來讀取和更新它們的狀態。(The apparent statefulness of ASP.NET server controls is obtained by making pages post to themselves)為了知道提交給search.aspx的資料,您必須採取針對ASP模型的直接在回傳資料中檢索的傳統風格:
protected void Page_Load(object sender, EventArgs e)
{
// Use the Request to retrieve posted data
string textToSearch = Request.Form["Keyword"].ToString();
...
// Use standard ASP.NET to populate the page UI
KeywordBeingUsed.Text = textToSearch;
}
這個技巧在ASP.NET 2.0中基本沒用,因為您會發現有2個新控制項:MultiView 和 Wizard。MultiView控制項使用邏輯等同的互斥表單,可惜它使用panel而沒用正統的form。MultiView允許你定義多個互斥的HTML panel,並提供了API來切換這幾個panel的可見度,並確保同一時刻只有一個被啟用並可見。MultiView控制項沒有提供內建的使用者介面。Wizard控制項僅是MultiView加上一個類嚮導的預定義的UI塊,我在MSDN雜誌2004年11期上講解過它(參閱 Cutting Edge: The ASP.NET 2.0 Wizard Control )。
Cross-Page Posting(跨頁提交)
ASP.NET 2.0 提供了一個新的內建進位以覆蓋常規處理周期並允許頁面提交到另一個頁面。通常,postback發生在下面兩種方式之一:Submit按鈕激發或通過script激發。典型的按鈕提交自動指向form指定的提交地址,而如果提交是通過script時則更加靈活機動。在ASP.NET 2.0中,您可以配置某個控制項(尤其那些實現了新的IButtonControl介面的)使其可以提交給其他目標頁面,具體可以查閱cross-page posting。
本編程模型也使用於ASP.NET 2.0, 不過,在 ASP.NET 2.0中,變得更簡單了,不再需要使用Handler.你可以使用與cross-page postings相同的編程模型並藉助一個非空的PreviousPage屬性和強型別訪問輸入欄位的@PreviousPageType指令來處理.那麼頁面如何才能檢測它是被server transfer調用還是cross-page postback?兩種方式下PreviousPage都是非空的,但是PreviousPage對象的Page.IsCrossPagePostBack在cross-page posting方式是為true,而erver transfer則為false.
小結
從一個頁面傳值到另一個頁面有很多種方法可以達成--cross-page posting,server transfer,HTML forms, cookies, session-state, query strings, 或者其他方法等等.那麼最有效是哪個呢。在 ASP.NET 2.0中,cross-page posting 和 server transfer提供了一個常見的編程模型,但卻潛在地通過View State移動了大塊的資料。而這些資訊是否真正需要依賴於目標頁面的描述。在很多情況下,目標頁面僅僅需要接收啟動運作的一些參數。if this is so,HTML用戶端表單可能用移動資料更加有效,儘管HTML表單需要一個類ASP的編程模型。
ASP.NET 2.0為HtmlForm類增加了一些新的特性,然而核心行為並未改變,因此提交自身仍是ASP.NET編程的主要方法。您可以混合用戶端form和伺服器form,也可以擁有多個伺服器form不過同一時間內僅有一個可見。
--------------------------------------------------------------------------------
圖 1 Properties of the HtmlForm Class
Property ASP.NET 1.x ASP.NET 2.0 Description
Attributes Inherited from Control. Gets a name/value collection with all the attributes declared on the tag.
ClientID Inherited from Control. Gets the value of UniqueID.
Controls Inherited from Control. Gets a collection object that represents the child controls of the form.
DefaultButton String property. Gets or sets the button control to display as the default button on the form.
DefaultFocus String property. Gets or sets the button control to give input focus when the form is displayed.
Disabled Gets or sets a value indicating whether the form is disabled. Matches the disabled HTML attribute.
EncType Gets or sets the encoding type. Matches the enctype HTML attribute.
ID Inherited from Control. Gets or sets the programmatic identifier of the form.
InnerHtml Inherited from HtmlContainerControl. Gets or sets the markup content found between the opening and closing tags of the form.
InnerText Inherited from HtmlContainerControl. Gets or sets the text between the opening and closing tags of the form.
Method Gets or sets a value that indicates how a browser posts form data to the server. The default value is POST. Can be set to GET if needed.
Name Gets the value of UniqueID.
Style Gets a collection of all cascading style sheet (CSS) properties applied to the form.
SubmitDisabledControls Indicates whether to force controls disabled on the client to submit their values, allowing them to preserve their values after the page posts back to the server. False by default.
TagName Returns "form".
Target Gets or sets the name of the frame or window to render the HTML generated for the page.
UniqueID Inherited from Control. Gets the unique, fully qualified name of the form.
Visible Gets or sets a value that indicates whether the form is rendered. If false, the form is not rendered to HTML.
--------------------------------------------------------------------------------
圖 2 Methods of the HtmlForm Class
Method ASP.NET 1.x ASP.NET 2.0 Description
ApplyStyleSheetSkin Applies the skin in the defined page theme in the manner defined by StyleSheetTheme.
DataBind Calls the DataBind method on all child controls.
FindControl Retrieves and returns the control that matches the specified ID.
Focus Sets the input focus to a control.
HasControls Indicates whether the form contains any child controls.
RenderControl Outputs the HTML code for the form. If tracing is enabled, caches tracing information to be rendered later, at the end of the page.
--------------------------------------------------------------------------------
圖 3 Server and Client Forms in the Same Page
<html>
<body>
<table><tr><td>
<form id="form1" runat="server">
<h2>Ordinary contents for an ASP.NET page</h2>
</form>
</td>
<td>
<form method="post" action="search.aspx">
<table><tr>
<td>Keyword</td>
<td><input type="text" id="Keyword" name="Keyword" /></td>
</tr><tr>
<td><input type="submit" id="Go" value="Search" /></td>
</tr></table>
</form>
</td>
</tr></table>
</body>
</html>
Property Description
CausesValidation Boolean value indicating whether validation is performed when the control is clicked.
CommandArgument Gets or sets an optional parameter passed to the button's Command event along with the associated CommandName.
CommandName Gets or sets the command name associated with the button that is passed to the Command event.
PostBackUrl Indicates the URL that will handle the postback triggered through the button control.
Text Gets or sets the caption of the button.
ValidationGroup Gets or sets the name of the validation group to which the button belongs.
Visible Boolean value indicating whether the button control is rendered.