Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > .NET > C#.NET > ASP.NET 2.0服务器控件与form runat=server标记
【标  题】:ASP.NET 2.0服务器控件与form runat=server标记
【关键字】:ASP.NET,2.0,form,runat,server
【来  源】:http://blog.csdn.net/net_lover/archive/2006/09/25/1282106.aspx

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

Your Ad Here
《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即可获得输入的关键字。

 
ASP.NET 2.0中TextBox服务器控件的ReadOnly和Disabled属性:【上一篇】
ADO.NET 2.0 中的新增 DataSet 功能--MSDN(翻译成Csharp):【下一篇】
【相关文章】
  • ASP.NET 2.0中TextBox服务器控件的ReadOnly和Disabled属性
  • ASP.NET2.0 Coexist with ASP.NET1.1/ASP.NET1.0?
  • ASP.NET 大文件上传研究
  • 关于ASP.NET调用JavaScript的实现
  • ASP.NET 调用ie打印(隐藏页眉、页脚以及打印按钮等)
  • 优化ASP.NET应用程序性能研究与探讨
  • SQL SERVER与C#的数据类型对应表
  • 用ASP.Net实现文件的在线压缩和解压缩
  • 进入Web 2.0时代-2006 Sun科技日
  • KODA FormDesigner 1.6.0.2 发布
  • 【随机文章】
  • JavaScript学习(二)
  • Oracle Listener启动失效
  • 在這裡使用 rsync 這個套件 來作兩台電腦的資料備份
  • VB取得网卡MAC地址
  • 拷贝的SQL Server 7的恢复方法
  • Servlet 是什么?
  • 利用vbs脚本设置IE的打印页眉页脚信息
  • 路由技术介绍
  • 使用控件也不是一件简单的事情。
  • 2D网络游戏开发(网络篇)(七)1
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 bbb软讯网络 All Rigths Reserved.