[. Net] checkboxlist usage

Source: Internet
Author: User

Attributes frequently used in the ASP. NET checkboxlist component:

I. textalign attributes: The value is left or right. If the value of textalign is left, the text of the check box in the checkboxlist component is on the left of the check box. Similarly, if the value of textalign is right, the text of the check box is on the right of the check box.

Ii. Selected attribute: boolean type, used to determine whether the check box in the component is selected.

Iii. repeatcolumns attributes: There are several check boxes in the checkboxlist component. This attribute mainly sets how many rows are used to display these check boxes.

Iv. repeatdirection attribute: the value of this attribute can be vertical or horizontal. After the repeatcolumns attribute is set, it is set to arrange the check boxes in the component. The details are as follows:

Assume that the checkboxlist component has four check boxes and the repeatcolumns attribute value is 2.

(1). If repeatdirection = vertical, check the box display on the page as follows:

Check box 01 check box 03

Check box 02 check box 04

(2) If repeatdirection = horizontal, check the box display on the page as follows:

Check box 01 check box 02

Check box 03 check box 04

V. Count attribute: return the number of check boxes in the checkboxlist component.

Iii. methods frequently used in ASP. NET checkboxlist component programming:

(1) Add a check box to the component. The syntax is as follows:

  1. Chklist. Items. Add (New listitem (<text >,< value> ))

(2). Access the check box in the component. The syntax is as follows:

  1. Chklist. items [<index>]

(3). Delete the check box in the component. The syntax is as follows:

  1. Chklist. Items. Remove (<index>)

4. The example introduces how to use the ASP. NET checkboxlist component:

(1). How to determine which check boxes are selected for the component:

In a program, the selected Attribute and count attribute are processed as follows:

  1. For (INT I = 0; I <chklist. Items. Count; I ++)
  2. {
  3. If (chklist. items [I]. Selected)
  4. {
  5. Lblresult. Text + = chklist. items [I]. Text + & quot ;;
  6. }
  7. }

(2) how to set the appearance layout of the ASP. NET checkboxlist component:

The checkboxlist component has many attributes to set its appearance. In the program described in this article, the component's appearance layout is mainly set through four aspects: the text in the check box and the position of the check box in the component, the layout of each check box in the component,

The check box arrangement direction in the component and the number of lines in each check box in the component. The specific program code is as follows:

  1. // The text in the check box and the position of the check box in the component
  2. Switch (cboalign. selectedindex)
  3. {
  4. Case 0:
  5. Chklist. textalign = textalign. Left;
  6. Break;
  7. Case 1:
  8. Chklist. textalign = textalign. Right;
  9. Break;
  10. }
  11. // Check box layout in the component
  12. Switch (cborepeatlayout. selectedindex)
  13. {
  14. Case 0:
  15. Chklist. repeatlayout = repeatlayout. Table;
  16. Break;
  17. Case 1:
  18. Chklist. repeatlayout = repeatlayout. flow;
  19. Break;
  20. }
  21. // Check box arrangement direction in the component
  22. Switch (cborepeatdirection. selectedindex)
  23. {
  24. Case 0:
  25. Chklist. repeatdirection = repeatdirection. vertical;
  26. Break;
  27. Case 1:
  28. Chklist. repeatdirection = repeatdirection. horizontal;
  29. Break;
  30. }
  31. // The number of lines in each check box in the component
  32. Try
  33. {
  34. Int Cols = int. parse (txtrepeatcols. Text );
  35. Chklist. repeatcolumns = Cols;
  36. }
  37. Catch (exception)
  38. {
  39. }

5. source code in the text (check. aspx ):

Check. aspx source code is as follows:

  1. <%@ Page Language = & quot; C # & quot; %>
  2. <HTML>
  3. <Head>
  4. <Title> checkboxlist component demo program </title>
  5. <SCRIPT runat = & quot; server & quot;>
  6. Protected void button_click (
    Object sender, eventargs E)
  7. {
  8. // The text in the check box and the position of the check box in the component
  9. Switch (cboalign. selectedindex)
  10. {
  11. Case 0:
  12. Chklist. textalign = textalign. Left;
  13. Break;
  14. Case 1:
  15. Chklist. textalign = textalign. Right;
  16. Break;
  17. }
  18. // Check box layout in the component
  19. Switch (cborepeatlayout. selectedindex)
  20. {
  21. Case 0:
  22. Chklist. repeatlayout = repeatlayout. Table;
  23. Break;
  24. Case 1:
  25. Chklist. repeatlayout = repeatlayout. flow;
  26. Break;
  27. }
  28. // Check box arrangement direction in the component
  29. Switch (cborepeatdirection. selectedindex)
  30. {
  31. Case 0:
  32. Chklist. repeatdirection = repeatdirection. vertical;
  33. Break;
  34. Case 1:
  35. Chklist. repeatdirection = repeatdirection. horizontal;
  36. Break;
  37. }
  38. // The number of lines in each check box in the component
  39. Try
  40. {
  41. Int Cols = int. parse (txtrepeatcols. Text );
  42. Chklist. repeatcolumns = Cols;
  43. }
  44. Catch (exception)
  45. {
  46. }
  47. Lblresult. Text = & quot ;;
  48. For (INT I = 0; I <chklist. Items. Count; I ++)
  49. {
  50. If (chklist. items [I]. Selected)
  51. {
  52. Lblresult. Text + = chklist. items [I]. Text + & quot ;;
  53. }
  54. }
  55. }
  56. </SCRIPT>
  57. </Head>
  58. <Body>
  59. <Form runat = & quot; server & quot;>
  60. <H1 align = center> checkboxlist component demo program
  61. <Table>
  62. <Tr>
  63. <TD> text placement in the component: </TD>
  64. <TD>
  65. <Asp: dropdownlist id = cboalign runat = & quot; server & quot;>
  66. <Asp: listitem> left </ASP: listitem>
  67. <Asp: listitem> right </ASP: listitem>
  68. </ASP: dropdownlist>
  69. </TD>
  70. </Tr>
  71. <Tr>
  72. <TD> layout of entries in the component: </TD>
  73. <TD>
  74. <Asp: dropdownlist id = cborepeatlayout runat = & quot; server & quot;>
  75. <Asp: listitem> table Type </ASP: listitem>
  76. <Asp: listitem> compact </ASP: listitem>
  77. </ASP: dropdownlist>
  78. </TD>
  79. </Tr>
  80. <Tr>
  81. <TD> the direction of each entry in the component: </TD>
  82. <TD>
  83. <Asp: dropdownlist id = cborepeatdirection runat = & quot; server & quot;>
  84. <Asp: listitem> horizontal direction </ASP: listitem>
  85. <Asp: listitem> vertical direction </ASP: listitem>
  86. </ASP: dropdownlist>
  87. </TD>
  88. </Tr>
  89. <Tr>
  90. <TD> Number of lines in each entry in the component: </TD>
  91. <TD> <asp: textbox id = & quot; txtrepeatcols & quot; runat = & quot; server & quot;/> </TD>
  92. </Tr>
  93. </Table>

Select the computer language type you want to learn:

  1. <Asp: checkboxlist id = & quot; chklist & quot; repeatdirection = horizontal runat = & quot; server & quot;>
  2. <Asp: listitem> visual
    C ++. Net </ASP: listitem>
  3. <Asp: listitem> visual
    C # </ASP: listitem>
  4. <Asp: listitem> VB. NET </ASP: listitem>
  5. <Asp: listitem> JScript. Net </ASP: listitem>
  6. <Asp: listitem> Visual J # </ASP: listitem>
  7. </ASP: checkboxlist>
  8. <Asp: button text = & quot; Submit & quot; runat = & quot; server & quot; onclick = & quot; button_click & quot;/>
  9. <H1> <font color = Red> the computer language type you selected is: </font>
  10. <Asp: Label id = lblresult runat = & quot; server & quot;/>
  11. </Form>
  12. </Body>
  13. </Html>

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.