Tuesday, September 22, 2015

List controls in asp.net

Suggested Videos
Part 26 - ASP.NET CheckBoxList and ListBox real time example
Part 27 - ASP.NET RadioButtonList control
Part 28 - Bulleted list in asp.net

In ASP.NET there are several list controls, like
1. DropDownList
2. CheckBoxList
3. RadioButtonList
4. ListBox
5. BulletedList

All these controls are 
1. Collection of ListItem objects
2. ListItems can be added in the HTML source or in the code behind file
3. Supports Databinding



The only difference here is the tag name, otherwise adding ListItems is very identical.
<asp:DropDownList ID="DropDownList1" runat="server">
    <asp:ListItem Text="Item1" Value="1"></asp:ListItem>
    <asp:ListItem Text="Item2" Value="2"></asp:ListItem>
    <asp:ListItem Text="Item3" Value="3"></asp:ListItem>
</asp:DropDownList>
<br />
<asp:CheckBoxList ID="CheckBoxList1" runat="server">
    <asp:ListItem Text="Item1" Value="1"></asp:ListItem>
    <asp:ListItem Text="Item2" Value="2"></asp:ListItem>
    <asp:ListItem Text="Item3" Value="3"></asp:ListItem>
</asp:CheckBoxList>
<br />
<asp:RadioButtonList ID="RadioButtonList1" runat="server">
    <asp:ListItem Text="Item1" Value="1"></asp:ListItem>
    <asp:ListItem Text="Item2" Value="2"></asp:ListItem>
    <asp:ListItem Text="Item3" Value="3"></asp:ListItem>
</asp:RadioButtonList>
<br />
<asp:ListBox ID="ListBox1" runat="server">
    <asp:ListItem Text="Item1" Value="1"></asp:ListItem>
    <asp:ListItem Text="Item2" Value="2"></asp:ListItem>
    <asp:ListItem Text="Item3" Value="3"></asp:ListItem>
</asp:ListBox>
<br />
<asp:BulletedList ID="BulletedList1" runat="server">
    <asp:ListItem Text="Item1" Value="1"></asp:ListItem>
    <asp:ListItem Text="Item2" Value="2"></asp:ListItem>
    <asp:ListItem Text="Item3" Value="3"></asp:ListItem>
</asp:BulletedList>



Adding ListItems using code. Since all the list controls inherit from ListControl class, AddListItems() method can be used to add listitems to any list control. A parent class reference variable can point to a derived class object.

This fact allows us to pass any list control into the 
AddListItems() method as a parameter. We have discussed about inheritance in Part 21 and polymorphism in Part 23 of C# Video Tutorial Series.
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        AddListItems(DropDownList1);
        AddListItems(CheckBoxList1);
        AddListItems(RadioButtonList1);
        AddListItems(ListBox1);
        AddListItems(BulletedList1);
    }
}
private void AddListItems(ListControl listControl)
{
    ListItem li1 = new ListItem("Item1", "1");
    ListItem li2 = new ListItem("Item2", "2");
    ListItem li3 = new ListItem("Item3", "3");

    listControl.Items.Add(li1);
    listControl.Items.Add(li2);
    listControl.Items.Add(li3);
}

ListBox (If SelectionMode=Multiple) and CheckBoxList allows user to select multiple items. So, to retrieve all the selected listitem's Text, Value and Index use a foreach loop.

Reusable method that can be used with any control that derives from ListControl class, but works best with controls 
that allows multiple selections.
private void RetrieveMultipleSelections(ListControl listControl)
{
    foreach (ListItem li in listControl.Items)
    {
        if (li.Selected)
        {
            Response.Write("Text = " + li.Text + ", Value = " + li.Value +
                ", Index = " + listControl.Items.IndexOf(li).ToString() + "<br/>");
        }
    }
}

ListBox (If SelectionMode=Single), RadioButtonList and DropDownList allows user to select only one item. So, use SelectedIndex and SelectedItem properties to retrieve the Text, Value and Index of the selected listitem.

Reusable method that can be used with any control that derives from 
ListControl class, but works best with controls that allows single selection.
private void RetrieveSelectedItemTextValueandIndex(ListControl listControl)
{
    if (listControl.SelectedIndex != -1)
    {
        Response.Write("Text = " + listControl.SelectedItem.Text + "<br/>");
        Response.Write("Value = " + listControl.SelectedItem.Value + "<br/>");
        Response.Write("Index = " + listControl.SelectedIndex.ToString());
    }
}

BulletedList(If DisplayMode=LinkButton) - In the click event handler, use the BulletedListEventArgs parameter object to retrieve the Text, Value and Index of the listitem, the user has clicked.
protected void BulletedList1_Click(object sender, BulletedListEventArgs e)
{
    ListItem li = BulletedList1.Items[e.Index];
    Response.Write("Text = " + li.Text + "<br/>");
    Response.Write("Value = " + li.Value + "<br/>");
    Response.Write("Index = " + e.Index.ToString());
}


If you are searching life partner. your searching end with kpmarriage.com. now kpmarriage.com offer free matrimonial website which offer free message, free chat, free view contact information. so register here : kpmarriage.com- Free matrimonial website

Related Posts:

  • SQL WHERE ClauseThe WHERE clause is used to filter records. The WHERE clause is used to extract only those records that fulfill a specified criterion. SQL WHERE Syn… Read More
  • SQL JOINThe JOIN keyword is used in an SQL statement to query data from two or more tables, based on a relationship between certain columns in these tables. … Read More
  • SQL SELECT INTO StatementThe SQL SELECT INTO statement can be used to create backup copies of tables. The SQL SELECT INTO StatementThe SELECT INTO statement selects data from … Read More
  • SQL UNION OperatorThe SQL UNION Operator The UNION operator is used to combine the result-set of two or more SELECT statements. Notice that each SELECT statement with… Read More
  • SQL AND & OR OperatorsThe AND & OR operators are used to filter records based on more than one condition. The AND & OR OperatorsThe AND operator displays a record… Read More

0 comments:

Post a Comment