0

What is use of ClientIDMode attribute in asp:CheckBox control? I found the values AutoID, Inherit, Predictable, and Static for that.

Mainly I am looking for how it is rendered in HTML? Can anyone explain about the attribute and the values?

Jesuraja
  • 3,774
  • 4
  • 24
  • 48

2 Answers2

0

It is how MS calculates ID of a .net control name itself. MSDN

Since ASP.net pages are put together on a server and sent to the client MS Will name the ids, based off of a variable amount of conditions, are they in a master page , are they in a user controller, a repeater, and so on.

Since you cannot have duplicate ids what do you do with a .net control that is put inside of a repeater? You need to have a naming algorithm for it. Some people foolishly use the rendered ID (ct100_*) instead of using getElementID to manipulate the element through javascript. I dont know why they exposed these algorithms instead of just saying everything is going to be 1 algorithm. Maybe someone has the answer to why they exposed the different algorithms.

gh9
  • 10,169
  • 10
  • 63
  • 96
0

Example of id generation:

Web form:

<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        lstMain.DataSource = new string[] { "a", "b" };
        lstMain.DataBind();
    }
</script>

<asp:Content ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <asp:ListView ID="lstMain" runat="server">
        <ItemTemplate>
            <asp:CheckBox ID="chkFour" runat="server" ClientIDMode="AutoID" />
            <asp:CheckBox ID="chkFive" runat="server" ClientIDMode="Predictable" />
            <asp:CheckBox ID="chkSix" runat="server" ClientIDMode="Static" />
        </ItemTemplate>
    </asp:ListView>
</asp:Content>

Result:

        <input id="ctl00_ContentPlaceHolder1_lstMain_ctrl0_chkFour" type="checkbox" name="ctl00$ContentPlaceHolder1$lstMain$ctrl0$chkFour" />
        <input id="ctl00_ContentPlaceHolder1_lstMain_ctrl0_chkFive_0" type="checkbox" name="ctl00$ContentPlaceHolder1$lstMain$ctrl0$chkFive" />
        <input id="chkSix" type="checkbox" name="ctl00$ContentPlaceHolder1$lstMain$ctrl0$chkSix" />

        <input id="ctl00_ContentPlaceHolder1_lstMain_ctrl1_chkFour" type="checkbox" name="ctl00$ContentPlaceHolder1$lstMain$ctrl1$chkFour" />
        <input id="ctl00_ContentPlaceHolder1_lstMain_ctrl1_chkFive_1" type="checkbox" name="ctl00$ContentPlaceHolder1$lstMain$ctrl1$chkFive" />
        <input id="chkSix" type="checkbox" name="ctl00$ContentPlaceHolder1$lstMain$ctrl1$chkSix" />

Pass id to JS:

<script type="text/javascript">
    var autoId = '<%=someControl.ClientID%>';
    var predictableId = '<%=someControl.ClientID%>';
    var staticId = 'someControl';
</script>
Der_Meister
  • 4,771
  • 2
  • 46
  • 53
  • Instead of ClientIDMode values, can we assign some other value the that attribute? `Eg: ClientIDMode="xyz"` – Jesuraja Aug 12 '14 at 12:39
  • Is it overwrite the existing `id` value? not rendered as separate attribute? – Jesuraja Aug 12 '14 at 12:42
  • No, you can use only existing values in .NET 4. Yes, it overwrites the id attribute. Usually I use Static for more simple JS code. You have to write <%=checkBox.ClientID %> every time to get the real id for other modes. – Der_Meister Aug 12 '14 at 12:47
  • Actually, I wanted to group some of checkboxes in grid... for that only I looking for some attribute to assign GroupID... Better I think I can use `tooltip` attribute – Jesuraja Aug 12 '14 at 12:50
  • You can create custom HTML 5 data- attributes. See this answer: http://stackoverflow.com/a/10706882/991267 – Der_Meister Aug 12 '14 at 12:55