2013-06-18 4 views
1

Я попытался следующий код на страницу Webform.aspx:Не можете добавить кнопку на ASPX странице

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

Он говорит: System.Web.HttpException: Control «ctl00_head_btnShowAssignLecturer» типа «Button» должен быть помещенный внутри тега формы с сервером runat =.

Когда я это сделаю, я получаю еще одну ошибку. Не совсем уверен, что делать.

Мастер кодовой страницы:

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

ответ

1

Ваша кнопка должна быть в форме тег формы, и в настоящее время это не так. Вы действительно хотите разместить кнопку в разделе вашей формы? Почему бы просто не добавить еще одну область содержимого для вашего основного контента. Попробуйте сделать что-то вроде этого:

Master Page:

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

Спасибо, что работал! – user2495628

0

кнопка должна быть помещена внутри сервера удалось <form> тег для того, чтобы пост на самом деле сделать что-нибудь в ASP.

Однако ваш контент-держатель Content1 находится в теге <head> главной страницы, где <form> недействителен. Вам нужно переосмыслить свою главную страницу, чтобы этот контент не находился внутри элемента <head>, и укажите <form runat='server'>, окружающий вашу форму.

Возможно, вы использовали элемент HTML 5 <header>, который отличается от обычного элемента <head>.

Смежные вопросы