Two new attributes are added to the checkbox control:InputattributesAndLabelattributes. By using these two attributes, you can easily add custom attributes to the label and input tags. However, attributes cannot be used to complete these two tasks. For example, the page defines an ASP: checkbox:
<asp:CheckBox ID="cbTest" Text="CheckBox" runat="server" />
Use in background codeAttributesAttribute to add an onclick event:
cbTest.Attributes.Add("onclick", "checkBoxClick(this)");
Then, browse the page and view the Page code:
<input id="cbTest" type="checkbox" name="cbTest" onclick="checkBoxClick(this);" /><label for="cbTest">CheckBox</label>
The background program adds an onclick event to the checkbox, but the label event cannot be added.LabelattributesAttribute to add an event to the label.
cbTest.InputAttributes.Add("onclick", "checkBoxClick(this)");
cbTest.LabelAttributes.Add("onclick", "checkBoxClick(this)");
View the Page code again:
<input id="cbTest" type="checkbox" name="cbTest" onclick="checkBoxClick(this)" /><label for="cbTest" onclick="checkBoxClick(this)">CheckBox</label>
It is found that the label has an onclick event. In the code above, we have used the inputattributes attribute to add events to the checkbox. This attribute achieves the same effect as the result of adding events using the attributes attribute. But they will be somewhat different. In some complex programs, Asp.net adds an additional <span> label to the checkbox when parsing the ASPX page, at this time, The onclick event added to the attributes attribute will be added to the span, and the second is not in the input. Therefore, we need the inputattributes attribute to add an event for the input.