本文是《asp.net 2.0應用程序開發》一書內容的延伸,講述asp.net 2.0服務器控件與標記之間的關系。
《asp.net 2.0應用程序開發》一書中,第19頁、第1.5.6小節的內容是關于asp.net 2.0服務器控件語法的描述,由于書中只是簡單地進行了介紹,現將更多的內容補充說明如下:
1,asp.net 2.0服務器控件與<form runat=server></form>的關系
asp.net 2.0服務器控件(html服務器控件和web服務器控件)是否必須需要放在<form runat=server></form>的標記之中,可以根據需要進行設置,大多數情況下,對于只用來進行界面顯示的控件、并且不需要處理事件的控件,可以不放在<form runat=server></form>之間,對于大多數控件來說,是要在服務器端進行事件處理和獲得某些返回值的,因此需要放在<form runat=server></form>之間。
2,如何進行控制
服務器控件在進行render、addattributestorender等的時候,會執行下面這句:
page page1 = this.page;
   if (page1 != null)
   {
         page1.verifyrenderinginserverform(this);
   }
page.verifyrenderinginserverform 方法 就是驗證服務器控件是否需要在<form runat=server></form>的標記之中,如果不在這個標記之中,將會引發下面的異常。例如下面的代碼:
<%@ page language="c#" %>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<script runat="server">
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title>verifyrenderinginserverform</title>
</head>
<body>
  <asp:textbox id="textbox1" runat="server"></asp:textbox>
  <form id="form1" runat="server">
  </form>
</body>
</html>
在瀏覽這樣的頁面時,將會引發異常:
類型“textbox”的控件“textbox1”必須放在具有 runat=server 的窗體標記內。 
說明: 執行當前 web 請求期間,出現未處理的異常。請檢查堆棧跟蹤信息,以了解有關該錯誤以及代碼中導致錯誤的出處的詳細信息。 
異常詳細信息: system.web.httpexception: 類型“textbox”的控件“textbox1”必須放在具有 runat=server 的窗體標記內。
這是因為,textbox控件在進行render的時候調用了page1.verifyrenderinginserverform(this);,因此,如果不放在<form runat=server></form>的標記之間,這個驗證過程是通不過的。
但是,我們可以在代碼中重載這個方法,以便是textbox控件可以放在<form runat=server></form>的標記之外,例如下面的代碼:
<%@ page language="c#" %>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<script runat="server">
  public override void verifyrenderinginserverform(control control)
  {
  }
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title>verifyrenderinginserverform</title>
</head>
<body>
  <asp:textbox id="textbox1" runat="server"></asp:textbox>
  <form id="form1" runat="server">
  </form>
</body>
</html>
瀏覽這樣的頁面就不會產生異常。
3,調整展現方式后,頁面能否正常工作
msdn上解釋page.verifyrenderinginserverform 方法時說:
如果回發或使用客戶端腳本的服務器控件沒有包含在 htmlform 服務器控件 (<form runat="server">) 標記中,它們將無法正常工作。這些控件可以在呈現時調用該方法,以在它們沒有包含在 htmlform 控件中時提供明確的錯誤信息。
是的,雖然下面的代碼可以正常顯示,但一旦單擊“提交”按鈕,服務器端將得不到輸入的值,頁不能保存狀態了。
<%@ page language="c#" %>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<script runat="server">
  public override void verifyrenderinginserverform(control control)
  {
  }
  protected void button1_click(object sender, eventargs e)
  {
    response.write("<li>textbox1.text = " + textbox1.text);
    response.write("<li>request.params = " + request.params[textbox1.uniqueid]);
  }
  protected void page_load(object sender, eventargs e)
  {
    response.write("<li>textbox1.text = " + textbox1.text);
    response.write("<li>request.params = " + request.params[textbox1.uniqueid]);
    if (!ispostback)
    {
      textbox1.text = "《asp.net2.0應用開發技術》";
    }
  }
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title>verifyrenderinginserverform</title>
</head>
<body>
  <asp:textbox id="textbox1" runat="server" width="600px"></asp:textbox>
  <form id="form1" runat="server">
    <asp:button id="button1" runat="server" onclick="button1_click"
      text="提交" />
  </form>
</body>
</html>
因此,在一般情況下,不要將服務器控件移到<form runat=server></form>的標記之外
4,如何強制將服務器控件放入<form runat=server></form>的標記之間
有些服務器控件可以不放在<form runat=server></form>的標記之間,如label控件,但如果需要強制將它放<form runat=server></form>的標記之間,可以使用下面的方法:
protected void label1_prerender(object sender, eventargs e)
{
  this.verifyrenderinginserverform(label1);
}
5,百害而無一益?
有時候,頁面上需要放置多個form表單(雖然只放置一個<form runat=server></form>的表單也能實現),將表單控件放在<form runat=server></form>標記之外,將非常方便使用,這在以前的asp頁面中很常見,現在在aspx中也可義實現。下面的頁面,既利用了服務器控件的方便性,也逃脫出了類型“textbox”的控件“textbox1”必須放在具有 runat=server 的窗體標記內的限制。例如:
<%@ page language="c#" %>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<script runat="server">
   protected void button1_click(object sender, eventargs e)
  {
    response.write("<li>textbox1.text = " + textbox1.text);
    response.write("<li>request.params = " + request.params[textbox1.uniqueid]);
  }
  protected void page_load(object sender, eventargs e)
  {
    keywords.text = "《asp.net2.0應用開發技術》";
    response.write("<li>textbox1.text = " + textbox1.text);
    response.write("<li>request.params = " + request.params[textbox1.uniqueid]);
    if (!ispostback)
    {
      textbox1.text = "《asp.net2.0應用開發技術》";
    }
  }
  public override void verifyrenderinginserverform(control control)
  {
  }
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title>verifyrenderinginserverform</title>
</head>
<body>
  <form method="post" action="searchdoc.aspx">
    關鍵字:<asp:textbox id="keywords" runat="server"></asp:textbox>
    <asp:button id="button2" runat="server" text="搜索" />
  </form>
  <form id="form1" runat="server">
    <asp:textbox id="textbox1" runat="server" width="600px"></asp:textbox>
    <asp:button id="button1" runat="server" onclick="button1_click"
      text="提交" />
  </form>
</body>
</html>
在searchdoc.aspx頁面,使用request.form即可獲得輸入的關鍵字。
新聞熱點
疑難解答
圖片精選