• DataList in GridView


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

  • 相关阅读:
    Sql Server2005 TransactSQL 新兵器学习总结之数据类型
    Sql Server2005 TransactSQL 新兵器学习总结之TOP 运算符
    Sql Server2005 TransactSQL 新兵器学习总结之TRY…CATCH
    SQL Server函数大全(三)Union与Union All的区别
    Sql Server2005 TransactSQL 新兵器学习总结之APPLY 运算符
    Sql Server2005 TransactSQL 新兵器学习总结之公用表表达式(CTE)
    SDUT 飞行棋 (概率DP & 期望)
    HDU 4276 The Ghost Blows Light (树形DP)
    POJ 2096 Collecting Bugs (概率DP & 期望 )
    HDU 4118 Holiday's Accommodation (树形DP 哎,头脑不清晰,没看懂。。。。)
  • 原文地址:https://www.cnblogs.com/RuiLei/p/1047628.html
Copyright © 2020-2023  润新知