Use System.Collection.Generic method to Load Dropdownlist

Namespace:

using System.Collections.Generic;

Learning:

we are making use of generic method to load dropdownlist with collection of any type. The motive here is to understand that web forms may have n number of dropdownlist control and each time we won't be writing same code replicating the functionality so as to reduce redundant code this approach will fetch good outcome with respect to clean and neat code.

Code Construct:




public partial class _Default : System.Web.UI.Page
{
private DropDownList m_DropdownList = null;
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (Page.IsPostBack == false)
{
/*
IDictionary countryList = new Dictionary();
countryList.Add(00, "INDIA");
countryList.Add(01, "SRILANKA");
countryList.Add(02, "NEPAL");
countryList.Add(03, "BUTAN");
*/
Hashtable countryHashList = new Hashtable();
countryHashList.Add(0, "INDIA");
countryHashList.Add(1, "SRILANKA");
countryHashList.Add(2, "NEPAL");
countryHashList.Add(3, "BUTAN");


m_DropdownList = drpCountry;
/*Dictionary List

BindDropdownlist(countryList);

*/

/*** Hashtable
BindDropdownlist(countryHashList);
//*** So similarly we can have datasource of any type here.


}
}
catch (Exception ex)
{
throw ex;
}
finally
{

}
}

//Generic Method
private void BindDropdownlist <T>(T list)
{
//*** Bind Grid
DropdownControl.DataSource = list;
DropdownControl.DataBind();
}

//Public Property
public DropDownList DropdownControl
{
get { return m_DropdownList; }
set { m_DropdownList = value; }
}
}


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: