关于runat = “server”

form runat=server标记

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.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">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
  <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>   
  <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即可获得输入的关键字。

Form中runat="server"属性的意义

运行编译aspx文件时,无runat server属性的标签直接写入Response;有runat server属性的标签,将被转换为HtmlContrl控件加入到ASP.NET自带的控件集合中。

用法区别:一个最直接的表现就是,当你写一个id="abc"div,如果有runat server属性,在你的aspx.cs文件中直接可以使用abc.XXX来操作这个对象,而没有runat server的话,在cs中是没有这个对象的。以上说的是asp.net中的html控件,asp控件只能runat server,因为他们不是直接和html对应的。

asp.net 的所有控件都必须放置在内吗?

ASP.NET 2.0服务器控件(HTML服务器控件和Web服务器控件)是否必须需要放在<form runat=server></form>的标记之中,可以根据需要进行设置,大多数情况下,对于只用来进行界面显示的控件、并且不需要处理事件的控件,可以不放在<form runat=server></form>之间,对于大多数控件来说,是要在服务器端进行事件处理和获得某些返回值的,因此需要放在<form runat=server></form>之间。

asp.net 必须放在具有

我称之为表单控件:

    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" Text="Button" />
        <asp:CheckBox ID="CheckBox1" runat="server" />
        <asp:DropDownList ID="DropDownList1" runat="server">
        </asp:DropDownList>
        <asp:FileUpload ID="FileUpload1" runat="server" />
        <asp:HiddenField ID="HiddenField1" runat="server" />
        <asp:ImageButton ID="ImageButton1" runat="server" />
        <asp:LinkButton ID="LinkButton1" runat="server">LinkButton</asp:LinkButton>
        <asp:ListBox ID="ListBox1" runat="server"></asp:ListBox>
        <asp:RadioButton ID="RadioButton1" runat="server" />
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    </div>
    </form>
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值