使用 FindControl 在内容页面中获取 GridView

gco*_*828 2 asp.net gridview master-pages findcontrol

我想GridView在一个单独的类中找到一个控件,但我在这样做时遇到了问题。我什至尝试将我的代码放在 aspx.cs 页面中,但无济于事。我不断收到未将对象引用设置为对象实例的情况。我确定我遗漏了一个简单的步骤,但在我的研究中我似乎找不到任何东西。

Aspx代码

  <asp:GridView ID="GridView1" EnableViewState="true" 
    runat="server"  
    BackColor="White" BorderColor="#CC9966"
    BorderStyle="None" BorderWidth="1px" CellPadding="4" Width="933px" 
    onrowdatabound="GridView1_RowDataBound"  
    onrowdeleting="GridView1_RowDeleting" 
    onrowediting="GridView1_RowEditing"
    onrowupdating="GridView1_RowUpdating" 
    onsorting="GridView1_Sorting"
    AllowSorting="true"
    AutoGenerateColumns="False" 
    PersistedSelection="true" onrowcancelingedit="GridView1_RowCancelingEdit">
    <EditRowStyle Font-Size="Small" Width="100px" />
    <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
    <Columns>
      <asp:TemplateField>
        <ItemTemplate>
          <asp:LinkButton runat="server" ID="EditLinkButton" CausesValidation="True"
                          Text="Edit" CommandName="Edit"/>
          <asp:LinkButton runat="server" ID="DeleteLinkButton" CausesValidation="False"
                          Text="Delete" CommandName="Delete"/>
        </ItemTemplate>
        <EditItemTemplate>
          <asp:LinkButton runat="server" ID="UpdateLinkButton" CausesValidation="True"
                          Text="Update" CommandName="Update"/>
          <asp:LinkButton runat="server" ID="CancelLinkButton" CausesValidation="False"
                          Text="Cancel" CommandName="Cancel"/>
        </EditItemTemplate>
      </asp:TemplateField>
    </Columns>
  </asp:GridView>
Run Code Online (Sandbox Code Playgroud)

.cs 代码

protected void Page_Load(object sender, EventArgs e) {
  SetDirectory();

  System.Web.UI.Page page = (System.Web.UI.Page)System.Web.HttpContext.Current.Handler;
  GridView GridViewCopy = (GridView)page.FindControl("GridView1");

  Log.WriteLine("SortBindGrid: GridView Row Count: " + 
                GridViewCopy.Rows.Count, Log.DEBUG_LEVEL.TERSE);
  return;
}
Run Code Online (Sandbox Code Playgroud)

我已经尝试了一些使用 MainContent_GridView 来获取和 Master.FindControl 的变体,结果都是一样的。

Zha*_*uid 5

在您的评论中,您指出GridView不在母版页上,那么假设它在使用母版页的页面上是否安全?因此它必须在ContentPlaceholder控制中?

关键问题是该FindControl方法只查找直接子项(强调):

只有在指定容器直接包含控件时,此方法才会查找控件;也就是说,该方法不会在控件内的控件层次结构中进行搜索

所以你要么需要:

  1. 在正确的ContentPlaceholder控件中搜索控件,而不是从Page.
  2. Page.Controls递归方式遍历每个控件,直到找到所需的控件。

2的例子:

private Control FindControlRecursive(Control rootControl, string controlID)
{
    if (rootControl.ID == controlID) return rootControl;

    foreach (Control controlToSearch in rootControl.Controls)
    {
        Control controlToReturn = 
            FindControlRecursive(controlToSearch, controlID);
        if (controlToReturn != null) return controlToReturn;
    }
    return null;
}
Run Code Online (Sandbox Code Playgroud)

获得控制权后,您应该使用它进行转换as,然后检查是否为 null,以防万一它不是您所期望的:

var gridView = FindControlRecursively(Page, "GridView1") as GridView

if (null != gridView) {
  // Do Stuff
}
Run Code Online (Sandbox Code Playgroud)