2012-08-25 35 views
1

我正在使用嵌套的GridViews,其中gridview中的每一行都具有子gridView。我正在使用父級GridView的RowDataBound事件綁定Child GridView。我的問題是,如何在父級gridViews RowDataBound事件中獲取Child GridView的Button findcontrol值。如何讓按鈕在嵌套網格中找到控件值使用c#

這是我的aspx

  <asp:GridView ID="grdSubClaimOuter" SkinID="GridView" runat="server" Width="100%" 
        AutoGenerateColumns="false" OnRowDataBound="grdSubClaimOuter_RowDataBound" OnRowCommand="grdSubClaimOuter_RowCommand" 
        ShowFooter="false" AllowPaging="true" OnPageIndexChanging="grdSubClaimOuter_PageIndexChanging"> 
        <%--<AlternatingRowStyle BackColor="ButtonFace" />--%> 
        <Columns> 
         <asp:TemplateField ItemStyle-Width="5%"> 
          <ItemTemplate> 
           <asp:HiddenField ID="hdnClaimNo" runat="server" Value='<%# Eval("ClaimNo") %>' /> 
           <asp:Image runat="server" ID="img1" ImageUrl="../images/Collapse_plus.png" /> 
          </ItemTemplate> 
          <ItemStyle Width="5%"></ItemStyle> 
         </asp:TemplateField> 
       <asp:GridView ID="grdSubClaim" runat="server" SkinID="GridView" CellPadding="4" Width="100%" 
            AutoGenerateColumns="false" ShowFooter="false" OnRowEditing="grdSubClaim_RowEditing" 
            OnRowCommand="grdSubClaim_RowCommand" OnRowDeleting="grdSubClaim_RowDeleted" 
            AllowPaging="false" > 
            <%--SkinID="GridView"--%> 
            <Columns> 
             <asp:TemplateField FooterStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left"> 
              <HeaderTemplate> 
               Sub Claim No 
              </HeaderTemplate> 
              <ItemTemplate> 
               <asp:Label ID="lblSubClaimNoValue" Width="" runat="server" Text='<%#Eval("SubClaimNo")%>'></asp:Label> 
              </ItemTemplate> 
              <FooterStyle HorizontalAlign="Left" /> 
             </asp:TemplateField> 
      <asp:Button ID="btnSubrogation" CssClass="groovybutton" runat="server" CommandName="Subrogation" 
                Text="Subrogation" CommandArgument='<%# Eval("ClaimNo") + "~" + Eval("SubClaimNo")%>' /> 
               <asp:Button ID="btnSalvage" CssClass="groovybutton" runat="server" CommandName="Salvage" 
                Text="Salvage" CommandArgument='<%# Eval("ClaimNo") + "~" + Eval("SubClaimNo")%>' /> 
              </ItemTemplate> 
             </asp:TemplateField> 
            </Columns> 
            <AlternatingRowStyle CssClass="" /> 
            <RowStyle CssClass="ob_gBody" /> 
            <HeaderStyle CssClass="gridHeader" /> 
           </asp:GridView> 
           <asp:Literal runat="server" ID="Literal2" Text="</td></tr>" /> 
          </ItemTemplate> 
         </asp:TemplateField> 
        </Columns> 
       </asp:GridView> 

頁這是我的aspx.cs文件

protected void grdSubClaimOuter_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 

    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     if (e.Row.Cells[1].Text.ToString() != "&nbsp;") 
     { 
      Literal ltrChild = (Literal)e.Row.FindControl("ltrChild"); 
      System.Web.UI.WebControls.Image img = (System.Web.UI.WebControls.Image)e.Row.Cells[0].FindControl("img1"); 

      ltrChild.Text = ltrChild.Text.Replace("trChildGrid", "trChildGrid" + e.Row.RowIndex.ToString()); 
      string strChildGrid = "trChildGrid" + e.Row.RowIndex.ToString(); 
      e.Row.Cells[0].Attributes.Add("OnClick", "OpenTable('" + strChildGrid + "','" + img.ClientID + "')"); 
      e.Row.Cells[0].RowSpan = 1; 
      System.Web.UI.WebControls.GridView gvChild = (System.Web.UI.WebControls.GridView)e.Row.FindControl("grdSubClaim"); 
      PolicyProcessor.DAL.Claim.ClaimSubClaim objDALClaimSubClaim = new PolicyProcessor.DAL.Claim.ClaimSubClaim(); 
      PolicyProcessor.BOL.Claim.ClaimSubClaim objInfoClaimSubClaim = new PolicyProcessor.BOL.Claim.ClaimSubClaim(); 
      HiddenField hdnClaimNo = (HiddenField)e.Row.FindControl("hdnClaimNo"); 

      if (hdnClaimNo.Value != "") 
      { 
       objInfoClaimSubClaim.ClaimNo = hdnClaimNo.Value; 
      } 
      else 
      { 
       objInfoClaimSubClaim.ClaimNo = "0"; 
      } 

      DataSet dsChild; 
      dsChild = objDALClaimSubClaim.ResultSet(objInfoClaimSubClaim, "SelectInnerGrid"); 
      if (dsChild.Tables[0].Rows.Count > 0) 
      { 


       Button btn = (Button)gvChild.FindControl("btnSalvage"); 

      //btn is null how to get text value in btn 

       btn.ForeColor = System.Drawing.Color.Red; 


       gvChild.DataSource = dsChild; 
       gvChild.DataBind(); 

      } 
      else 
      { 
       Helper.EmptyGrid(gvChild, dsChild.Tables[0]); 
      } 

     } 

    } 
} 

,如果有人知道,請幫我解決這個problem.thanks提前。

+0

問題不清楚?因爲父網格將具有n個位於網格內的基於子按鈕。你能進一步解釋一下嗎? – Nikshep

回答

5

首先讓孩子GridView控件的引用,然後使用的FindControl來獲取裏面的按鈕:

foreach (GridViewRow row in grdSubClaimOuter.Rows) 
{ 
if (row.RowType == DataControlRowType.DataRow) 
{ 
    GridView gvChild = (GridView) row.FindControl("grdSubClaim"); 
    // Then do the same method for Button control column 
    if (gvChild != null) 
    { 
     foreach (GridViewRow row in gvChild .Rows) 
     { 
      if (row.RowType == DataControlRowType.DataRow) 
      { 
       Button btn = (Button) row.FindControl("buttonID"); 
       if (btn != null) 
       { 
        // do your work 
       } 
      } 
     } 
    } 
} 
} 
+0

非常感謝你@Waqar Janjua。我用你的答案我得到結果謝謝 – Neeraj

+0

@Neeraj你是最受歡迎的 –

0

如果你想必將在父網格的數據值的事件,然後用任何一種方式

1)您也可以使用您的dsChild(您的數據集)並獲取您爲按鈕綁定的字段值。

2)綁定子網格後,從gvChild獲取按鈕的值。

無需循環進入父網格和子網格。

相關問題