Click here to Skip to main content
15,611,967 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all!

i want to remove ctl00$ from button control NamingContainer when using masterpage.

What I have tried:

I tried to remove ctl00$ from server side button's name and id by overriding it's NamingContainer like:
namespace NoName
   public class MatchingNameButton : Button
        public override Control NamingContainer
                return null;

and then registered that in aspx page:
<%@ Register TagPrefix="MatchingNameHtmlTextWriter" Namespace="NoName" Assembly="app_code" %>

and then button comes like this:
<MatchingNameHtmlTextWriter:MatchingNameButton runat="server" Text="btnTest" ID="btnTest" OnClick="btnTest_Click" />

and output is this:
<input type="submit" name="btnTest" value="btnTest" onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions('btnTest', '', true, '', '', false, false))" id="btnTest">

this way is good for removing control path. but using this will prevent btnTest_Click event!
and it's so bad for me.
my question is: how should i keep button events in this way?
my .net version is 4.5
all helps will be appreciated.
Updated 6-Jan-17 8:06am

1 solution

You can set the ClientIDMode.

Control.ClientIDMode Property (System.Web.UI)[^]
Share this answer
Masoud__Sh 6-Jan-17 16:52pm    
ClientIDMode will keep ID as entered by designer. but i need to keep Button name like Button ID.
for example, asp:Button runat="server" ID="btnTest" at runtime comes to:
input type="submit" name="ctl00$ContentPlaceHolder$btnTest" id="btnTest"

but this is what i want: input type="button" name="btnTest" id="btnTest"

I did it by overriding NamingContainer as well like this article for .net 2.0:

this article will help to keep Button name Like it's ID in output:

input type='submit' name='btnTest' value='Button' onclick='javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions('btnTest', '', true, '', '', false, false))' id='btnTest'

it's what i want and will make a tight control.
but the problem is when user clicks on this overrided button, the Click event will not fire. and i dont know where is the problem?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900