Programming The checkboxlist component of webform in ASP. NET

Source: Internet
Author: User

The component selected by checkbox 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:

If (C1. Checked)
{
}
If (C2. Checked)
{
}
....
If (C10. Checked)
{
}

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

For (INT I = 0; I <chk. Items. Count; I ++)
{
If (chk. items [I]. Selected)
{
// Process the work you want to complete
}
}

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 a checkboxlist component:

<Asp: checkboxlist runat = "server" id = C1>
<Asp: listitem value = 1> first check box </ASP: listitem>
<Asp: listitem value = 2> second check box </ASP: listitem>
<Asp: listitem value = 3> third check box </ASP: listitem>
.....
Note: You can add several check boxes here.
</ASP: checkboxlist>

Add the preceding statement to the ASP. NET page to generate a checkboxlist component named "C1.

2. Attributes frequently used in the checkboxlist component:

I>. textalign property: 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 the number of rows 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 check boxes in the checkboxlist component.

Iii. methods frequently used in the checkboxlist component:

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

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

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

Chklist. items [<index>]

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

Chklist. Items. Remove (<index>)
4. The example introduces how to use the 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:

For (INT I = 0; I <chklist. Items. Count; I ++)
{
If (chklist. items [I]. Selected)
{
Lblresult. Text + = chklist. items [I]. Text + "<br> ";
}
}

(2) how to set the appearance layout of the 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:

// The text in the check box and the position of the check box in the component
Switch (cboalign. selectedindex)
{
Case 0:
Chklist. textalign = textalign. Left;
Break;
Case 1:
Chklist. textalign = textalign. Right;
Break;
}
// Check box layout in the component
Switch (cborepeatlayout. selectedindex)
{
Case 0:
Chklist. repeatlayout = repeatlayout. Table;
Break;
Case 1:
Chklist. repeatlayout = repeatlayout. flow;
Break;
}

// Check box arrangement direction in the component
Switch (cborepeatdirection. selectedindex)
{
Case 0:
Chklist. repeatdirection = repeatdirection. vertical;
Break;
Case 1:
Chklist. repeatdirection = repeatdirection. horizontal;
Break;
}

// The number of lines in each check box in the component
Try
{
Int Cols = int. parse (txtrepeatcols. Text );
Chklist. repeatcolumns = Cols;
}
Catch (exception)
{
}
 
Check. aspx source code is as follows:

<% @ Page Language = "C #" %>

<HTML>
<Head>
<Title> checkboxlist component demo </title>
<SCRIPT runat = "server">
Protected void button_click (Object sender, eventargs E)
{
// The text in the check box and the position of the check box in the component
Switch (cboalign. selectedindex)
{
Case 0:
Chklist. textalign = textalign. Left;
Break;
Case 1:
Chklist. textalign = textalign. Right;
Break;
}

// Check box layout in the component
Switch (cborepeatlayout. selectedindex)
{
Case 0:
Chklist. repeatlayout = repeatlayout. Table;
Break;
Case 1:
Chklist. repeatlayout = repeatlayout. flow;
Break;
}

// Check box arrangement direction in the component
Switch (cborepeatdirection. selectedindex)
{
Case 0:
Chklist. repeatdirection = repeatdirection. vertical;
Break;
Case 1:
Chklist. repeatdirection = repeatdirection. horizontal;
Break;
}

// The number of lines in each check box in the component
Try
{
Int Cols = int. parse (txtrepeatcols. Text );
Chklist. repeatcolumns = Cols;
}
Catch (exception)
{
}

Lblresult. Text = "";
For (INT I = 0; I <chklist. Items. Count; I ++)
{
If (chklist. items [I]. Selected)
{
Lblresult. Text + = chklist. items [I]. Text + "<br> ";
}
}
}
</SCRIPT>
</Head>
<Body>
<Form runat = "server">
<H1 align = center> checkboxlist component demo <Table>
<Tr>
<TD> text placement in the component: </TD>
<TD>
<Asp: dropdownlist id = cboalign runat = "server">
<Asp: listitem> left </ASP: listitem>
<Asp: listitem> right </ASP: listitem>
</ASP: dropdownlist>
</TD>
</Tr>
<Tr>
<TD> layout of entries in the component: </TD>
<TD>
<Asp: dropdownlist id = cborepeatlayout runat = "server">
<Asp: listitem> table Type </ASP: listitem>
<Asp: listitem> compact </ASP: listitem>
</ASP: dropdownlist>
</TD>
</Tr>
<Tr>
<TD> arrangement of entries in the component: </TD>
<TD>
<Asp: dropdownlist id = cborepeatdirection runat = "server">
<Asp: listitem> horizontal direction </ASP: listitem>
<Asp: listitem> vertical </ASP: listitem>
</ASP: dropdownlist>
</TD>
</Tr>
<Tr>
<TD> Number of lines in each entry in the component: </TD>
<TD> <asp: textbox id = "txtrepeatcols" runat = "server"/> </TD>
</Tr>
</Table>
<Br>
Select the computer language type you want to learn:
<Br>
<Asp: checkboxlist id = "chklist" repeatdirection = horizontal runat = "server">
<Asp: listitem> visual c ++. Net </ASP: listitem>
<Asp: listitem> Visual C # </ASP: listitem>
<Asp: listitem> VB. NET </ASP: listitem>
<Asp: listitem> JScript. Net </ASP: listitem>
<Asp: listitem> Visual J # </ASP: listitem>
</ASP: checkboxlist>
<Br>
<Asp: button text = "Submit" runat = "server" onclick = "button_click"/>
<H1> <font color = Red> the computer language type you select is: </font> <Asp: Label id = lblresult runat = "server"/>
</Form>
</Body>
</Html>
 

6. 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.

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.