none
Asp Checkbox check and uncheck issue with checkbox text. RRS feed

  • Question

  • Hi,

    I have a checkbox in my aspx page. I need to check and uncheck if i click on "box" only not on checkbox text.

    <asp:CheckBox ID="Chk1" runat="server" Text="vs12"/>

    For the above checkbox it will rendered to browser(HTML) as 

    <input name="Chk1" id="Chk1" type="checkbox" value="on"/> <label for="Chk1">vs12</lable>

    

    My requirement is,When the user clicks on "vs12" the checkbox should not check or uncheck. When the user clicks on "box"only it should be checked on unchecked.

    For this i removed the text property for the checkbox and i added a HTML label with the same text.

    <asp:CheckBox ID="Chk1" runat="server" /><label>vs12</label>

    The above changes worked fine it overridden the default label.

    It rendered as

    <input name="Chk1" id="Chk1" type="checkbox" value="on"/>
    <label>vs12</lable>

    But the problem is when i want to make the checkbox invisible from code behind or from Javascript only the checkbox is getting invisible not the label. So i modified the code as follows.

    <asp:CheckBox ID="Chk1" runat="server" />
    <label for="Chk1" onclick="return false">vs12</label>

    But it is also not working as per my expectations. when i made the checkbox invisible it should invisible both lablel and checkbox. but only checkbox is getting invisible not a label text.

    When i made the checkbox invisible from the codebehind the checkbox and label are inside a span.

    //Aspx page
    <asp:CheckBox ID="Chk1" runat="server" Text="vs12"/>
    
    //Codebehind
    Chk1.Style.Add(HtmlTextWriterStyle.Display, "none");
    
    //rendered HTML to browser
    <SPAN style="DISPLAY: none">
    <INPUT id=Chk1 name=Chk1 value="" type=checkbox>
    <LABEL for=Chk1>vs12</LABEL>
    </SPAN>

    For my workaround, adding a label explicitly is not getting in to the span if i set the"for" property for the label also.

    My requirement is i need to disable clicking on checkbox text for check or uncheck and it should be easily accessible in javascript or codebehind.

    This is the issue in my whole application which contains around 100's of aspx pages. I tied to change it to normal html checkbox but the issue is i need to change the code in codebehind and javascript too.

    Please help me......

    Thanks in advance.

    • Moved by Jason Dot Wang Friday, October 12, 2012 6:26 AM This thread is about ASP.NET (From:Visual C# Language)
    Thursday, October 11, 2012 10:05 AM

All replies

  • The ASP.NET forums http://forums.asp.net is the better place for such questions

    With Thanks and Regards
    Sambath Raj.C
    click "Proposed As Answer by" if this post solves your problem or "Vote As Helpful" if a post has been useful to you
    Happy Programming!

    Thursday, October 11, 2012 10:15 AM