gpt4 book ai didi

c# - 使用 C# 将排序添加到嵌套的 Gridview ASP.net

转载 作者:行者123 更新时间:2023-11-30 17:06:45 24 4
gpt4 key购买 nike

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

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

    <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 上填充了主 GridView ,嵌套的 GridView 是使用 OnRowDataBound 方法创建的。我有一个主 gridview 的排序方法,它也可以正常工作。下面是嵌套 gridview 的 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...

}

最佳答案

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

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

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

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

希望它有用!

关于c# - 使用 C# 将排序添加到嵌套的 Gridview ASP.net,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15221643/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com