Refactor Code

  • Gravatar
    Populating DropDownLists in ASP.NET

    by azamsharp on 5/3/2008 2:24:06 PM
  • In my job application we have several DropDownLists in many pages and usually they are filled with the same data like "Departments", "Sections". For this reason I have extracted out the filling method and made it into a helper method. How can I improve the following:
  • public static void FillDropDownList<TSource>(DropDownList ddlist, IEnumerable<TSource> dataSource,
    string dataTextField, string dataValueField)
    {
    ddlist.AppendDataBoundItems = true;

    ddlist.DataSource = dataSource;
    ddlist.DataTextField = dataTextField;
    ddlist.DataValueField = dataValueField;
    ddlist.DataBind();
    }
  • Refactor it!
  • Gravatar
    If departments and Sections are not user based then i think better option would be to keep data in cache by using data caching and create dependency on that file.
    by muhammadadnan on 5/3/2008 11:04:12 PM
  • Gravatar
    Hi Adnan, Well the reason that the Departments and Sections are not placed in an XML file is that they have dependencies and relationships with other tables. I was more talking about how to fill the dropdownlist. In the code above I have implemented a generic method which fills a dropdownlist with any IEnumerable data source.
    by azamsharp on 5/4/2008 7:30:18 AM
  • Gravatar
    dear sir, i have small doubt for c#.net. how can i change the textbox to dropdownlist in Datagrid. '****************************************** private void DataGrid1_EditCommand_1(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) { DataGrid1.EditItemIndex=e.Item.ItemIndex; vbindata(); } private void vbinddata() { sql="select * from dbt_User_mst where username like '"+ username.Text +"%'"; connection cc=new connection(); SqlDataReader sd=cc.vReader(sql); DataGrid1.DataSource=sd; DataGrid1.DataBind(); }
    by rajesh on 5/13/2008 4:43:02 AM
Please log in to refactor the code! Login