Last post Apr 08, 2010 10:42 AM by codeaddict7423
Dec 01, 2006 10:05 AM|deDogs|LINK
I have been playing with Checkbox control, and noticed that when the control is rendered, the name attribute for the html element is supplied automatically.
I have been searching for what method to override. I would like to remove or change the name attribute when the Checkbox is rendered, and add a name attribute of my own liking.
Currently, I had attempted overriding the Render method without success, and I have searched MSN for any other methods, which I could use to override, but did not find any possible methods, or it may that I am not understanding on what mehods are utilized
when the control is rendered.
Again, if possible, I would like to replace the supplied name attribute value with my personal name attribute value, utilizing the Checkbox control.
Dec 02, 2006 06:43 AM|joteke|LINK
what CB renders in name attribute, comes from UniqueID property. It's not necessarily good idea to override it, since ASP.NET uses that to locate control on postback to rouite postback data & events. However, if you are sure it is ok in your scenario, you
certainly can override UniqueID property or use something like Wilco Bauwer's IDOverride :
Dec 02, 2006 09:47 AM|deDogs|LINK
Thank you, I am not looking to change the CB's ID attribute. I would like to change its Name attribute.
When the CB renders, it creates a Name attribute, which it is set a value equal to its ID value.
The presented code below, I created eight checkboxs, and all will have their ID attributes values set equal to chk1, chk2,...,chk8.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Dim chk As orCheckBox
Dim i As Integer
'Here I dynamically create eight checkbox server controls,
'which will be attached to the form control.
For i = 1 To 8
chk = New orCheckBox
chk.ID = "chk" & i
chk.Name = "check"
chk.Enabled = True
chk.EnableViewState = True
If you view its rendered HTML, it would be:<input id="chk1" type="checkbox" name="chk1" /><input id="chk2" type="checkbox" name="chk2" /> etc.
Notice, the CB Name attribute. When CB was rendered, its Name attribute took the same name as its ID.
I was successful in adding a Name attribute by overriding the Render method (see code below), However, the rendered HTML now includes two name attributes, which I tried, without success, to remove the rendered Name attribute.
Protected Overrides Sub Render(ByVal writer As HtmlTextWriter)
Dec 02, 2006 11:43 AM|joteke|LINK
You misread my post. I said: UniqueID property which every ASP.NET control has,
which CheckBox renders as name attribute in markup in order to provide the control hierarchy it belongs to (for postbacking). CheckBox renders the name attribute like this
Friend Overridable Sub RenderInputTag(ByVal writer As HtmlTextWriter, ByVal clientID As String, ByVal onClick As String)
If (Not clientID Is Nothing) Then
If (Not Me.UniqueID Is Nothing) Then
So you can conclude that it's 15 000 times easier to change the UniqueID than trying to override half of the rendering mechanism of CB since big part of it is internal. Of course, you could try to grab the rendered markup
of the CB and try to replace it straight from the string. However here's one possible mechanism.
Public Class MyCheckBox
Private _myNewUniqueID As String = ""
Public Property Name() As String
Set(ByVal value As String)
_myNewUniqueID = value
Public Overrides ReadOnly Property UniqueID() As String
If _myNewUniqueID <> "" Then
'Usage on Page
<%@ Register TagPrefix="samples" Assembly="VBWebApplication" Namespace="VBWebApplication" %>
<samples:MyCheckBox ID="mycb1" runat="server" Name="NameOne" Text="CB One" />
<samples:MyCheckBox ID="MyCheckBox1" runat="server" Name="NameTwo" Text="CB Two" />
When the rendered HTML would look like
<input id="NameOne" type="checkbox" name="NameOne" /><label for="NameOne">CB One</label>
<input id="NameTwo" type="checkbox" name="NameTwo" /><label for="NameTwo">CB Two</label>
Rendered id is now same, because it is derived from ClientID property which basically takes UniqueID and replaces expected separator characters (by default ':' in v1, '$' in v2.0, configurable via IdSeparator property in v2.0) with underscores (_) for it
to be scriptable. With this sample, there are no separator chars in UniqueID so it comes out as-is.
Dec 02, 2006 12:18 PM|deDogs|LINK
Very nice reply! Apologize for the misread.
It would probably have taken days,nights, and maybe years finding this solution, as for the
ambiguity of the UniqueID property.
I thank you very much,
Apr 08, 2010 10:42 AM|codeaddict7423|LINK
I read your post with great interest. I have a situation similar to the one described in your explanation. I have a checkbox that i'm attempting to change its ID from something like "chk_Date_of_Conference" to "Date of Conference" without the underscores.
I'm using Visual Studio programming on VB.NET for a user control file (*.ascx).
Can you please guide me to how to do something like you described above?
Thank you in advance for your time.