Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
802 views
in Technique[技术] by (71.8m points)

html - Changing the generated ASP.Net <form> id?

In my ASP.Net page I have

<form id="MasterPageForm" runat="server">

However, whenever the markup is generated, it turns into

<form name="aspnetForm" method="post" action="SomePage.aspx..." id="aspnetForm">

Is it possible to set what the generated HTML id for the form is?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Note: you are seeing "aspnetForm" because you are using a master page.

I found your solution in this thread...

http://forums.asp.net/p/883974/929349.aspx

In short, this is what the answer is from that link:

Here's the responsible code for that error:

public override string UniqueID
{
      get
      {
            if (this.NamingContainer == this.Page)
            {
                  return base.UniqueID;
            }
            return "aspnetForm";
      }
}

As you can see, when the naming container is different from the current page (something that happens when you use a master page) the UniqueID property return "aspnetForm". this property is rendered into the name attribute that is sent to the client in the form tag. so, if you really need to, you can create your own form by inheriting from htmlform and then override the UniqueID property or the Name property (this may be a better option).

An example custom HtmlForm class could be like this:

public class Form : System.Web.UI.HtmlControls.HtmlForm
{
    public Form() : base() { }

    public override string UniqueID
    {
        get {
            if (this.NamingContainer == this.Page)
            { return base.UniqueID; }

            return "f";
        }
    }
}

Note: You can certainly change the name of the form from "f" to something else, or have it read a dynamic value, say from a web.config file or so.

and used like so

<%@Register tagprefix="LA" Namespace="Mynamespace"%>
...
<LA:form runat="server" id="frm">
...
</LA:form>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...