write a custom reusable function to populate a dropdownlist



Write a custom function in c-sharp. The custom function parameters should be an instance of a dropdownlist, an xml file and a string.
1. The function should be capabale of populating the passed in dropdownlist.
2. The first item in the dropdownlist should be the passed in string parameter.
3. The data for the dropdownlist comes from the passed in XML file.

The idea is to create a custom function which can be reused through out the project for populating any dropdownlist on any web page. You have 20 minutes to code, test and demonstrate.

The sample code for custom function is shown below. For this example to work drop the XML file in the root folder of the web application.

protected void Page_Load(object sender, EventArgs e)
{
PopulateDropdownlist(DropDownList1, "DropDownListSource.xml", "Select State");
}

public void PopulateDropdownlist(System.Web.UI.WebControls.DropDownList DropDownListObjectToBePopulated,string XMLFilePath, string InitialString)
{
try
{
DataSet DS = new DataSet();
DS.ReadXml(Server.MapPath(XMLFilePath));
if (InitialString != string.Empty)
{
ListItem LI = new ListItem(InitialString, "-1");
DropDownListObjectToBePopulated.Items.Add(LI);
}
foreach (DataRow DR in DS.Tables["State"].Rows)
{
ListItem LI = new ListItem();
LI.Text = DR["StateName"].ToString();
LI.Value = DR["StateCode"].ToString();
DropDownListObjectToBePopulated.Items.Add(LI);
}
}
catch(Exception Ex)
{
}
}

The XML file that has the data for the dropdownlist is as shown below.

<?xml version="1.0" encoding="utf-8" ?>
<StatesList>
<State>
<StateName>Virginia</StateName>
<StateCode>VA</StateCode>
</State>
<State>
<StateName>Iowa</StateName>
<StateCode>IA</StateCode>
</State>
<State>
<StateName>North Carolina</StateName>
<StateCode>NC</StateCode>
</State>
<State>
<StateName>Pennsylvania</StateName>
<StateCode>PA</StateCode>
</State>
<State>
<StateName>Texas</StateName>
<StateCode>TX</StateCode>
</State>
</StatesList>

Explanation of the code:
1.
PopulateDropdownlist function has 3 parameters. DropDownList to be populated, the path of the XML file which has the data for the dropdownlist and the initial string.

2. Create an instance of DataSet. In our example the instance is DS.
DataSet DS = new DataSet();

3. Read the XML data into the dataset instance using ReadXml() method. Pass the path of the XML file to ReadXml() method. We used Server.MapPath() method to return the physical file path that corresponds to the specified virtual path on the web server.
DS.ReadXml(Server.MapPath(XMLFilePath));

4. We now have the data from the XML file in the dataset as a DataTable.

5. Check if the InitialString is empty. If not empty create a new ListItem object and populate the Text and Value properties. Then add the listitem object to the dropdownlist.
if (InitialString != string.Empty)
{
ListItem LI = new ListItem(InitialString, "-1");
DropDownListObjectToBePopulated.Items.Add(LI);
}

6. Finally loop thru the rows in the DataTable and create an instance of ListItem class. Populate the Text and Value properties to StateName and StateCode respectively. Finally add the ListItem object to the dropdownlist.
foreach (DataRow DR in DS.Tables["State"].Rows)
{
ListItem LI = new ListItem();
LI.Text = DR["StateName"].ToString();
LI.Value = DR["StateCode"].ToString();
DropDownListObjectToBePopulated.Items.Add(LI);
}

7. Drag and drop the dropdownlist on a webform. Call the PopulateDropdownlist() custom function in the Page_Load event handler. When you call the custom function pass the dropdownlist to be populated, XML file path and the initial string.
protected void Page_Load(object sender, EventArgs e)
{
PopulateDropdownlist(DropDownList1, "DropDownListSource.xml", "Select State");
}

4 comments:

  1. Wonderful example and explanation! To make it more practical could you please add a sort to this code so the dropdown shows values sorted in an ascending order. Thanks

    ReplyDelete
  2. super dear...tx a lot

    ReplyDelete
  3. Instead of looping through the items and adding in the DropDownList, we wold also use DataSource property to populate the items as follows:

    DataSet ds = new DataSet();
    ds.ReadXml(Server.MapPath(XMLFilePath));

    if (InitialString != string.Empty)
    {
    ListItem LI = new ListItem(InitialString, "-1");
    DropDownListObjectToBePopulated.Items.Add(LI);
    }

    DropDownListObjectToBePopulated.AppendDataBoundItems = true;
    DropDownListObjectToBePopulated.DataTextField = "Name";
    DropDownListObjectToBePopulated.DataValueField = "ID";
    DropDownListObjectToBePopulated.DataSource = ds.Tables[0];
    DropDownListObjectToBePopulated.DataBind();

    - DD

    ReplyDelete

If you are aware of any other asp.net questions asked in an interview, please post them below. If you find anything missing or wrong, please feel free to correct by submitting the form below.

 
Disclaimer - Terms of use - Contact Us