Tuesday 29 May 2012

DropDownList


DropDownList is used to create a drop-down list in which each item is defined by listitems.
DropDownList is used when you want to bound the user such that he can select an option from a list of options.
Each item in drop-down list is defined by ListItem element.Inother words we can say that each item is created by ListItem control in asp.net.For example:    
<asp:dropdownlist ID="Dropdownlist1" runat="server">
        <asp:ListItem>United States</asp:ListItem>
        <asp:ListItem>Germany</asp:ListItem>
        <asp:ListItem>Japan</asp:ListItem>
        <asp:ListItem>China</asp:ListItem>
        <asp:ListItem>Russia</asp:ListItem>
        <asp:ListItem>Pakistan</asp:ListItem>
        <asp:ListItem>India</asp:ListItem>
        <asp:ListItem>Australia</asp:ListItem>
        <asp:ListItem>London</asp:ListItem>
        <asp:ListItem>Canada</asp:ListItem>
</asp:dropdownlist>    
Here you can see DropDownList contains a ListItem control from which you can add your values.
How to Add Values Manually in DropDownLIst?
To manually add values in drop-down list you can use the previious code else follow this picture you can also add values graphically.


How to Add Values Programmatically in DropDownLIst?
If you want to set values to drop-down list at run time then you can use the following code at page load event or some other place wher you want.

protected void Page_Load(object sender, EventArgs e)
        {
            RadioButtonList1.Items.Add(new ListItem("C#","C#"));
            RadioButtonList1.Items.Add(new ListItem("VB","VB"));
            RadioButtonList1.Items.Add(new ListItem("J#","J#"));
            RadioButtonList1.Items.Add(new ListItem("C++", "C++"));
        }



How to Add Values in DropDownLIst from Database?
if you want to get values from database and show them in drop-down list then use the following pocedure.
First of all place your DropDownList control into web form.After placing the control a small tag will appear with the control click on it.Then two options will appear select or click Choose Data Source.


if your data source already exists then select it else create a new data source.


Some Commonly Used Properties of DropDownList
Selected Value Property:
If you to get a selected value from the drop-down list then you will you the SelectedValue property.This property returns the string value of the selected item in drop-down list.
Label1.Text =  DropDownList.SelectedValue;


Selected Index Property:
SelectedIndex property is used to Gets or Sets the index of the selected item.This property returns the index value as integer;
int a = DropDownList.SelectedIndex;


Selected Item Property:
Selected Item property is used to gets the selected item with the lowest index in the list control.


To see more properties use the following link:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.dropdownlist_properties.aspx





2 comments:

  1. Thanks for this wonderful step by step tutorial. Drop down lists are commonly used by ASP NET programmers in their application, so it would be a lot of help to them.

    ReplyDelete
  2. Thanks for sharing, I will bookmark and be back again






    Dot Net Tutorial

    ReplyDelete