C# 如何为中继器中嵌套的子gridview提供导航

C# 如何为中继器中嵌套的子gridview提供导航,c#,asp.net,gridview,nested,repeater,C#,Asp.net,Gridview,Nested,Repeater,我需要您的帮助,我有一个嵌套的中继器父级(Repeater1)和子gridview(gvNewChild)。父中继器正在从父表调用记录Id(FId),子gridview正在从子表获取父中继器的OnItemDataBound上的记录Id(FId),因此父表和子表都不同,但与FId链接,后者在两个表中都是相同的记录Id号 现在,我可以分别使用它们的FId从两个表中检索outer repeater和child gridview中的数据。但是我想知道是否有可能在child gridview中提供导航,而

我需要您的帮助,我有一个嵌套的中继器父级(
Repeater1
)和子gridview(
gvNewChild
)。父中继器正在从父表调用记录Id(
FId
),子gridview正在从子表获取父中继器的
OnItemDataBound
上的记录Id(
FId
),因此父表和子表都不同,但与
FId
链接,后者在两个表中都是相同的记录Id号

现在,我可以分别使用它们的
FId
从两个表中检索outer repeater和child gridview中的数据。但是我想知道是否有可能在child gridview中提供导航,而不是一次显示所有记录,所以实际上我希望我的child repeater只显示一条记录,并访问我想使用下一条和上一条导航的其他记录。请引导我,下面是代码:


GridView ID=“gvNewChild”runat=“server”DataKeyNames=“FId”AutoGenerateColumns=“false”
CssClass=“tableWall”HeaderStyle BorderColor=“#ffffff”GridLines=“无”可见=“真”
AllowPagin=“true”PageSize=“1”OnPageIndexChanging=“gvNewChild\u PageIndexChanging”>


资料来源如下:

if (!IsPostBack)
{
    Repeater1.DataSource = GetData("select *from News order by (Id) desc");
    Repeater1.DataBind();
}

private static DataTable GetData(string query)
{
    string strConnString = ConfigurationManager.ConnectionStrings["BHCONNECTION"].ConnectionString;
    using (SqlConnection con = new SqlConnection(strConnString))
    {
        using (SqlCommand cmd = new SqlCommand())
        {
            cmd.CommandText = query;
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.Connection = con;
                sda.SelectCommand = cmd;
                using (DataSet ds = new DataSet())
                {
                    DataTable dt = new DataTable();
                    sda.Fill(dt);
                    return dt;
                }
            }
        }
    }
}

protected void OnItemDataBound(object sender, RepeaterItemEventArgs e)
{ 
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        string FId = (e.Item.FindControl("hfFId") as HiddenField).Value;
        Repeater rptOrders = e.Item.FindControl("rptOrders") as Repeater;
        rptOrders.DataSource = GetData(string.Format("select * from NewsPic where FId='{0}'", FId));
        rptOrders.DataBind();
    }
}


 protected void gvNewChild_PageIndexChanging(object sender, GridViewPageEventArgs e)
    { 
        GridView gvNewChild = (sender as GridView);
        gvNewChild.PageIndex = e.NewPageIndex;
        gvNewChild.DataBind();

    }

请看一个快速示例:

HTML标记:

<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
    <ItemTemplate>

        <asp:Label ID="lblFID" runat="server" Text='<%# Eval("FID") %>'></asp:Label>
        <!-- Your other controls -->
        <asp:GridView ID="GridView1" runat="server" AllowPaging="true" PageSize="1"
        AutoGenerateColumns="false" OnPageIndexChanging="GridView1_PageIndexChanging" >
            <Columns>
                <asp:TemplateField>
                <ItemTemplate>

               <asp:Label ID="lblFID" runat="server" Text='<%# Eval("FID") %>'></asp:Label>
               <!-- Your other controls -->

                </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <PagerStyle HorizontalAlign="Right" CssClass="pagination-ys" />
        </asp:GridView>

    </ItemTemplate>
</asp:Repeater>
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || 
             e.Item.ItemType == ListItemType.AlternatingItem)
    {
        string FId = (e.Item.FindControl("FID") as Label).Text;
        GridView GridView1 = e.Item.FindControl("GridView1") as GridView;

        // select data from parent Table
        GridView1.DataSource = 
                GetData(string.Format("select * from ParentTable where FId='{0}'", FId));
        GridView1.DataBind();
    }
}

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    // get repeater item where clicked
    RepeaterItem item = ((GridView)sender).Parent as RepeaterItem;
    string FId = ((Label)Repeater1.Items[item.ItemIndex].FindControl("FID")).Text;

    // get gridview from Repeater
    GridView GridView1 = (sender as GridView);
    GridView1.PageIndex = e.NewPageIndex;

    // select data from child table
    GridView1.DataSource = 
            GetData(string.Format("select * from ChildTable where FId='{0}'", FId));
    GridView1.DataBind();
}
.pagination-ys {
    //display: inline-block;
    padding-left: 0;
    margin: 20px 0;
    border-radius: 4px;
}

.pagination-ys table > tbody > tr > td {
    display: inline;
}

.pagination-ys table > tbody > tr > td > a,
.pagination-ys table > tbody > tr > td > span {
    position: relative;
    float: left;
    padding: 8px 12px;
    line-height: 1.42857143;
    text-decoration: none;
    color: #dd4814;
    background-color: #ffffff;
    border: 1px solid #dddddd;
    margin-left: -1px;
}

.pagination-ys table > tbody > tr > td > span {
    position: relative;
    float: left;
    padding: 8px 12px;
    line-height: 1.42857143;
    text-decoration: none;    
    margin-left: -1px;
    z-index: 2;
    color: #aea79f;
    background-color: #f5f5f5;
    border-color: #dddddd;
    cursor: default;
}

.pagination-ys table > tbody > tr > td:first-child > a,
.pagination-ys table > tbody > tr > td:first-child > span {
    margin-left: 0;
    border-bottom-left-radius: 4px;
    border-top-left-radius: 4px;
}

.pagination-ys table > tbody > tr > td:last-child > a,
.pagination-ys table > tbody > tr > td:last-child > span {
    border-bottom-right-radius: 4px;
    border-top-right-radius: 4px;
}

.pagination-ys table > tbody > tr > td > a:hover,
.pagination-ys table > tbody > tr > td > span:hover,
.pagination-ys table > tbody > tr > td > a:focus,
.pagination-ys table > tbody > tr > td > span:focus {
    color: #97310e;
    background-color: #eeeeee;
    border-color: #dddddd;
}
分页样式:

<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
    <ItemTemplate>

        <asp:Label ID="lblFID" runat="server" Text='<%# Eval("FID") %>'></asp:Label>
        <!-- Your other controls -->
        <asp:GridView ID="GridView1" runat="server" AllowPaging="true" PageSize="1"
        AutoGenerateColumns="false" OnPageIndexChanging="GridView1_PageIndexChanging" >
            <Columns>
                <asp:TemplateField>
                <ItemTemplate>

               <asp:Label ID="lblFID" runat="server" Text='<%# Eval("FID") %>'></asp:Label>
               <!-- Your other controls -->

                </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <PagerStyle HorizontalAlign="Right" CssClass="pagination-ys" />
        </asp:GridView>

    </ItemTemplate>
</asp:Repeater>
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || 
             e.Item.ItemType == ListItemType.AlternatingItem)
    {
        string FId = (e.Item.FindControl("FID") as Label).Text;
        GridView GridView1 = e.Item.FindControl("GridView1") as GridView;

        // select data from parent Table
        GridView1.DataSource = 
                GetData(string.Format("select * from ParentTable where FId='{0}'", FId));
        GridView1.DataBind();
    }
}

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    // get repeater item where clicked
    RepeaterItem item = ((GridView)sender).Parent as RepeaterItem;
    string FId = ((Label)Repeater1.Items[item.ItemIndex].FindControl("FID")).Text;

    // get gridview from Repeater
    GridView GridView1 = (sender as GridView);
    GridView1.PageIndex = e.NewPageIndex;

    // select data from child table
    GridView1.DataSource = 
            GetData(string.Format("select * from ChildTable where FId='{0}'", FId));
    GridView1.DataBind();
}
.pagination-ys {
    //display: inline-block;
    padding-left: 0;
    margin: 20px 0;
    border-radius: 4px;
}

.pagination-ys table > tbody > tr > td {
    display: inline;
}

.pagination-ys table > tbody > tr > td > a,
.pagination-ys table > tbody > tr > td > span {
    position: relative;
    float: left;
    padding: 8px 12px;
    line-height: 1.42857143;
    text-decoration: none;
    color: #dd4814;
    background-color: #ffffff;
    border: 1px solid #dddddd;
    margin-left: -1px;
}

.pagination-ys table > tbody > tr > td > span {
    position: relative;
    float: left;
    padding: 8px 12px;
    line-height: 1.42857143;
    text-decoration: none;    
    margin-left: -1px;
    z-index: 2;
    color: #aea79f;
    background-color: #f5f5f5;
    border-color: #dddddd;
    cursor: default;
}

.pagination-ys table > tbody > tr > td:first-child > a,
.pagination-ys table > tbody > tr > td:first-child > span {
    margin-left: 0;
    border-bottom-left-radius: 4px;
    border-top-left-radius: 4px;
}

.pagination-ys table > tbody > tr > td:last-child > a,
.pagination-ys table > tbody > tr > td:last-child > span {
    border-bottom-right-radius: 4px;
    border-top-right-radius: 4px;
}

.pagination-ys table > tbody > tr > td > a:hover,
.pagination-ys table > tbody > tr > td > span:hover,
.pagination-ys table > tbody > tr > td > a:focus,
.pagination-ys table > tbody > tr > td > span:focus {
    color: #97310e;
    background-color: #eeeeee;
    border-color: #dddddd;
}

请看一个快速示例:

HTML标记:

<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
    <ItemTemplate>

        <asp:Label ID="lblFID" runat="server" Text='<%# Eval("FID") %>'></asp:Label>
        <!-- Your other controls -->
        <asp:GridView ID="GridView1" runat="server" AllowPaging="true" PageSize="1"
        AutoGenerateColumns="false" OnPageIndexChanging="GridView1_PageIndexChanging" >
            <Columns>
                <asp:TemplateField>
                <ItemTemplate>

               <asp:Label ID="lblFID" runat="server" Text='<%# Eval("FID") %>'></asp:Label>
               <!-- Your other controls -->

                </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <PagerStyle HorizontalAlign="Right" CssClass="pagination-ys" />
        </asp:GridView>

    </ItemTemplate>
</asp:Repeater>
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || 
             e.Item.ItemType == ListItemType.AlternatingItem)
    {
        string FId = (e.Item.FindControl("FID") as Label).Text;
        GridView GridView1 = e.Item.FindControl("GridView1") as GridView;

        // select data from parent Table
        GridView1.DataSource = 
                GetData(string.Format("select * from ParentTable where FId='{0}'", FId));
        GridView1.DataBind();
    }
}

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    // get repeater item where clicked
    RepeaterItem item = ((GridView)sender).Parent as RepeaterItem;
    string FId = ((Label)Repeater1.Items[item.ItemIndex].FindControl("FID")).Text;

    // get gridview from Repeater
    GridView GridView1 = (sender as GridView);
    GridView1.PageIndex = e.NewPageIndex;

    // select data from child table
    GridView1.DataSource = 
            GetData(string.Format("select * from ChildTable where FId='{0}'", FId));
    GridView1.DataBind();
}
.pagination-ys {
    //display: inline-block;
    padding-left: 0;
    margin: 20px 0;
    border-radius: 4px;
}

.pagination-ys table > tbody > tr > td {
    display: inline;
}

.pagination-ys table > tbody > tr > td > a,
.pagination-ys table > tbody > tr > td > span {
    position: relative;
    float: left;
    padding: 8px 12px;
    line-height: 1.42857143;
    text-decoration: none;
    color: #dd4814;
    background-color: #ffffff;
    border: 1px solid #dddddd;
    margin-left: -1px;
}

.pagination-ys table > tbody > tr > td > span {
    position: relative;
    float: left;
    padding: 8px 12px;
    line-height: 1.42857143;
    text-decoration: none;    
    margin-left: -1px;
    z-index: 2;
    color: #aea79f;
    background-color: #f5f5f5;
    border-color: #dddddd;
    cursor: default;
}

.pagination-ys table > tbody > tr > td:first-child > a,
.pagination-ys table > tbody > tr > td:first-child > span {
    margin-left: 0;
    border-bottom-left-radius: 4px;
    border-top-left-radius: 4px;
}

.pagination-ys table > tbody > tr > td:last-child > a,
.pagination-ys table > tbody > tr > td:last-child > span {
    border-bottom-right-radius: 4px;
    border-top-right-radius: 4px;
}

.pagination-ys table > tbody > tr > td > a:hover,
.pagination-ys table > tbody > tr > td > span:hover,
.pagination-ys table > tbody > tr > td > a:focus,
.pagination-ys table > tbody > tr > td > span:focus {
    color: #97310e;
    background-color: #eeeeee;
    border-color: #dddddd;
}
分页样式:

<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
    <ItemTemplate>

        <asp:Label ID="lblFID" runat="server" Text='<%# Eval("FID") %>'></asp:Label>
        <!-- Your other controls -->
        <asp:GridView ID="GridView1" runat="server" AllowPaging="true" PageSize="1"
        AutoGenerateColumns="false" OnPageIndexChanging="GridView1_PageIndexChanging" >
            <Columns>
                <asp:TemplateField>
                <ItemTemplate>

               <asp:Label ID="lblFID" runat="server" Text='<%# Eval("FID") %>'></asp:Label>
               <!-- Your other controls -->

                </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <PagerStyle HorizontalAlign="Right" CssClass="pagination-ys" />
        </asp:GridView>

    </ItemTemplate>
</asp:Repeater>
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || 
             e.Item.ItemType == ListItemType.AlternatingItem)
    {
        string FId = (e.Item.FindControl("FID") as Label).Text;
        GridView GridView1 = e.Item.FindControl("GridView1") as GridView;

        // select data from parent Table
        GridView1.DataSource = 
                GetData(string.Format("select * from ParentTable where FId='{0}'", FId));
        GridView1.DataBind();
    }
}

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    // get repeater item where clicked
    RepeaterItem item = ((GridView)sender).Parent as RepeaterItem;
    string FId = ((Label)Repeater1.Items[item.ItemIndex].FindControl("FID")).Text;

    // get gridview from Repeater
    GridView GridView1 = (sender as GridView);
    GridView1.PageIndex = e.NewPageIndex;

    // select data from child table
    GridView1.DataSource = 
            GetData(string.Format("select * from ChildTable where FId='{0}'", FId));
    GridView1.DataBind();
}
.pagination-ys {
    //display: inline-block;
    padding-left: 0;
    margin: 20px 0;
    border-radius: 4px;
}

.pagination-ys table > tbody > tr > td {
    display: inline;
}

.pagination-ys table > tbody > tr > td > a,
.pagination-ys table > tbody > tr > td > span {
    position: relative;
    float: left;
    padding: 8px 12px;
    line-height: 1.42857143;
    text-decoration: none;
    color: #dd4814;
    background-color: #ffffff;
    border: 1px solid #dddddd;
    margin-left: -1px;
}

.pagination-ys table > tbody > tr > td > span {
    position: relative;
    float: left;
    padding: 8px 12px;
    line-height: 1.42857143;
    text-decoration: none;    
    margin-left: -1px;
    z-index: 2;
    color: #aea79f;
    background-color: #f5f5f5;
    border-color: #dddddd;
    cursor: default;
}

.pagination-ys table > tbody > tr > td:first-child > a,
.pagination-ys table > tbody > tr > td:first-child > span {
    margin-left: 0;
    border-bottom-left-radius: 4px;
    border-top-left-radius: 4px;
}

.pagination-ys table > tbody > tr > td:last-child > a,
.pagination-ys table > tbody > tr > td:last-child > span {
    border-bottom-right-radius: 4px;
    border-top-right-radius: 4px;
}

.pagination-ys table > tbody > tr > td > a:hover,
.pagination-ys table > tbody > tr > td > span:hover,
.pagination-ys table > tbody > tr > td > a:focus,
.pagination-ys table > tbody > tr > td > span:focus {
    color: #97310e;
    background-color: #eeeeee;
    border-color: #dddddd;
}

@阿西夫,它就像一个魅力!!!谢谢你的救命恩人。但是我可以用下一个和上一个导航代替页码吗?很抱歉打扰你,你可以点击这个链接@Asif,但我现在检查了,它显示了一些记录,而没有显示一些记录(子记录-Gridview)。就是这样,它在每个记录之后都会跳过,例如记录1显示,记录2不显示,但记录3显示,记录4不显示等等。。。你能给我最后一个帮助吗?请尝试很久。@Asif我在这里弄明白了:if(e.Item.ItemType==ListItemType.Item | | | e.Item.ItemType==ListItemType.AlternatingItem)那么如果(e.Item.ItemType==ListItemType.AlternatingItem)这个
发生了什么事。它解决了吗?@Asif,它就像一个符咒!!!谢谢你的救命恩人。但是我可以用下一个和上一个导航代替页码吗?很抱歉打扰你,你可以点击这个链接@Asif,但我现在检查了,它显示了一些记录,而没有显示一些记录(子记录-Gridview)。就是这样,它在每个记录之后都会跳过,例如记录1显示,记录2不显示,但记录3显示,记录4不显示等等。。。你能给我最后一个帮助吗?请尝试很久。@Asif我在这里弄明白了:if(e.Item.ItemType==ListItemType.Item | | | e.Item.ItemType==ListItemType.AlternatingItem)那么如果(e.Item.ItemType==ListItemType.AlternatingItem)这个
发生了什么事。问题解决了吗?