ASP.NET 2.0服务器控件与form runat=server标记

80酷酷网    80kuku.com

  asp.net|server|服务器|控件

本文是《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"
      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"
      Text="提交" />
  </form>
</body>
</html>

在SearchDoc.aspx页面,使用Request.Form即可获得输入的关键字。

 

分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: