木子屋 Dnawo's BLOG

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

👤 dnawo 📅 2010-03-25 👁 9604 👍 0 💬 0 🔄 来源:本站原创
今天测试一个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>
    [color=Red]<form id="form1" runat="server">[/color]
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    </div>
    [color=Red]</form>[/color]
</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)

暂无评论。

验证码
评论需审核通过后显示
← 上一篇 C#获取sql中print语句输出的内容 下一篇 → 宝软网(baoruan.com)机型设置/资源下载总结