Link to home
Start Free TrialLog in
Avatar of vielkacarolina1239
vielkacarolina1239Flag for United States of America

asked on

How to use a DataList?

I am using ASP.NET 2.0

I have a datalist control. I have bound the datalist control to a DataTable. This DataTable contains
3 fields and 8 records. I need to add 3 dropdownlist, one for each column in the DataTable, to the DataList control. I need to bind the DropDownLists to the DataTable fields. Each of the DropDownList should contain 8 records and there should be 3 dropdownlists in the datalist control.

How can I do this. Please, show me the code.
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

You need to define an ItemTemplate that will get rendered for each item in the DataList.


ASP.NET Data Controls Part 3: DataList

Avatar of vielkacarolina1239


If I do what you have suggested, I will not get what I need. If we refere to the example I have posted of 3 dropdown list and 8 records in the DataTable and follow your suggestion, I will have 3 dropdownlists for every item to wich the datalist is bound to, and each of the dropdownlist will contain 1 record. This means I will have 3 * 8 = 24 dropdownlists, each with 1 record. This is not what I am looking for. I need to bind the dropdownlists to 8 records in one field, not 1 record (every time that the list is bound to an item in the DataTable).

Please, can someone show me how to fix the below code snippet so that DropDownList0 will contain the 8 records for the Index field, DropDownList1 will contain the 8 records for the Name field, DropDownList2 will contain the 8 records for the Year field, and each of the dropdownlist should show only once and not every time there occurs a bind in the datalist.


//my .ascx page contains the following:
<asp:DataList ID="list" runat="server" OnItemDataBound="list_ItemDataBound">
        <asp:DropDownList ID="DropDownList0" runat="server" >   
        <asp:DropDownList ID="DropDownList1" runat="server"> 
        <asp:DropDownList ID="DropDownList2" runat="server" > 
//my .ascx.cs contains the following:
//this is the DataTable use for binding the DataList and dropdownlists
DataTable dt; 
//This method creates the DataTable and bind the list
protected void Page_Load(object sender, EventArgs e)
dt = new DataTable();
DataColumn dc1 = new DataColumn("Index", System.Type.GetType
DataColumn dc2 = new DataColumn("Name", System.Type.GetType
DataColumn dc3 = new DataColumn("Year", System.Type.GetType
int[] index = new int[] { 0, 1, 2, 3, 4, 5, 6, 7 };
string [] names= {"darren", "peter", "robin", "phil", "wayne", "rob", "doug", "sam"};
int[] year = new int[] { 1990, 19991, 1992, 1993, 1994, 1995, 1996, 1997 };
DataRow dr;
for (int i = 0; i < index.Length; i++ )
   dr = dt.NewRow();
   dr["Index"] = i;
   dr["Name"] = names[i];
   dr["Year"] = year[i];
list.DataSource = dt;
//this method binds the dropdownlists
protected void list_ItemDataBound(object sender, 
                                              DataListItemEventArgs e)
DropDownList DropDownList0 = (DropDownList)e.Item.FindControl
DropDownList0.Items.Add(new ListItem( 
(((DataRowView)e.Item.DataItem).Row.ItemArray[0].ToString()), "Index"));
DropDownList0.DataValueField = "Index";
DropDownList0.DataTextField = "Index";
DropDownList DropDownList1 = (DropDownList)e.Item.FindControl
DropDownList1.Items.Add(new ListItem( 
(((DataRowView)e.Item.DataItem).Row.ItemArray[1].ToString()), "Index"));
DropDownList1.DataValueField = "Index";
DropDownList1.DataTextField = "Year";
DropDownList DropDownList2 = (DropDownList)e.Item.FindControl
DropDownList2.Items.Add(new ListItem(
(((DataRowView)e.Item.DataItem).Row.ItemArray[2].ToString()), "Index"));
DropDownList2.DataValueField = "Index";
DropDownList2.DataTextField = "Year";

Open in new window

How can I bind multiple records per row in a DataList?

This article says that this is posible.
Analyzing the DataList
Recall that the DataGrid renders as an HTML <table>, which each DataSource record as a table row (<tr>) and each record field as a table column (<td>). At times you might want more control over the presentation of data. For example, you might want to have the data displayed in an HTML <table>, but rather than have one record per row, you might want to display five records per row. Alternatively, you might not want to have the data displayed in a <table> tag at all, but rather have each element displayed in a <span> tag.

Do you need 1 row with 3 DropDownList controls each with 8 entries?

Yes that is exactly what I am looking for.
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial