【问题标题】:Add sorting to a Nested Gridview ASP.net with C#使用 C# 将排序添加到嵌套的 Gridview ASP.net
【发布时间】:2013-03-05 10:38:26
【问题描述】:

我已经到处搜索了如何在我的页面上实现这一点。我有一个顶级gridview,它允许排序和嵌套gridviews,它们在页面编译时动态生成,所以'x'个嵌套gridviews位于动态html div中,可以在用户命令中从不可见切换到可见。我遇到的问题是,我无法弄清楚如何在不折叠 div/导致回发的情况下允许对这些嵌套的网格视图进行排序。

下面展示了在asp.net中如何生成主gridview(gvSalesDiv)和嵌套gridview(gvTheDivisionCustomers)

    <asp:GridView ID="gvSalesDiv" AllowSorting="true" onsorting="GridView1_Sorting" runat="server" GridLines="Both" OnRowDataBound="gvOrderLineDetail_RowDataBound" AutoGenerateColumns="False"
        Width="100%" Height="210px" BackColor="WhiteSmoke" AlternatingRowStyle-BackColor="#DADDE2"
        HeaderStyle-Font-Size="Medium"  Visible="true">
        <Columns>
        <asp:TemplateField HeaderText="Toggle Detail">
                    <ItemTemplate>
                        <a href="javascript:switchViews('div<%# Eval("SalesDivision") %>');">

                        <img id="imgdiv<%# Eval("SalesDivision") %>" alt="toggle"  border="0"
                         src="/salesconsole/toggle-off.png" />
                        </a>                      

                    </ItemTemplate>
                    </asp:TemplateField>

        <asp:BoundField DataField="SalesDivision"  HeaderText="Sales Division">

                <ItemStyle  Font-Bold="True" ForeColor="CornflowerBlue" HorizontalAlign="Center"></ItemStyle>
                </asp:BoundField>
                <asp:BoundField DataField="LastDay" SortExpression="LastDay" DataFormatString="{0:C}" HeaderText="Last 24 Hours" >


                </asp:BoundField>
                <asp:BoundField DataField="LastWeek" SortExpression="LastWeek" DataFormatString="{0:C}" HeaderText="Last 7 Days" >

                </asp:BoundField>
                <asp:BoundField DataField="Last30Days" SortExpression="Last30Days" DataFormatString="{0:C}" HeaderText="Last 30 Days" >

                </asp:BoundField>
                <asp:BoundField DataField="Last3Months" SortExpression="Last3Months" DataFormatString="{0:C}" HeaderText="Last 3 Months" >

                </asp:BoundField>
                <asp:BoundField DataField="Last6Months" SortExpression="Last3Months" DataFormatString="{0:C}" HeaderText="Last 6 Months" >

                </asp:BoundField>
                <asp:BoundField DataField="LastYear" SortExpression="LastYear" DataFormatString="{0:C}" HeaderText="Last Year" >

                </asp:BoundField>
                <asp:TemplateField>
                    <ItemTemplate>
                                 <tr>
                                 <td colspan="100">     
                             <div id="div<%# Eval("SalesDivision") %>" style="display:none;position:relative;left:25px;" >

         <h3 title="<%# Eval("SalesDivision") %> Sales"><%# Eval("SalesDivision") %> Sales Breakdown</h3>

        <asp:GridView ID="gvTheDivisionCustomers" AllowSorting="true" onsorting="GridView2_Sorting" BackColor="WhiteSmoke" AlternatingRowStyle-BackColor="#DADDE2"
          Width="100%" 
        AutoGenerateColumns="false" runat="server">
        <Columns>
        <asp:TemplateField HeaderText="Show More Detail">
                    <ItemTemplate>
                       <a href="sales-customers-detail.aspx?CustomerID=<%# Eval("CustomerID") %>&CustomerName=<%# Eval("CustomerName") %>" target="_blank" style="color:Blue; text-decoration:underline"> More Details
                        </a>                      

                    </ItemTemplate>
                    </asp:TemplateField>

        <asp:BoundField DataField="CustomerID" HeaderText="ID"/>
        <asp:BoundField DataField="CustomerName" HeaderText="Name"  />
        <asp:BoundField DataField="Last24Hours" HeaderText="Last 24 Hours" SortExpression="LastDay" DataFormatString="{0:C}"  />
        <asp:BoundField DataField="Last7Days" HeaderText="Last 7 Days" SortExpression="Last7Days" DataFormatString="{0:C}"  />
       <asp:BoundField DataField="Last30Days" HeaderText="Last 30 Days" SortExpression="Last30Days" DataFormatString="{0:C}"  />
       <asp:BoundField DataField="Last3Months" HeaderText="Last 3 Months" SortExpression="Last3Months" DataFormatString="{0:C}"  />
       <asp:BoundField DataField="Last6Months" HeaderText="Last 6 Months" SortExpression="Last6Months" DataFormatString="{0:C}"  />
       <asp:BoundField DataField="LastYear" SortExpression="LastYear" HeaderText="Last Year" DataFormatString="{0:C}" />

       </Columns>


        </asp:GridView>

                               </div>    
                                 </td></tr>    
                              </ItemTemplate>

                                            </asp:TemplateField>  
        </Columns>
        </asp:GridView>

我在 Page_Load 上填充主网格视图,并使用 OnRowDataBound 方法创建嵌套的网格视图。我有一个用于主网格视图的排序方法,它也可以正常工作。下面是嵌套网格视图的 OnSorting 方法,这是我卡住的地方...我无法访问这个对象

    protected void GridView2_Sorting(Object sender, GridViewSortEventArgs e)
{
    // TO DO : Sort the nested gridview....All I can get at is the sort expressions or
    cast the sender into a gridview but even then I wouldn't know the correct SQL query to bind with unless I knew which 'div' I was in... 

}

【问题讨论】:

    标签: c# asp.net visual-studio sorting gridview


    【解决方案1】:

    所以基本上我们可以说,您正在寻找一个 ID 或可以用来为排序方法创建查询的东西? 如果这是正确的,我们可以找到解决方案。 在第一个 gridviews(gvSalesDiv) ItemTemplate 中放置一个新标签,如下所示:

     <asp:Label ID="lblID" runat="server" Text='<%# Bind("Id") %>'></asp:Label>
    

    您可以在代码隐藏中找到它:

    Label lblID = (Label)((GridView)sender).NamingContainer.FindControl("lblID");
    

    希望它有效!

    【讨论】:

    • 谢谢你,我可以看到它是如何工作的,下一个问题是这会导致页面状态丢失,即。当我单击以对内部网格视图进行排序时,Div 将折叠,因为外部网格视图正在“重新绑定”。你知道我如何保持 div 的状态吗?
    • 也许您可以尝试使用 Ajax UpdatePanel 控件来强制 gridview 重新加载而无需整页回发。
    【解决方案2】:

    如果你正在寻找如何获取 gridview 所在的父 div,你可以使用这个:

    首先,您需要将sender 转换为gridview,然后创建一个html 元素并将其转换为gridview 的父级。像这样:

        Dim test As Button = CType(sender, Button)
        Dim div As HtmlGenericControl
        div = CType(test.Parent, HtmlGenericControl)
        Dim t As String = test.ID
    

    在本例中,我将发件人投射到一个按钮,但您可以轻松更改它。在这个例子中,你需要让 div 在服务器上运行,使用 "runat="server""。如果您不想在服务器端创建它,您可以更改从 HTMLGenericControl 转换为 ContentPlaceHolder 的方式。如果这有帮助或者您需要更多信息,请告诉我。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-20
      • 2011-03-01
      • 1970-01-01
      • 2011-09-02
      相关资源
      最近更新 更多