DataList in GridView
GridView嵌套DataList,这里讲述了在DataList中用三种不同载体,加载数据的显示方式
目录
- 使用PlaceHolder load UserControl的方式
- 使用直接Add数据到DataList,显示
- 使用UserControl嵌套DatList的方式
1.使用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:
首先:Page_Load事件中绑定GridView
然后:GridView_RowDataBound事件中
IList<String> str = new List<String>();//添加测试数据
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) //去掉加载的重复项
{
//第一个方式
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语句, sqlConnection);
sqlDataAdapter.SelectCommand.Parameters.Add("@orderid", SqlDbType.Int).Value = OrderId值
sqlDataAdapter.Fill(dataSet, "[order details]");
c.DataSource = dataSet.Tables["[order details]"];
//第一个方式
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.使用直接Add数据到DataList,显示
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:
首先:Page_Load事件中绑定GridView
然后:GridView_RowDataBound事件中
IList<String> str = new List<String>();//添加测试数据
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) //去掉加载的重复项
{
//第二个方式
DataList dl = e.Row.FindControl("DataList2") as DataList;
SqlDataAdapter sqlDataAdapter;
DataSet dataSet = new DataSet();
sqlDataAdapter = new SqlDataAdapter(SQL语句, sqlConnection);
sqlDataAdapter.SelectCommand.Parameters.Add("@orderid", SqlDbType.Int).Value = OrderId值
sqlDataAdapter.Fill(dataSet, "[order details]");
c.DataSource = dataSet.Tables["[order details]"];
dl.DataSource = dataSet.Tables["[order details]"];
dl.DataBind();
}
}
}
}
3.使用UserControl嵌套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:
首先:Page_Load事件中绑定GridView
然后:GridView_RowDataBound事件中
IList<String> str = new List<String>();//添加测试数据
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) //去掉加载的重复项
{
//第三个方式
WebUserControl1 c = e.Row.FindControl("WebUserControl1") as WebUserControl1;
SqlDataAdapter sqlDataAdapter;
DataSet dataSet = new DataSet();
sqlDataAdapter = new SqlDataAdapter(SQL语句, sqlConnection);
sqlDataAdapter.SelectCommand.Parameters.Add("@orderid", SqlDbType.Int).Value = OrderId值
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;
}
}