【问题标题】:How to give a navigation to nested child gridview inside a repeater如何为中继器内的嵌套子网格视图提供导航
【发布时间】:2017-08-23 21:11:15
【问题描述】:

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

现在我可以分别使用它们的FId 从两个表中检索外部中继器和子网格视图中的数据。但我想知道是否有可能在子网格视图中提供导航而不是一次显示所有记录,所以实际上我希望我的子转发器只显示一条记录并访问我想使用的其他记录下一个和上一个导航。请指导我,下面是代码:

    <asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="OnItemDataBound">
        <ItemTemplate>
            <u><b>
                <asp:Label ID="Label7" class="HeadMasterTextBox" Width="" runat="server" Text='<%# Eval("NTitle") %>'></asp:Label>
            </b></u>
            <asp:Label ID="lblDetails" class="HeadMasterTextBox" Width="" runat="server" Text='<%# Eval("NDetails") %>'></asp:Label>
            <asp:Panel ID="pnlOrders" runat="server" Style="display: block;">
                GridView ID="gvNewChild" runat="server" DataKeyNames="FId" AutoGenerateColumns="false"
                                               CssClass="tableWall" HeaderStyle-BorderColor="#ffffff" GridLines="None" Visible="true"
                                               AllowPaging="true"  PageSize="1" OnPageIndexChanging="gvNewChild_PageIndexChanging" >
                                               <Columns>
                                                   <asp:TemplateField>
                                                       <ItemTemplate>
                                                           <table border="0" width="100%" align="center" class="">
                                                               <tr>
                                                                   <td align="center" colspan="2">

                                                                     <!--  <asp:ImageButton runat="server" ID="imgData" class="imgwall" ImageUrl='<%# Eval("ImageName") %>'
                                                                           CommandName='<%# Eval("FId") %>' CommandArgument='<%# Eval("ImageName") %>' />
                                                                           -->

                                                                           <asp:Image ID="Image1" runat="server" ImageUrl='<%# "data:image/jpg;base64," + Convert.ToBase64String((byte[])Eval("BData")) %>'
                                                    Height="180px" Width="200px" />

                                                    <br />
                                                    <asp:Label ID="Label1" runat="server" Visible="true" Text='<%# Eval("ImageName") %>'></asp:Label>

                                                                   </td>
                                                               </tr>
                                                               <tr>
                                                                   <td style="vertical-align: top;" align="center">
                                                                       <asp:Label ID="lblFId" runat="server" Visible="false" Text='<%# Eval("FId") %>'></asp:Label>
                                                                       <asp:TextBox ID="txt_GFId" AutoPostBack="true" runat="server" Visible="true" Text='<%# Eval("FId") %>'></asp:TextBox>
                                                                       <br />
                                                                       <asp:LinkButton Text="" ID="lnkFIdCount" class="tableAdminTextBox" CommandArgument='<%#  Eval("FId") %>'
                                                                           runat="server" CommandName="Select" />
                                                                   </td>
                                                               </tr>
                                                           </table>

                                                       </ItemTemplate>
                                                   </asp:TemplateField>
                                               </Columns>
                                               <RowStyle BackColor="#ffffff" ForeColor="#333333" />
                                           </asp:GridView>
 <asp:HiddenField ID="hfFId" runat="server" Value='<%# Eval("FId") %>' />
                                    </asp:Panel>
                               </ItemTemplate>
                            </asp:Repeater>

以下是出处:

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();

    }

【问题讨论】:

    标签: c# asp.net gridview nested repeater


    【解决方案1】:

    看一下快速示例:

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

    【讨论】:

    • @Asif,它就像一个魅力!感谢一吨救生员。但是我也可以使用下一个和上一个导航而不是页码吗?
    • 你可以关注这个链接msdn.microsoft.com/en-us/library/…
    • @Asif 很抱歉打扰你,但我现在实际上检查了它显示一些记录而不显示一些记录(子记录 - Gridview)。所以它是这样的,它在每条记录之后都跳过,例如记录 1 正在显示,记录 2 没有显示,但记录 3 正在显示,而记录 4 没有显示等等......你能给我最后的帮助吗请尝试很久。
    • @Asif 我在这里想通了:if (e.Item.ItemType == ListItemType.Item ||e.Item.ItemType == ListItemType.AlternatingItem)
    • 那么if (e.Item.ItemType == ListItemType.Item ||e.Item.ItemType == ListItemType.AlternatingItem) 发生了什么。解决了吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多