Solved

Findcontrol Within a Gridview Footer template

Posted on 2006-11-29
2
1,275 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
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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

829 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