DataList in GridView

本文详细介绍了在ASP.NET WebForm中实现GridView嵌套DataList的三种方法:使用PlaceHolder加载UserControl、直接在GridView中添加DataList及使用UserControl嵌套DataList。每种方法都提供了具体的代码示例,帮助开发者更好地理解和应用。
摘要由CSDN通过智能技术生成

GridView嵌套DataList,这里讲述了在DataList中用三种不同载体,加载数据的显示方式

目录

  • 使用PlaceHolder load UserControl的方式
  • 使用直接Add数据到DataList,显示
  • 使用UserControl嵌套DatList的方式

datalistingridview

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;
           }
       }

转载于:https://www.cnblogs.com/RuiLei/archive/2008/01/21/1047628.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值