2013-03-05 37 views
3

我搜索了高和低如何實現這一點在我的網頁上。我有一個頂級的gridview,它允許排序和嵌套的網格視圖,當頁面編譯'x'嵌套的網格視圖時,動態生成的動態html div可以在用戶命令中從不可見變爲可見。我遇到的問題是,我無法弄清楚如何在不折疊div的情況下對這些嵌套的gridviews進行排序/導致回發。添加排序到一個嵌套的Gridview ASP.net與C#

下面示出了如何在主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和使用OnRowDataBound方法創建嵌套GridView的。我有一個主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... 

} 

回答

0

如果你正在尋找如何獲取父div的GridView控件駐留在,你可以使用這個:

首先,你需要將發送者轉換爲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 

在這個例子中,我將發件人強制轉換爲按鈕,但您可以輕鬆更改此設置。在這個例子中,您將需要使用「runat =」server「」在服務器上運行div。如果您不想在服務器端創建它,則可以更改從HTMLGenericControl投射到ContentPlaceHolder的方式。讓我知道這是否有幫助或您需要更多信息。

2

所以基本上我們可以說,你正在尋找一個ID或什麼東西,你可以創建查詢的排序方法? 如果這是正確的,我們可以找到解決方案。 把第一GridView的內部新的標籤(gvSalesDiv)的ItemTemplate像這樣的:

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

而且在codebehinde您可以在此找到它:

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

希望工程!

+0

謝謝你,我可以看到這將如何工作,接下來的問題是,這會導致頁面狀態丟失,即。當我點擊排序內部網格視圖時,Div會崩潰,因爲外部網格視圖是'重新綁定'。你有什麼想法,我怎麼能保持divs的狀態? – AndyBryce 2013-03-05 12:30:04

+0

也許你可以嘗試使用Ajax UpdatePanel控件來強制gridview在沒有完整頁面回發的情況下重新加載。 – Cerbi 2013-03-05 12:41:40