1

我在 Webform.aspx 页面上尝试了以下代码:

 <asp:Button ID="btnShowAssignLecturer" runat="server" 
        onclick="btnShowAssignLecturer_Click" Text="Assign Lecturer To Room" />

它说: System.Web.HttpException:“按钮”类型的控件“ctl00_head_btnShowAssignLecturer”必须放在带有 runat=server 的表单标记内。

当我这样做时,我得到另一个错误。不太确定该怎么做。

母版页代码:

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="MasterPage.master.cs" Inherits="AllsWellHospital.Front_End.MasterPage" %>

<!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">
    <h1>All Wells Hospital</h1>
      <p>
      <asp:Label ID="DateDisplay" runat="server"></asp:Label>
      </p>
    <asp:ContentPlaceHolder ID="head" runat="server"></asp:ContentPlaceHolder>   
    <link id="Link1" href="/Styles/StyleSheet2.css" runat="server" rel="stylesheet" type="text/css"/>
    <style>
    body
    {
     background-color:#d0e4fe;   
    }
    </style>

</head>
<body>
    <form id="form1" runat="server">    
     <div id="topContent">
     <asp:Menu ID="NavigationMenu" runat="server" CssClass="menu" EnableViewState="false" IncludeStyleBlock="false" Orientation="Horizontal">
             <Items>
      <asp:MenuItem NavigateUrl="WebForm1.aspx" Text="Web form.aspx"  Value="Upload SP10"></asp:MenuItem>
             </Items>
            </asp:Menu>
    </div>




    </form>
</body>
</html>

Webform.aspx 代码:

<%@ Page Title="" Language="C#" MasterPageFile="~/Front_End/MasterPage.Master" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="AllsWellHospital.Front_End.WebForm1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">

 <asp:Label ID="Label1" runat="server" Font-Bold="True" Font-Size="Medium" 
        Text="Course Report"></asp:Label>

         <asp:Button ID="btnShowAssignLecturer" runat="server" 
        onclick="btnShowAssignLecturer_Click" Text="Assign Lecturer To Room" />

</asp:Content>
4

2 回答 2

1

您的按钮需要在表单标签内,目前不是。你真的想把按钮放在表单的头部吗?为什么不为您的主要内容添加另一个内容区域。尝试做这样的事情:

母版页:

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="MasterPage.master.cs" Inherits="AllsWellHospital.Front_End.MasterPage" %>
<!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">

<h1>All Wells Hospital</h1>
  <p>
  <asp:Label ID="DateDisplay" runat="server"></asp:Label>
  </p>
<asp:ContentPlaceHolder ID="head" runat="server"></asp:ContentPlaceHolder>   
<link id="Link1" href="/Styles/StyleSheet2.css" runat="server" rel="stylesheet" type="text/css"/>
<style>
body
{
 background-color:#d0e4fe;   
}
</style>

</head>
<body>
<form id="form1" runat="server">    
 <div id="topContent">
 <asp:Menu ID="NavigationMenu" runat="server" CssClass="menu" EnableViewState="false" IncludeStyleBlock="false" Orientation="Horizontal">
         <Items>
  <asp:MenuItem NavigateUrl="WebForm1.aspx" Text="Web form.aspx"  Value="Upload SP10"></asp:MenuItem>
         </Items>
        </asp:Menu>
</div>
    <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server"></asp:ContentPlaceHolder>



</form>

你的aspx:

<%@ Page Title="" Language="C#" MasterPageFile="~/Front_End/MasterPage.Master" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="AllsWellHospital.Front_End.WebForm1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<asp:Label ID="Label1" runat="server" Font-Bold="True" Font-Size="Medium" 
    Text="Course Report"></asp:Label>


</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
  <asp:Button ID="Button1" runat="server" 
    onclick="btnShowAssignLecturer_Click" Text="Assign Lecturer To Room" />
</asp:Content>
于 2013-06-18T04:06:35.903 回答
0

一个按钮必须放置在服务器管理的<form>标记内,以便帖子在 ASP 管道中实际执行任何操作。

但是,您的内容占位符位于母版页Content1的标记内,此处无效。您需要重新考虑您的母版页,以便此内容不会存在于元素内部,并包含围绕您的表单。<head><form><head><form runat='server'>

您可能打算使用 HTML 5<header>元素,它与普通元素截然不同<head>

于 2013-06-18T03:58:37.457 回答