Solved

Findcontrol Within a Gridview Footer template

Posted on 2006-11-29
2
1,283 Views
Last Modified: 2008-01-09
Ah! Mega Urgent!

I am trying to locate a Label within a footer template of a Gridview and am using the following code to do so but get the error 'Object reference not set to an instance of an object'...

Code-Behind Code within an button OnClick event:

Dim lblRec As Label = GridView1.FindControl("lblRecTonT")


Could anyone kindly advise what I could be doing wrong?

Many thanks

Rit
0
Comment
Question by:rito1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 1

Author Comment

by:rito1
ID: 18038365
I found the answer, just in case anyone is in the same position as me.. ever :-)

Dim lblRec As Label = GridView1.FooterRow.FindControl("lblRecTonT") - in needs the 'FooterRow'
0
 
LVL 28

Accepted Solution

by:
strickdd earned 500 total points
ID: 18039146
Here is some C# code which should be easy to convert. Pass in the id "lblRecTonT" and the control collection Page.controls or GridView1.Controls. This is guaranteed to work if the ID exists.

public static System.Web.UI.Control FindControl(string controlId, System.Web.UI.ControlCollection controls)
            {
                  for (int counter = 0; counter < controls.Count; ++counter) // loop through each control in the control collection
                  {
                        if (controls[counter].ID == controlId) //see if the current control is a match
                        {
                              return controls[counter];
                        }
                        else if (controls[counter].Controls.Count > 0) // check the child controls
                        {
                              System.Web.UI.Control FoundControl = FindControl(controlId, controls[counter].Controls); // recursive call to check child controls

                              if (FoundControl != null) // control found in child's controls
                              {
                                    return FoundControl;
                              }
                        }
                  }

                  return null;
            }
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question