vb.net 获取“一个页面只能有一个服务器端表单标签”错误,而不是使用母版页
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18138399/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting "A page can have only one server-side Form tag" error, not using a master page
提问by Karl
So I'm the new guy on the dev team, trying to update a "Dashboard" type page, which is the default landing page for our users. The default.aspx page consists of 16 Web User Controls (.ascx). I've been asked to add stock "widget" on the dashboard, so users can monitor our company's stock symbol. To allow for different timeframes (1 day, 5 day, 3 month, etc) I was planning on using a Menu (tabs) within a new .ascx. When I try to use a menu, I get the error "Control 'StockDisplay_StockMenu' of type 'Menu' must be placed inside a form tag with runat=server". If I wrap the menu in form tags, I get the "A page can have only one server-side Form tag" error.
所以我是开发团队的新成员,尝试更新“仪表板”类型的页面,这是我们用户的默认登录页面。default.aspx 页面由 16 个 Web 用户控件 (.ascx) 组成。我被要求在仪表板上添加股票“小部件”,以便用户可以监控我们公司的股票代码。为了允许不同的时间范围(1 天、5 天、3 个月等),我计划在新的 .ascx 中使用菜单(选项卡)。当我尝试使用菜单时,出现错误“ Control 'StockDisplay_StockMenu' of type 'Menu' must be put into a form tag with runat=server”。如果我将菜单包装在表单标签中,我会收到“一个页面只能有一个服务器端表单标签”错误。
Most search results suggest looking at the Master Page, but we're not using one here. I checked The default.aspx page, and it does not have any form tags. Other searches suggested removing < head> and < body> tags in the .ascx, which I've done, but still I have the issue.
大多数搜索结果都建议查看母版页,但我们在此处不使用母版页。我检查了 default.aspx 页面,它没有任何表单标签。其他搜索建议删除 .ascx 中的 < head> 和 < body> 标签,我已经这样做了,但仍然存在问题。
Here's my stripped down code, which still produces the error:
这是我的精简代码,它仍然会产生错误:
<%@ Control Language="VB" AutoEventWireup="false" CodeFile="UC_StockDisplay.ascx.vb" Inherits="UC_StockDisplay" %>
<%@ Import Namespace="System.Data" %>
<form id="Form1" runat="server">
<asp:Menu ID="StockMenu" runat="server">
<Items>
<asp:MenuItem Text="One Day" />
<asp:MenuItem Text="Five Days" />
<asp:MenuItem Text="Three Months" />
<asp:MenuItem Text="Six Months" />
<asp:MenuItem Text="One Year" />
</Items>
</asp:Menu>
</form>
Here's the default.aspx:
这是默认的.aspx:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<%@ Register src="UC_HelpUs.ascx" tagname="HelpUs" tagprefix="uc1" %>
<%@ Register src="UC_ImportantNumbers.ascx" tagname="ImportantNumbers" tagprefix="uc2" %>
<%@ Register src="UC_ImportantLinks.ascx" tagname="ImportantLinks" tagprefix="uc3" %>
<%@ Register src="UC_Printers.ascx" tagname="Printers" tagprefix="uc4" %>
<%@ Register src="UC_WorkOrder.ascx" tagname="WorkOrder" tagprefix="uc5" %>
<%@ Register src="UC_RequestForms.ascx" tagname="UCRequestForms" tagprefix="uc6" %>
<%@ Register src="UC_MiscLinks.ascx" tagname="MiscLinks" tagprefix="uc7" %>
<%@ Register src="UC_Emergency.ascx" tagname="Emergency" tagprefix="uc8" %>
<%@ Register src="UC_Floorplan.ascx" tagname="Floorplan" tagprefix="uc9" %>
<%@ Register src="UC_Directory.ascx" tagname="Directory" tagprefix="uc10" %>
<%@ Register src="UC_Weather.ascx" tagname="Weather" tagprefix="uc11" %>
<%@ Register src="UC_Holiday.ascx" tagname="Holiday" tagprefix="uc12" %>
<%@ Register src="UC_CoreValues.ascx" tagname="CoreValues" tagprefix="uc14" %>
<%@ Register src="UC_MyInfo.ascx" tagname="MyInfo" tagprefix="uc15" %>
<%@ Register src="UC_Travel.ascx" tagname="Travel" tagprefix="uc16" %>
<%@ Register src="UC_StockDisplay.ascx" tagname="StockDisplay" tagprefix="uc17" %>
<!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>IT Helpdesk</title>
</head>
<body style="background-color:#1B5B9D;">
<center>
<a href="/"><img src="images\banner2.gif" border=0></a><br>
<font face="Courier New Bold" size=5 color=white>Helpdesk 3.5</font><br>
<table width=100% cellspacing=10>
<tr>
<td width="33%" valign=top>
<uc1:HelpUs ID="HelpUs" runat="server" />
<br>
<uc3:ImportantLinks ID="ImportantLinks" runat="server" />
<br>
<uc10:Directory ID="Directory" runat="server" />
<br>
<uc6:UCRequestForms ID="UCRequestForms" runat="server"/>
<br>
<uc7:MiscLinks ID="MiscLinks" runat="server" />
</td>
<td width="33%" valign=top align=center>
<uc5:WorkOrder ID="WorkOrder" runat="server" />
<br>
<uc8:Emergency ID="Emergency" runat="server" />
<br>
<uc2:ImportantNumbers ID="ImportantNumbers" runat="server" />
<br>
<uc11:Weather ID="Weather" runat="server" />
<td width="33%" valign=top>
<uc17:StockDisplay ID="StockDisplay" runat="server" />
<br />
<uc9:Floorplan ID="Floorplan" runat="server" />
<br>
<uc4:Printers ID="Printers" runat="server" />
<br>
<uc15:MyInfo ID="MyInfo" runat="server" />
<br>
<uc14:CoreValues ID="CoreValues" runat="server" />
<br>
<uc12:Holiday ID="Holiday" runat="server" />
<br />
<uc16:Travel ID="Travel" runat="server"></uc16:Travel>
</td>
</tr>
</table>
</center>
</body>
</html>
回答by afzalulh
After adding one control you have a form with runat="server" tag:
添加一个控件后,您将拥有一个带有 runat="server" 标签的表单:
<form id="Form1" runat="server">
<asp:Menu ID="StockMenu" runat="server">
You already mentioned you have 16 usercontrol in one page. So you have 16 forms with runat="server" tag. Which is not allowed.
您已经提到您在一页中有 16 个用户控件。所以你有 16 个带有 runat="server" 标签的表单。这是不允许的。
Solution:As Chris Lively suggested, strip out the the form tag from your wsercontrols. Add just one form tag in the page. You should be ok.
解决方案:正如 Chris Lively 建议的那样,从您的 wsercontrols 中去掉 form 标签。在页面中只添加一个表单标签。你应该没问题。
Here's how should allyour controls should look like:
下面是应该所有的控制应该是这样的:
<asp:Menu ID="Menu1" runat="server">
<Items>
<asp:MenuItem Text="One Day" />
<asp:MenuItem Text="Five Days" />
<asp:MenuItem Text="Three Months" />
<asp:MenuItem Text="Six Months" />
<asp:MenuItem Text="One Year" />
</Items>
</asp:Menu>
And your page should look like:
您的页面应如下所示:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<%@ Register src="UC_HelpUs.ascx" tagname="HelpUs" tagprefix="uc1" %>
<%@ Register src="UC_ImportantNumbers.ascx" tagname="ImportantNumbers" tagprefix="uc2" %>
<%@ Register src="UC_ImportantLinks.ascx" tagname="ImportantLinks" tagprefix="uc3" %>
<%@ Register src="UC_Printers.ascx" tagname="Printers" tagprefix="uc4" %>
<%@ Register src="UC_WorkOrder.ascx" tagname="WorkOrder" tagprefix="uc5" %>
<%@ Register src="UC_RequestForms.ascx" tagname="UCRequestForms" tagprefix="uc6" %>
<%@ Register src="UC_MiscLinks.ascx" tagname="MiscLinks" tagprefix="uc7" %>
<%@ Register src="UC_Emergency.ascx" tagname="Emergency" tagprefix="uc8" %>
<%@ Register src="UC_Floorplan.ascx" tagname="Floorplan" tagprefix="uc9" %>
<%@ Register src="UC_Directory.ascx" tagname="Directory" tagprefix="uc10" %>
<%@ Register src="UC_Weather.ascx" tagname="Weather" tagprefix="uc11" %>
<%@ Register src="UC_Holiday.ascx" tagname="Holiday" tagprefix="uc12" %>
<%@ Register src="UC_CoreValues.ascx" tagname="CoreValues" tagprefix="uc14" %>
<%@ Register src="UC_MyInfo.ascx" tagname="MyInfo" tagprefix="uc15" %>
<%@ Register src="UC_Travel.ascx" tagname="Travel" tagprefix="uc16" %>
<%@ Register src="UC_StockDisplay.ascx" tagname="StockDisplay" tagprefix="uc17" %>
<!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 id="Head1" runat="server">
<title>IT Helpdesk</title>
</head>
<body style="background-color:#1B5B9D;">
<form id="form1" runat="server">
<%-- your usercontrols and other tags --%>
</form>
EDIT:As you mentioned other usercontrols don't have forms, just adding form tag in the main page and remove that tag from usercontrol should solve your problem.
编辑:正如您提到的,其他用户控件没有表单,只需在主页中添加表单标签并从用户控件中删除该标签即可解决您的问题。

