The GridView nested DataList describes how to load data display using three different carriers in DataList.
Directory
- Using PlaceHolder load UserControl
- Directly Add data to DataList.
- Use UserControl to nest DatList
1. Using PlaceHolder load UserControl
WebForm:
<Asp: GridView ID = "GridView1" runat = "server" OnRowDataBound = "GridView1_RowDataBound">
<Columns>
<Asp: TemplateField>
<ItemTemplate>
<% # Eval ("Orderid") %>
</ItemTemplate>
</Asp: TemplateField>
<Asp: TemplateField>
<ItemTemplate>
<Asp: PlaceHolder ID = "place" runat = "server"> </asp: PlaceHolder>
</ItemTemplate>
</Asp: TemplateField>
</Columns>
</Asp: GridView>
UserControl:
<Asp: DataList ID = "DataList2" runat = "server" RepeatDirection = "Horizontal" GridLines = "Both">
<ItemTemplate>
Orderid: <% # Eval ("Orderid") %>
Unitprice: <% # Eval ("unitprice") %>
</ItemTemplate>
</Asp: DataList>
WebForm Code Bebind:
First, bind the GridView to the Page_Load event.
Then: In the GridView_RowDataBound event
IList <String> str = new List <String> (); // Add Test Data
Str. Add ("ID1 ");
Str. Add ("ID2 ");
Str. Add ("ID3 ");
Str. Add ("ID3 ");
If (e. Row. RowIndex>-1)
{
For (int I = 0; I <str. Count; I ++)
{
If (e. Row. RowIndex = I) // remove the loaded duplicate items.
{
// Method 1
PlaceHolder pl = e. Row. FindControl ("place") as PlaceHolder;
WebUserControl1 c = this. Page. LoadControl ("WebUserControl1.ascx") as WebUserControl1;
SqlDataAdapter sqlDataAdapter;
DataSet dataSet = new DataSet ();
SqlDataAdapter = new SqlDataAdapter (SQL statement, sqlConnection );
SqlDataAdapter. SelectCommand. Parameters. Add ("@ orderid", SqlDbType. Int). Value = OrderId Value
SqlDataAdapter. Fill (dataSet, "[order details]");
C. DataSource = dataSet. Tables ["[order details]"];
// Method 1
Pl. Controls. Add (c );
}
}
}
}
UC CodeBebind:
Protected void Page_Load (object sender, EventArgs e)
{
This. DataList2.DataSource = dataSource;
This. DataBind ();
}
Private DataTable dataSource;
Public DataTable DataSource
{
Set
{
DataSource = value;
}
}
2. directly Add data to DataList, and display
WebForm:
<Asp: GridView ID = "GridView1" runat = "server" OnRowDataBound = "GridView1_RowDataBound">
<Columns>
<Asp: TemplateField>
<ItemTemplate>
<% # Eval ("Orderid") %>
</ItemTemplate>
</Asp: TemplateField>
<Asp: TemplateField>
<ItemTemplate>
<Asp: DataList ID = "DataList2" runat = "server" RepeatDirection = "Horizontal">
<ItemTemplate>
Orderid: <% # Eval ("Orderid") %>
Unitprice: <% # Eval ("unitprice") %>
</ItemTemplate>
<Asp: DataList>
</ItemTemplate>
</Asp: TemplateField>
</Columns>
</Asp: GridView>
WebForm Code Bebind:
First, bind the GridView to the Page_Load event.
Then: In the GridView_RowDataBound event
IList <String> str = new List <String> (); // Add Test Data
Str. Add ("ID1 ");
Str. Add ("ID2 ");
Str. Add ("ID3 ");
Str. Add ("ID3 ");
If (e. Row. RowIndex>-1)
{
For (int I = 0; I <str. Count; I ++)
{
If (e. Row. RowIndex = I) // remove the loaded duplicate items.
{
// Method 2
DataList dl = e. Row. FindControl ("DataList2") as DataList;
SqlDataAdapter sqlDataAdapter;
DataSet dataSet = new DataSet ();
SqlDataAdapter = new SqlDataAdapter (SQL statement, sqlConnection );
SqlDataAdapter. SelectCommand. Parameters. Add ("@ orderid", SqlDbType. Int). Value = OrderId Value
SqlDataAdapter. Fill (dataSet, "[order details]");
C. DataSource = dataSet. Tables ["[order details]"];
Dl. DataSource = dataSet. Tables ["[order details]"];
Dl. DataBind ();
}
}
}
}
3. Use UserControl to nest DatList
WebForm:
<% @ Register Src = "WebUserControl1.ascx" TagName = "WebUserControl1" TagPrefix = "uc1" %>
<Asp: GridView ID = "GridView1" runat = "server" OnRowDataBound = "GridView1_RowDataBound">
<Columns>
<Asp: TemplateField>
<ItemTemplate>
<% # Eval ("Orderid") %>
</ItemTemplate>
</Asp: TemplateField>
<Asp: TemplateField>
<ItemTemplate>
</ItemTemplate>
</Asp: TemplateField>
</Columns>
</Asp: GridView>
UserControl:
<Asp: DataList ID = "DataList2" runat = "server" RepeatDirection = "Horizontal" GridLines = "Both">
<ItemTemplate>
Orderid: <% # Eval ("Orderid") %>
Unitprice: <% # Eval ("unitprice") %>
</ItemTemplate>
</Asp: DataList>
WebForm Code Bebind:
First, bind the GridView to the Page_Load event.
Then: In the GridView_RowDataBound event
IList <String> str = new List <String> (); // Add Test Data
Str. Add ("ID1 ");
Str. Add ("ID2 ");
Str. Add ("ID3 ");
Str. Add ("ID3 ");
If (e. Row. RowIndex>-1)
{
For (int I = 0; I <str. Count; I ++)
{
If (e. Row. RowIndex = I) // remove the loaded duplicate items.
{
// Method 3
WebUserControl1 c = e. Row. FindControl ("WebUserControl1") as WebUserControl1;
SqlDataAdapter sqlDataAdapter;
DataSet dataSet = new DataSet ();
SqlDataAdapter = new SqlDataAdapter (SQL statement, sqlConnection );
SqlDataAdapter. SelectCommand. Parameters. Add ("@ orderid", SqlDbType. Int). Value = OrderId Value
SqlDataAdapter. Fill (dataSet, "[order details]");
C. DataSource = dataSet. Tables ["[order details]"];
}
}
}
}
UC CodeBebind:
Protected void Page_Load (object sender, EventArgs e)
{
This. DataList2.DataSource = dataSource;
This. DataBind ();
}
Private DataTable dataSource;
Public DataTable DataSource
{
Set
{
DataSource = value;
}
}