0% found this document useful (0 votes)
33 views

Change Id Form When HTML - ASP

This document discusses how the ID of a <form> tag generated by ASP.NET can be changed. By default, the ID is set to 'aspnetForm' when using master pages, but alternatives are provided like setting the ClientIDMode attribute to 'static' or overriding the UniqueID property to return a custom value.

Uploaded by

Luis Fernando
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Change Id Form When HTML - ASP

This document discusses how the ID of a <form> tag generated by ASP.NET can be changed. By default, the ID is set to 'aspnetForm' when using master pages, but alternatives are provided like setting the ClientIDMode attribute to 'static' or overriding the UniqueID property to return a custom value.

Uploaded by

Luis Fernando
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

24/11/2016 html 

­ Changing the generated ASP.Net <form> id? ­ Stack Overflow
     
sign up log in tour help 

x Dismiss

Join the Stack Overflow Community

Stack  Overflow  is  a  community  of  6.4  million


programmers,  just  like  you,  helping  each  other. 
Join  them;  it  only  takes  a  minute: 

Sign up

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?

asp.net   html   forms   master­pages

edited Jul 15 '10 at 16:09 asked Jul 15 '10 at 15:53
Earlz
26k 57 207 407

3 Answers

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 

http://stackoverflow.com/questions/3257369/changing­the­generated­asp­net­form­id 1/2
24/11/2016 html ­ Changing the generated ASP.Net <form> id? ­ Stack Overflow
    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>

edited Jul 15 '10 at 16:12 answered Jul 15 '10 at 16:04
Earlz Carter Medlin
26k 57 207 407 5,967 1 31 49

5   In ASP.NET 4, this behavior has changed. Even if the  <form>  control is inside a naming container, it no


longer hard­codes it to  aspnetForm . It actually now has the correct client ID. I ran into a situation where
we had a master page nested inside another master page. The nested master page is where the  <form>  is
defined and had the exact same thing happening. – Sumo Jul 11 '12 at 12:20

Set the "clientidmode" attribute to "static" on the form tag to prevent the framework from overriding
it with "aspnetForm". This was driving me nuts for hours.

answered Jan 11 '13 at 22:07
parliament
6,787 14 80 142

    For those who are not familiar with the attribute (as me): The attribute should be set in the master page
parameters  <%@ Master ClientIDMode="Static"  ... . For more information weblog.west­
wind.com/posts/2009/Nov/07/… – IvanH Nov 14 '14 at 9:32

I am agree with @Sumo's comment under accepted answer and I had the same situation.

In ASP.NET 4.0, master page, if a is not given an id, the rendered html will be automatically
assigned one, such as .

Otherwise, the rendered html will have its original defined id.

answered Feb 18 '15 at 22:28
Yang
66 5

http://stackoverflow.com/questions/3257369/changing­the­generated­asp­net­form­id 2/2

You might also like