Asp.net Javascript gets the value of CheckBoxList

Source: Internet
Author: User

In the future, I will write out what I learned during this period and share it with you. This article is also a note at work. I hope to give some help to my friends who have encountered the same problem.
In the development work, because CheckBoxList is used to operate in js on the client, no matter how js is debugged, the value of value cannot be obtained. It was very depressing. Later, Google went down to the CodeProject, lucky. We place the following code on the webpage:
Copy codeThe Code is as follows:
<Asp: CheckBoxList runat = "server" ID = "chkDemo" RepeatDirection = "Horizontal" RepeatLayout = "Flow"> <asp: listItem Text = "test A" Value = "A"> </asp: ListItem>
<Asp: ListItem Text = "Test B" Value = "B"> </asp: ListItem>
<Asp: ListItem Text = "Test C" Value = "C"> </asp: ListItem>
<Asp: ListItem Text = "test D" Value = "D"> </asp: ListItem>
<Asp: ListItem Text = "Test E" Value = "E"> </asp: ListItem>
</Asp: CheckBoxList>

After the browser shows the code, let's look at the Html Script:
<Table id = "chkDemo" border = "0">
<Tr> <td> <input id = "chkDemo_0" type = "checkbox" name = "chkDemo $0"/> <label for = "chkDemo_0"> test A </label> </td>
<Td> <input id = "chkDemo_1" type = "checkbox" name = "chkDemo $1"/> <label for = "chkDemo_1"> Test B </label> </ td>
<Td> <input id = "chkDemo_2" type = "checkbox" name = "chkDemo $2"/> <label for = "chkDemo_2"> test C </label> </ td>
<Td> <input id = "chkDemo_3" type = "checkbox" name = "chkDemo $3"/> <label for = "chkDemo_3"> test D </label> </ td>
<Td> <input id = "chkDemo_4" type = "checkbox" name = "chkDemo $4"/> <label for = "chkDemo_4"> test E </label> </ td> </tr> </table>
This Html Script may differ in RepeatLayout settings, but it has one thing in common: the generated CheckBox has no value attribute,
Therefore, you cannot obtain the value using js on the client.
To solve this problem, we need to extend the CheckBoxList: this is the source code I found on CodeProject. After a long time, the link will not be pasted.
Copy codeThe Code is as follows:
[ToolboxData ("<{0}: CheckBoxListEx runat = \" server \ "> </{0}: CheckBoxListEx>")]
Public class CheckBoxListEx: CheckBoxList, IRepeatInfoUser
{
Void IRepeatInfoUser. RenderItem (ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)
{
String clientID = UniqueID + this. ClientIDSeparator + repeatIndex. ToString (NumberFormatInfo. InvariantInfo); // var

Writer. WriteBeginTag ("input ");
Writer. WriteAttribute ("type", "checkbox ");
Writer. WriteAttribute ("name", UniqueID + this. IdSeparator + repeatIndex. ToString (NumberFormatInfo. InvariantInfo ));
Writer. WriteAttribute ("id", clientID );
Writer. WriteAttribute ("value", Items [repeatIndex]. Value );
If (Items [repeatIndex]. Selected)
Writer. WriteAttribute ("checked", "checked ");

System. Web. UI. AttributeCollection attrs = Items [repeatIndex]. Attributes;
Foreach (string key in attrs. Keys)
{
Writer. WriteAttribute (key, attrs [key]);
}
Writer. Write ("/> ");
Writer. Write ("<label for = '" + clientID + "'> ");
Writer. Write (Items [repeatIndex]. Text );
Writer. Write ("</label> ");

}

I have modified the above Code, which is different from the original one: Generation of clientID and addition of the Checked attribute. I don't need to explain this code in detail.
Compile it into a separate class and it will automatically appear on the Toolbox. Just like using the normal CheckBoxList, you can drag it to the page.
On the client side, the value of js is roughly as follows:
Copy codeThe Code is as follows:
<Script>
Function getDemoValue ()
{Var els = document. getElementById ("chkDemo"); var vals = ''; if (els! = Null) {var chks = els. getElementsByTagName ("input"); for (var k = 0, len = chks. length; k <len; k ++) {var chk = chks [k]; if (chk! = Null & chk. type = 'checkbox' & chk. checked) {vals + = ',' + chk. value ;}}}
If (vals. length> 1)
Vals = vals. substring (1 );
Return vals;
}
</Script>

End

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.