【问题标题】:Using FindControl to get GridView in a Content Page使用 FindControl 在内容页面中获取 GridView
【发布时间】:2011-09-25 10:20:08
【问题描述】:

我想在一个单独的类中找到一个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>

.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;
}

我尝试了几种使用 MainContent_GridView 进行查找的变体,并使用 Master.FindControl 获得所有相同的结果。

【问题讨论】:

    标签: asp.net gridview master-pages findcontrol


    【解决方案1】:

    在您的一个 cmets 中,您声明 GridView 不在母版页上,那么假设它在使用母版页的页面上是否安全?因此它必须在ContentPlaceholder 控件中?

    关键问题是FindControl方法only looks for direct children(强调添加):

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

    所以你要么需要:

    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;
    }
    

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

    var gridView = FindControlRecursively(Page, "GridView1") as GridView
    
    if (null != gridView) {
      // Do Stuff
    }
    

    【讨论】:

    • 好的,我会试一试本。谢谢你。我提到 MasterPage 的原因是因为我之前遇到过问题,如果涉及 MasterPage(即使控件不在 site.master 内),您需要查找的内容也会发生变化(即 MainContent_controID)再次感谢我'将发布它的进展情况
    • 感谢大家的想法和回答。代码 public static string GetControlName() { string controlName = "";控制控制=空;尝试 { System.Web.UI.Page currentPage = (System.Web.UI.Page)System.Web.HttpContext.Current.Handler; control = currentPage.Master.FindControl("MainContent").FindControl("GridView1"); controlName = control.ClientID; } catch (Exception ex) { } return controlName; }
    【解决方案2】:

    如果您已经在页面中,请不要从 HttpContext 获取页面。相反,是否有可以从中使用 FindControl 的控件?而不是使用页面,使用:

    parentControl.FindControl("GridView1") as GridView;
    

    相反。从页面级别查找网格存在问题,使用更接近网格的较低级别控件将获得更好的成功。

    【讨论】:

    • @HTH 我猜你的意思是像这个 Grid GridView grid = (GridView)this.Master.FindControl("GridView1");这没有用。你确定你必须为它设置一个接口吗?似乎有点超出了我们的需要
    • 另外,不确定我是否提到了这一点,但网格不是母版页的一部分,但我知道使用站点管理员时必须以不同的方式访问这些内容
    • 哦,那会改变一切。我以为它在母版页中。
    • Brian- 如果 gridview 在顶部,父控件是什么?我必须把它放在一个面板中才能工作吗?
    • 当然,为了测试目的,尝试用 包装它并尝试使用 P1.FindControl("GridView1") 看看是否有效.
    【解决方案3】:

    Brian 说对了,但他忘记了最重要的部分。 除非您将此代码添加到要使用它的文件的 HTML 代码之上,否则您将无法使用他的代码。(Page.aspx)

    <%@ MasterType VirtualPath="~/Master/Site.master" %>
    

    那么您可以使用 Brian 建议的代码:

    GridView grid = this.Master.FindControl("GridView1");
    

    编辑: 如果您想在同一文件的另一个类中使用 gridview,我将使用以下内容: 将此添加到创建页面时创建的类中

     public partial class YourPageName: System.Web.UI.Page
     {
        public static Gridview mygrid = this.GridviewnameOFYourASPX
        ...
     }
    

    然后在你的方法中添加这个到你的自定义类

    YourPageName.mygrid.(The changes you want to make);
    

    【讨论】:

    • 我一定在这里遗漏了一些东西。 GridView grid = this.Master.FindControl("GridView1");产生错误错误 2 无法将类型“System.Web.UI.Control”隐式转换为“System.Web.UI.WebControls.GridView”。存在显式转换我不确定上面的 MasterType 代码将如何解决这个问题?另外,我尝试在其中添加 MasterType,但仍然无法正常工作。我有一个存在于 Newpage.aspx 中的 GridView 控件,但有一个 Site.Master 页面。我想从同一个项目中的另一个通用类文件中访问这个 GridView。这可以做到吗?如果是这样的话。
    • FindControl 返回一个控件,不能将其分配给 GridView 变量。这就是错误。但是,如果您没有在母版页中找到控件,则不能使用 Master 属性。
    • 这是你想要的吗?布赖恩:你可以正常地做 ((GridView)this.Master.FindControl("GridView1")).Something();
    • 我希望使用位于同一个项目中但不是同一个文件的类来访问控件的类。它是单个 .cs 文件,而不是 aspx.cs。这仍然有效吗?此外,网格不在 site.master 文件中。我只是提到我有一个主文件......如果它不在母版页中,我还需要做一个 .master 吗?
    • @Beejee,我的意思是,他在我的帖子的评论中说,该控件不在母版页中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-12
    • 2017-07-26
    • 2012-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多