Repeater嵌套gridview

前台:
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1" OnItemDataBound="Repeater1_ItemDataBound">
        <ItemTemplate>
           id:<%#Eval("ID")%>
           carid:<%#Eval("car_id")%>
           <asp:GridView ID="inGridView" runat="server" AutoGenerateColumns="False">
            <Columns>
                <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True"
                    SortExpression="ID" />
                <asp:BoundField DataField="name1" HeaderText="name1" SortExpression="name1" />
                <asp:BoundField DataField="sex" HeaderText="sex" SortExpression="sex" />
                <asp:BoundField DataField="qu" HeaderText="qu" SortExpression="qu" />
            </Columns>
        </asp:GridView>
           userid:<%#Eval("userID")%>
        </ItemTemplate>
        </asp:Repeater>

后台:
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            GridView gridview = (GridView)e.Item.FindControl("inGridView");
            DataRowView rowv = (DataRowView)e.Item.DataItem;
            string id = Convert.ToString(rowv["car_id"]);//Repeater1中绑定中的数据,也即主表上和子表的关联字段
            if (id != null && id != "")
            {
                string sqltext = "select top 5 id,name1,sex,qu from second where id='" + id + "'";
                DataSet ds = datacontrol.getdatas(sqltext);//获取从数据,这里是我的数据提取类,换成你自己的数据提取方法
                if (ds != null)
                {
                    try
                    {
                        gridview.DataSource = ds;
                        gridview.DataBind();
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(ex.Message);
                    }
                }
            }

        }

posted on 2014-06-26 12:18  lxboy  阅读(326)  评论(0编辑  收藏  举报

导航