Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6077053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:40:39+00:00 2026-05-23T10:40:39+00:00

I would like to find a GridView Control within a separate class and I

  • 0

I would like to find a GridView Control within a separate class and I am having issues doing so. I even tried placing my code in the aspx.cs page to no avail. I keep getting Object reference not set to an instance of an object. I’m sure there is a simple step I’m missing, but in my research I cannot seem to find anything.

Aspx code

  <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 code

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

I’ve tried a few variations of using MainContent_GridView for the find to get and Master.FindControl with all the same result.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T10:40:40+00:00Added an answer on May 23, 2026 at 10:40 am

    In one of your comments you state that the GridView isn’t on the Master Page, so is it safe to assume that it’s on a page that uses a Master Page? And therefore it must be in a ContentPlaceholder control?

    The key issue is that FindControl method only looks for direct children (emphasis added):

    This method will find a control only if the control is directly contained by the specified container; that is, the method does not search throughout a hierarchy of controls within controls.

    So you either need to:

    1. Search for the control within the correct ContentPlaceholder control, rather than from Page.
    2. Loop through each of the Controls in Page.Controls recursively until you find the control you’re after.

    An example of 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;
    }
    

    Once you’ve got your control, you should cast it using as and then check for null just in case it’s not quite what you were expecting:

    var gridView = FindControlRecursively(Page, "GridView1") as GridView
    
    if (null != gridView) {
      // Do Stuff
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two methods in the same class and would like to find out
I would like to do something like: find . -iname *Advanced*Linux*Program* -exec kpdf {}
I would like to find out safe ways of implementing three dimensional arrays of
I would like to find out if a particular python datetime object is older
I would like to find out where I would be able to find the
I would like to find all the rows in a table and match on
I would like to find out what is the most significant differences of MS
I would like to find out, in JavaScript, which element currently has focus. I've
I would like to find a way to see what happens while my XAML
I would like to find the values of some of windows API constants, such

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.