无法向aspx页面添加按钮

时间:2013-06-18 03:43:52

标签: c# asp.net button

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

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

它说:System.Web.HttpException:'Button'类型的控件'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>

2 个答案:

答案 0 :(得分:1)

您的按钮必须位于表单标记内,目前不是。您真的想将按钮放在表单的 head 部分吗?为什么不为您的主要内容添加其他内容区域。尝试做这样的事情:

母版页

<%@ 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>

答案 1 :(得分:0)

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

但是,您的内容占有者Content1位于母版页的<head>标记内,其中<form>无效。您需要重新考虑您的母版页,以使此内容不在<head>元素内,并在您的表单中包含<form runat='server'>

您可能打算使用HTML 5 <header>元素,该元素与普通<head>元素完全不同。