2009-07-22 49 views
0

我有一個父GridView有一個子GridView(代碼如下),我如何獲得子Gridview複選框的值?另外,如何保存子網格視圖的狀態,即如果顯示或不顯示?這是出版物已經選擇當按下按鈕觸發功能,通過父格眼看寫着:GridView,訪問子GridView複選框

protected void DeleteSelectedProducts_Click(object sender, EventArgs e) 
    { 
     bool atLeastOneRowDeleted = false; 

     // Iterate through the Products.Rows property 
     foreach (GridViewRow row in GridView1.Rows) 
     { 
      // Access the CheckBox 
      CheckBox cb = (CheckBox)row.FindControl("PublicationSelector"); 
      if (cb != null && cb.Checked) 
      { 
       // Delete row! (Well, not really...) 
       atLeastOneRowDeleted = true; 

       // First, get the ProductID for the selected row 
       int productID = 
        Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value); 

       // "Delete" the row 
       DeleteResults.Text += string.Format(
        "This would have deleted ProductID {0}<br />", productID); 
       //DeleteResults.Text = "something"; 
      } 


      // Show the Label if at least one row was deleted... 
      DeleteResults.Visible = atLeastOneRowDeleted; 
     } 
    } 

     <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" 
     AutoGenerateColumns="False" DataKeyNames="PublicationID" 
     DataSourceID="ObjectDataSource1" Width="467px" OnRowDataBound="GridView1_RowDataBound" 
     Font-Names="Verdana" Font-Size="Small"> 
     <Columns> 
      <asp:TemplateField> 
       <ItemTemplate> 
        <asp:CheckBox ID="PublicationSelector" runat="server" /> 
       </ItemTemplate> 
      </asp:TemplateField> 
      <asp:BoundField DataField="NameAbbrev" HeaderText="Publication Name" SortExpression="NameAbbrev" /> 
      <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" /> 
      <asp:BoundField DataField="State" HeaderText="State" SortExpression="State" /> 
      <asp:TemplateField HeaderText="Owners"> 
       <ItemTemplate> 
        <asp:Label ID="Owners" runat="server"></asp:Label> 
       </ItemTemplate> 
       <ItemStyle HorizontalAlign="Center" /> 
      </asp:TemplateField> 
      <asp:BoundField DataField="Type" HeaderText="Type" SortExpression="Type" /> 
      <asp:TemplateField HeaderStyle-CssClass="hidden-column" ItemStyle-CssClass="hidden-column" FooterStyle-CssClass="hidden-column"> 
       <ItemTemplate> 
        <tr> 
         <td colspan="8" > 
          <div id="<%# Eval("PublicationID") %>" style="display: none; position: relative;" > 
           <asp:GridView ID="GridView2_ABPubs" runat="server" AutoGenerateColumns="false" Width="100%" 
            DataKeyNames="PublicationID" Font-Names="Verdana" Font-Size="small"> 
            <Columns> 
             <asp:TemplateField> 
              <ItemTemplate> 
               <asp:CheckBox ID="ChildPublicationSelector" runat="server" /> 
              </ItemTemplate> 
             </asp:TemplateField> 
             <asp:BoundField DataField="NameAbbrev" HeaderText="Publication Name" SortExpression="NameAbbrev" /> 
            </Columns> 
           </asp:GridView> 
          </div> 
         </td> 
        </tr> 
       </ItemTemplate> 
      </asp:TemplateField> 
     </Columns> 
    </asp:GridView> 
    <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetData" 
     TypeName="shoom_dev._Default"> 
    </asp:ObjectDataSource> 

    <p> 
     <asp:Button ID="DeleteSelectedProducts" runat="server" 
      Text="Delete Selected Products" onclick="DeleteSelectedProducts_Click" /> 
    </p> 

    <p> 
     <asp:Label ID="DeleteResults" runat="server" EnableViewState="False" Visible="False"></asp:Label> 
    </p> 

回答

1

你有相同row.FindControl()方法的複選框的GridView2_ABPubs控制。這應該給你gridview,然後你可以做一個查找控制。

但是,剛剛花了三天的時間凝視和定製一個GridView,您最後一個模板列與子網格視圖不需要和節點,因爲這些將由GridView控件自動添加,這可能會使得更棘手找到孩子的控制權。

我也發現的FindControl看上去並不很遠了棧,所以我創建了一個擴展方法遞歸查清控制:

public static T FindControl<T>(this Control parent, string controlName) where T: Control 
{ 
    T found = parent.FindControl(controlName) as T; 
    if (found != null) 
     return found; 

    foreach(Control childControl in parent.Controls) 
    { 
     found = childControl.FindControl<T>(controlName) as T; 
     if (found != null) 
      break; 
    } 

    return found; 
} 
+0

謝謝,我會放棄它。但是,我不明白這一行「...但是,剛剛花了三天的時間凝視和定製一個GridView,您最後一個模板列與子網格視圖不需要和節點,因爲這些將自動添加GridView控件,這可能會讓它更難找到子控件。「 我可以刪除哪個模板? 3天,這件事情讓我瘋狂! 感謝您的幫助,R. – flavour404 2009-07-22 20:49:31

+0

您需要該模板。您不需要在最後一個TemplateField的項目模板佈局中包含TR和TD節點 – 2009-07-22 20:52:49

0

大衛,這是正確的:

using System; 
using System.Collections.Generic; 
using System.Web; 
using System.Web.UI; 

public static class FindControl<T> 
{public static T FindControl<T>(this Control parent, string controlName) where T : Control 
    { 
    T found = parent.FindControl(controlName) as T; 
    if (found != null) 
     return found; 
    foreach (Control childControl in parent.Controls) 
    { found = childControl.FindControl(controlName) as T; 
     if (found != null) 
      break; 
    } 
    return found; } 

}

該類保存爲FindControl.cs。我如何調用這個函數?

我感覺有點遲鈍,但在我的防守中,這是我第一次使用extendor類。

嗯,我剛剛得到了非通用方法必須在一個靜態類,所以我猜測我有一些錯誤定義錯誤消息...哈哈

感謝。