控件必须放在具有 runat=server 的窗体标记内 解决方法
类型“TextBox”的控件“txt_UserName”必须放在具有 runat=server 的窗体标记内。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.Web.HttpException: 类型“TextBox”的控件“txt_UserName”必须放在具有 runat=server 的窗体标记内。
源错误:
执行当前 Web 请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪信息确定有关异常原因和发生位置的信息。
堆栈跟踪:
[HttpException (0x80004005): 类型“TextBox”的控件“txt_UserName”必须放在具有 runat=server 的窗体标记内。]
System.Web.UI.Page.VerifyRenderingInServerForm(Control control) +8775651
System.Web.UI.WebControls.TextBox.AddAttributesToRender(HtmlTextWriter writer) +48
System.Web.UI.WebControls.WebControl.RenderBeginTag(HtmlTextWriter writer) +20
System.Web.UI.WebControls.TextBox.Render(HtmlTextWriter writer) +20
System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +134
System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +19
System.Web.UI.Page.Render(HtmlTextWriter writer) +29
System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1266
代码:
<script language="javascript">
function check()
{
if(Form1.txt_UserName.value=="")
{
alert("登陆用户名不能为空!");
Form1.txt_UserName.focus();
return false;
}
if(Form1.txt_Password.value=="")
{
alert("密码不能为空!");
Form1.txt_Password.focus();
return false;
}
else
__doPostBack('btn_OK','');
}
function OK()
{
if(confirm("添加成功!"))
{
return true;
}
else
{
return false;
}
}
function keydown()
{
if(event.keyCode==13)
{
check();
}
}
</script>
</HEAD>
<body leftMargin="0" topMargin="0" MS_POSITIONING="GridLayout">
<form id="Form1" onsubmit="javascript:return checkInput(this);" method="post" runat="server">
<table cellSpacing="0" cellPadding="0" width="100%" bgColor="#f1f1f1" border="0">
<tr>
<td height="46"><IMG height="45" src="../images/subtitle_info.gif" width="180"></td>
</tr>
</table>
<table cellSpacing="0" cellPadding="0" width="100%" bgColor="#f1f1f1" border="0">
<tr>
<td>
<table cellSpacing="0" cellPadding="0" background="../images/sheet_bk.gif" border="0">
<tr>
<td vAlign="bottom"><IMG height="18" src="../images/sheet_left.gif" width="24"></td>
<td class="SheetSelected" vAlign="bottom" width="60">后台管理</td>
<td valign="bottom"><img src="../images/sheet_right.gif" width="25" height="18"></td>
</tr>
</table>
</td>
<td vAlign="bottom" align="right">
<table cellSpacing="2" cellPadding="0" width="99%" border="0">
<tr>
<td align="right">您的位置:<A href="../default.htm" target="_top">后台管理</A> >>
管理员登录</td>
</tr>
</table>
</td>
</tr>
<tr>
<td bgColor="#cccccc" colSpan="2"><img height="1" alt="控件必须放在具有 runat=server 的窗体标记内 解决办法" src="" width="1" name=""></td>
</tr>
</table>
<table cellSpacing="23" cellPadding="0" width="100%" border="0">
<tr>
<td>
<table cellSpacing="0" cellPadding="0" width="100%" border="0">
<tr>
<td class="SubTitle" vAlign="bottom"><IMG height="20" src="../images/icon_info.gif" width="20" align="absMiddle">
管理员登录</td>
</tr>
<tr>
<td>
<hr width="100%" color="#000000" noShade SIZE="2">
</td>
</tr>
<tr align="center">
<td align="center">
<table cellSpacing="0" cellPadding="4" width="100%" border="0">
<tr>
<td class="FormUnderLine" width="15%">用户名:</td>
<td class="FormUnderLine" width="85%"><asp:textbox id="txt_UserName" runat="server" Width="150px"></asp:textbox></td>
</tr>
<tr>
<td class="FormUnderLine">密 码:</td>
<td class="FormUnderLine"><asp:textbox id="txt_Password" TextMode="Password" Runat="server" Width="150px"></asp:textbox></td>
</tr>
</table>
</td>
</tr>
</table>
<table cellSpacing="0" cellPadding="4" align="center" border="0">
<tr>
<td width="155"> </td>
</tr>
<tr>
<td align="left">
<div align="center"><input id="btn_OK" onclick="check();" type="button" value="确定"><asp:button id="btn_OK" Runat="server" Visible="False"></asp:button> <input type="reset" value="重置">
<asp:linkbutton id="LinkButton1" runat="server"></asp:linkbutton></div>
</td>
</tr>
</table>
<FONT face="宋体"> </FONT>
<table cellSpacing="0" cellPadding="0" width="100%" border="0">
<tr>
<td> </td>
</tr>
</table>
<table cellSpacing="0" cellPadding="4" width="100%" align="center" border="0">
<tr>
<td width="29" height="20"></td>
</tr>
</table>
</td>
</tr>
</table>
</form>
</body>
[解决办法]
<form id="Form1" onsubmit="javascript:return checkInput(this);" method="post" runat="server">
你在Js中处理表单,就不能在表单中加 runat="server",这个表明它是服务器端控件,需要在后台处理
[解决办法]