总结aspx必须放在具有 runat=server 的窗体标记内的控件

今天测试一个aspx页报错,提示:类型“TextBox”的控件“TextBox1”必须放在具有 runat=server 的窗体标记内。 页面代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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 runat="server">
    <title>无标题页</title>
</head>
<body>
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    </div>
</body>
</html>

很明显,少了form的原因,原因是我想去掉页面中隐藏的input而去掉了form:
引用内容 引用内容
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUJNzgzNDMwNTMzZGSeyZbEJyAclc40+nGVQK0EIblvEA==" />

加上form再测试就正常了:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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 runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    </div>
    </form>
</body>
</html>

后来又发现并非所有服务控件都要放在form中,总结了下:

必须放在具有 runat=server 的窗体标记内的控件:
TextBox
Button
LinkButton
ImageButton
DropDownList
ListBox
CheckBox
RadioButton
HiddenField
Calendar
FileUpload
Wizard
GridView
DetailsView
FormView

可以不放在具有 runat=server 的窗体标记内的控件:
Label
HyperLink
CheckBoxList
RadioButtonList
Image
ImageMap
Table
BulletedList
Literal
AdRotator
Xml
MultiView
Panel
PlaceHolder
View
Substitution
Localize
DataList
ListView
Repeater
SqlDataSource
AccessDataSource

评论: 0 | 引用: 0 | 查看次数: 9091
发表评论
登录后再发表评论!