Bug : 必须放在具有 runat=server 的窗体标记内

本文介绍了如何解决ASP.NET中类型为TextBox的控件必须放置于具有runat=server属性的窗体标记内的问题。首先提供了通过重写VerifyRenderingInServerForm方法的临时解决方案,并进一步给出了在母版页<body>标签内加入<form>标签的根本解决办法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

类型“TextBox”的控件“ctl00_contentplaceholder_textbox1”必须放在具有 runat=server 的窗体标记内。

用了母版页就这样了

解决方法:

在内容页中重写基类page的VerifyRenderingInServerForm方法,重写就行了
public override void VerifyRenderingInServerForm(Control control)
{
//这里面可以不写任何内容
}

这个方法有个非常不好的地方,就是会把javascript弄失效
===========================================================
过了几天我找到了根本解决问题的方法,原来在母版页里的<body>标签内要加<form id="form1" runat="server">
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="WebForm1.aspx.cs" Inherits="WebForm1" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>用户注册</title> <style type="text/css"> .error { color: red; } </style> </head> <body> <form id="form1" runat="server"> <div> <label for="txtName">姓名:</label> <asp:TextBox ID="txtName" runat="server"></asp:TextBox> <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtName" ErrorMessage="用户必须填写" ForeColor="Red" Display="Dynamic"></asp:RequiredFieldValidator> </div> <div> <label for="txtPassword">密码:</label> <asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox> <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="txtPassword" ValidationExpression="^.{8,}$" ErrorMessage="密码必须八位以上" ForeColor="Red" Display="Dynamic"></asp:RegularExpressionValidator> </div> <div> <label for="txtConfirmPassword">确认密码:</label> <asp:TextBox ID="txtConfirmPassword" runat="server" TextMode="Password"></asp:TextBox> <asp:CompareValidator ID="CompareValidator1" runat="server" ControlToValidate="txtConfirmPassword" ControlToCompare="txtPassword" ErrorMessage="两次密码必须一致" ForeColor="Red" Display="Dynamic"></asp:CompareValidator> </div> <div> <label for="txtPhone">手机号:</label> <asp:TextBox ID="txtPhone" runat="server"></asp:TextBox> <asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ControlToValidate="txtPhone" ValidationExpression="^1[35789][0-35-9]\d{8}$" ErrorMessage="手机号格式不正确" ForeColor="Red" Display="Dynamic"></asp:RegularExpressionValidator> </div> <div> <label>请修正以下错误:</label> <asp:ValidationSummary ID="ValidationSummary1" runat="server" ShowSummary="true" ShowMessageBox="false" HeaderText="" CssClass="error"></asp:ValidationSummary> </div> <div> <asp:Button ID="btnRegister" runat="server" Text="注册" OnClick="btnRegister_Click" /> </div> </form> </body> </html>
最新发布
05-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值