简体   繁体   中英

Runtime error when attempting to change the Master Page for the Login.aspx page given by default with the Asp.Net WebForms template

I started an Asp.Net WebForms project in Visual Studios 2015 using the WebForms template. However, I would now like to change the MasterPage from the default (~/Site.Master) to a master page I created from a downloaded Bootstrap template.

The downloaded template works just find on the Webforms pages I created but I get this error when I attempt to use it with the Login page:

Additional information: Control 'Email' of type 'TextBox' must be placed inside a form tag with runat=server.

I have tried to create a new page without using the Master Page and I continue to get the error. The first thing I checked was the textbook used for the email but it does have runat="server". Here is a snippet of the code:

<div class="form-group" runat="server">
     <asp:Label runat="server" AssociatedControlID="Email" CssClass="col-md-2 control-label">Email</asp:Label>
     <div class="col-md-10">
          <asp:TextBox runat="server" ID="Email" CssClass="form-control" TextMode="Email" />
          <asp:RequiredFieldValidator runat="server" ControlToValidate="Email" CssClass="text-danger" ErrorMessage="The email field is required." />
     </div>
 </div>

Any help would be greatly appreciated. I've checked a few questions already but most deal with dynamically changing the MasterPage in the code behind (by the way, that's not the solution I'm looking for). Thanks.

As the error says, you should place your control inside a ContentPlaceHolder element. See MSDN documentation for same https://msdn.microsoft.com/en-us/library/wtxbf3hh.aspx

In your master page your have a contentplaceholder like

    <asp:contentplaceholder id="cp1" runat="server">
    </asp:contentplaceholder>

In your Login page include master page including MasterPageFile property

<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" ...

<asp:Content ID="Content1" ContentPlaceHolderID="cp1" Runat="Server">
<asp:TextBox runat="server" ID="Email" CssClass="form-control" TextMode="Email" />
</asp:Content>

I have found the answer from this link which says:

*Master Pages and Themes

You cannot directly apply an ASP.NET theme to a master page. If you add a theme attribute to the @ Master directive, the page will raise an error when it runs. However, themes are applied to master pages under these circumstances: If a theme is defined in the content page. Master pages are resolved in the context of content pages, so the content page's theme is applied to the master page as well. If the site as a whole is configured to use a theme by including a theme definition in the pages Element (ASP.NET Settings Schema) element. For more information, see ASP.NET Themes and Skins.*

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM