Programming analysis of ASP. NET CheckBoxList component

Source: Internet
Author: User

ASP. NET CheckBoxList component programming: the CheckBox selection component is a common component in a program. When this component is used in programming, it is generally not used only for one, but often in the form of multiple such components. To use multiple CheckBox components on the ASP. NET page, in addition to adding multiple CheckBox components to the page, you can also use the CheckBoxList component. The CheckBoxList component is composed of a group of CheckBox components. In this component, the CheckBox appears as an entry, and each CheckBox in the CheckBoxList component has an index number, in this way, it is easier to process in the program.

At this point, you may ask, isn't this an option? Now that we have the CheckBox component, what should we do with the CheckBoxList component? This is because in the Process of program design, it is easier and clearer to process the CheckBoxList component than to process the CheckBox component. Example:

Assume that there is a CheckBoxList component and ten CheckBox components, and the CheckBoxList component is composed of these ten CheckBox components. To check which of the ten CheckBox components have been selected, the following code is required if the CheckBox component is selected in the program:

 
 
  1.  if ( C1 . Checked )  
  2.  {  
  3. }  
  4.  if ( C2 . Checked )  
  5.  {  
  6. }  
  7.  ....  
  8.  if ( C10 . Checked )  
  9.  {  
  10.  }  

However, if the CheckBoxList component is used in the program, you only need the following lines of code:

 
 
  1.  For(IntI = 0; I <CHK. Items. Count; I ++)
  2. {
  3. If(CHK. Items [I]. Selected)
  4. {
  5.  // Process the work you want to complete 
  6. }
  7. }

Note: C1 -- C10 is the CheckBox component and CHK is the CheckBoxList component.

It can be seen that the CheckBoxList component is used to make it clearer and more concise in programming. And as long as you have mastered the usage of the CheckBoxList component, the usage of the CheckBox component is generally enough.

1. How to create an ASP. NET CheckBoxList component:

 
 
  1. <Asp: CheckBoxList runat = & quot; server & quot; id = C1>
  2. <Asp: ListItem Value = 1> first check box </asp: ListItem>
  3. <Asp: ListItem Value = 2> second check box </asp: ListItem>
  4. <Asp: ListItem Value = 3> third check box </asp: ListItem>
  5. .....
  6.  
  7. // Note: You can add several check boxes here. 
  8.  
  9. </Asp: CheckBoxList>

Add the preceding statement to the ASP. NET page to generate a CheckBoxList component named & quot; C1 & quot.

Ii. Attributes frequently used in ASP. NET CheckBoxList components:

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 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 in 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;  &quot; ;  
  6. }  

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 and box placement in the check box in the component, the layout of each check box in the component, the orientation of each check box 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.  Case0:
  5. ChkList. TextAlign = TextAlign. Left;
  6. Break;
  7.  Case1:
  8. ChkList. TextAlign = TextAlign. Right;
  9. Break;
  10. }
  11. // Check box layout in the component 
  12. Switch(CboRepeatLayout. SelectedIndex)
  13. {
  14.  Case0:
  15. ChkList. RepeatLayout = RepeatLayout. Table;
  16. Break;
  17.  Case1:
  18. ChkList. RepeatLayout = RepeatLayout. Flow;
  19. Break;
  20. }
  21. // Check box arrangement direction in the component 
  22. Switch(CboRepeatDirection. SelectedIndex)
  23. {
  24.  Case0:
  25. ChkList. RepeatDirection = RepeatDirection. Vertical;
  26. Break;
  27.  Case1:
  28. ChkList. RepeatDirection = RepeatDirection. Horizontal;
  29. Break;
  30. }
  31. // The number of lines in each check box in the component 
  32. Try 
  33. {
  34.  IntCols =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 VoidButton_Click (ObjectSender, 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.  Case0:
  12. ChkList. TextAlign = TextAlign. Left;
  13. Break;
  14.  Case1:
  15. ChkList. TextAlign = TextAlign. Right;
  16. Break;
  17. }
  18. // Check box layout in the component 
  19. Switch(CboRepeatLayout. SelectedIndex)
  20. {
  21.  Case0:
  22. ChkList. RepeatLayout = RepeatLayout. Table;
  23. Break;
  24.  Case1:
  25. ChkList. RepeatLayout = RepeatLayout. Flow;
  26. Break;
  27. }
  28. // Check box arrangement direction in the component 
  29. Switch(CboRepeatDirection. SelectedIndex)
  30. {
  31.  Case0:
  32. ChkList. RepeatDirection = RepeatDirection. Vertical;
  33. Break;
  34.  Case1:
  35. ChkList. RepeatDirection = RepeatDirection. Horizontal;
  36. Break;
  37. }
  38. // The number of lines in each check box in the component 
  39. Try 
  40. {
  41.  IntCols =Int. Parse (txtRepeatCols. Text );
  42. ChkList. RepeatColumns = cols;
  43. }
  44. Catch(Exception)
  45. {
  46. }
  47. LblResult. Text = & quot ;;
  48. For(IntI = 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>

Vi. ASP. NET CheckBoxList component programming summary:

In fact, the CheckBoxList component is also a server component. This article describes some of the main attributes and methods of the CheckBoxList component. NET page, in fact, for another important component, CheckBox, they have many similarities, after understanding the usage of the CheckBoxList component, you can also master the usage of the CheckBox component.

The content of ASP. NET CheckBoxList component programming is introduced here. I hope it will be helpful for you to learn ASP. NET CheckBoxList component programming.

  1. Detailed explanation of the lifecycle of ASP. NET components
  2. Analysis on Transmission Mechanism of ASP. NET Component Design
  3. Analysis on complex attributes and status management of ASP. NET Component Design
  4. Analysis of ASP. NET httpHandler
  5. Programming of ASP. NET components

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.