.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
sritaa
Sandeep Singh
Melody Anderson
Sharad
Eminent IT

  

DropDownList Control or Combobox

   
 
     


Introduction :

Dropdownlist Control is used for selecting one predefined (available in DropdownList) value. That means user can be able to select only one value at a time. For eg., it is useful for selecting the birth year, Highest Qualification etc., The benefit of using DropDownList is it takes less space on your web page while designing your website. ASP.NET Dropdownlist itself is a class like other controls which are present under System.Web.UI.WebControls namespace.

Dropdownlist Control is used for selecting one predefined (available in DropdownList) value. That means user can be able to select only one value at a time. For eg., it is useful for selecting the birth year, Highest Qualification etc., The benefit of using DropDownList is it takes less space on your web page while designing your website.
ASP.NET Dropdownlist itself is a class like other controls which are present under System.Web.UI.WebControls namespace.

Important Properties of Asp.NET Webserver  Button control are given below. You can Set /Get these properties at Design time or at Runtime.

AutoPostBack -  Accept Boolean Value  - If True, then page will submit to the server if the index of the dropdown is changed

CausesValidation -  Accept Boolean Value - Used to Validate the page when Button click event fires.

DataTextField - Accept String Value - Used to set the column Name from  the DataSource which user Can see (eg. Name).

DataVaueField - Accept String Value - Used to set the column Name from  the DataSource which user Can not see (eg. ID).

Enabled - Accept Boolean Value- Used to Get/Set button Enabled

Height - Accept IntergerValue  -  Used to Set/Get height of the textbox

Width - Accept IntergerValue -  Used to set the width of the Button

Events available in Asp.Net Webserver DropDownList Control are:

DataBound - This event will fire when you call assigned the dataSource property and you write this line of code to bind your dropDownList

myDropDownList.DataBind();//myDropDownList is the id of your dropDownList;

SelectedIndexChanged - This event will fire when user has changed his/her selection and DropDownList's "AutoPostBack" property is set to true;

Lets See an example

Your .aspx code will look like this for a WebServer Control DropDownList

<asp:DropDownList ID="MyDropdownList" runat="server" AutoPostBack="True"

            onselectedindexchanged="MyDropdownList_SelectedIndexChanged" ondatabound="MyDropdownList_DataBound" >

        </asp:DropDownList>


Full Html Code used in attached example

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title>Dropdownlist Example - DotNetSpark.com</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        <asp:Label ID="lblDropDownList" runat="server" Text="DropDownList Example"></asp:Label> </br> </br>

        <asp:DropDownList ID="MyDropdownList" runat="server" AutoPostBack="True"

            onselectedindexchanged="MyDropdownList_SelectedIndexChanged" ondatabound="MyDropdownList_DataBound" >

        </asp:DropDownList>

   

     

   

        <br />

   

    </div>

    <asp:Label ID="lblSelectedVal" runat="server"></asp:Label>

    </form>

</body>

</html>



And to Fill the value in DropdownList control  in any event you want just add this lines of code

MyDropdownList.DataSource = MyDataTable(); //DataTable as Data Source

MyDropdownList.DataTextField = "SomeText";//Column name from Table for user to see

MyDropdownList.DataValueField = "ID"; //Column name hidden to the user

MyDropdownList.DataBind();// Bind the DataTable the DropDownList Control

Other Tutorials in the same Category:
      
  • CheckBoxList Control      
  • Button Control      
  • Calendar Control      
  • CheckBox Control      
  • DropDownList Control or Combobox      
  • FileUpload Control      
  • Hidden Field Control      
  • ImageButton Control      
  • Link Button Control      
  • ListBox Control      
  • Literal Control      
  • RadioButton Control      
  • RadioButtonList Control      
  • TextBox Control

  •  
         
     
    Return to Main Index Page


    Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend