简体   繁体   中英

A page can have only one server-side Form tag

I have read the other results, but nothing specific to my issue.

I realize having two form tags with runat="server" does not work. My problem is that I require a form in the Master Page file to handle the menus and such, and the 'child' page of my application also requires a form tag to handle its operations.

I have tried to remove the form tag in my 'child' pages, but the code-behind doesn't see that I am using the form tag in the Master Page. Due to this, compilation fails ("The name 'form1' does not exist in the current context")

How can I attain the goal of keeping my menus in the Master Page working, while keeping my 'child' pages from erroring out when compiling?

If you go the route of removing all the form tags from all the child pages (which I would suggest), you can add a reference to the Master Page's Form tag as a property that can be accessed by the child pages. Here would be the Master Page code behind:

public HtmlForm form1 {
    get { return this.form1; }
}

Then you could reference from the child page:

public void MyMethod() {
    HtmlForm theForm = ((MyMasterPageType)this.Master).form1;
    theForm.Controls.Add(...);
}

And if you set the MasterPageType in the child pages, you can reference directory with this.Master.form1 (add this to the markup page of the child page):

<%@ MasterType TypeName="MyMasterPageType" %>

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