473,404 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

FindControl needs an explaination!

149 100+
Hi,

I am using FindControl to access dynamically loaded user controls. I'm sure I've got it all wrong as I'm new to .NET.

Currently if I want to access a button or view mode in a user control nested in a page nested in a master page. I have to use this method to find the control.

Expand|Select|Wrap|Line Numbers
  1. FormView viewFC1 = (FormView)this.FindControl("UpdatePanelFC").FindControl("DetailsViewFC1");
  2.             viewFC1.ChangeMode(FormViewMode.Edit);
This doesn't seem like the best way.

Can anyone please explain.
Oct 2 '07 #1
12 3753
rsdev
149 100+
HI Again,

What I mean is there a way of assigning an ID in the code behind class to reference controls that are layered deep into the page?

Thanks again.
Oct 3 '07 #2
Shashi Sadasivan
1,435 Expert 1GB
HI Again,

What I mean is there a way of assigning an ID in the code behind class to reference controls that are layered deep into the page?

Thanks again.
Controls burried under somthing else will need to be fished out the same way you burried them

FormView viewFC1 = (FormView)this.FindControl("UpdatePanelFC").FindCo ntrol("DetailsViewFC1");

from that I presume UpdatePanelFc is not a dynamically created control

you can shorten that piece of code using by
Expand|Select|Wrap|Line Numbers
  1. FormView viewFC1 = (FormView)thisUpdatePanelFC.FindControl("DetailsViewFC1");
cheers
Oct 3 '07 #3
rsdev
149 100+
Thanks for responding Shashi.

I am using a BasePage class for most of my methods/event handlers etc. Because they are used by lots of different user controls. The user controls are loaded into the page dynamically when they page is built. I have managed to assign them IDs when I load each control.

My ignorance starts to show when I am trying to access controls inside updatepanels inside user controls that have been dynamically loaded. I know there is a Controls Collection but when I try to recursively look through the collection to find a control I get errors.

The second problem I am having is that I have set my updatepanels inside each control to conditional. I want to an event in one user control to trigger an update in all the updatepanels. But these updatepanels are inside user controls that are positioned randomly on the page.

I need to find a way to access controls and updatepanels without using long strings of FindControl.

Any small examples to point me in the right direction would be appreciated.


Controls burried under somthing else will need to be fished out the same way you burried them

FormView viewFC1 = (FormView)this.FindControl("UpdatePanelFC").FindCo ntrol("DetailsViewFC1");

from that I presume UpdatePanelFc is not a dynamically created control

you can shorten that piece of code using by
Expand|Select|Wrap|Line Numbers
  1. FormView viewFC1 = (FormView)thisUpdatePanelFC.FindControl("DetailsViewFC1");
cheers
Oct 3 '07 #4
rsdev
149 100+
My answer lies somewhere in the ControlCollection. I have been playing around with this code but cannot get it to work!

Expand|Select|Wrap|Line Numbers
  1. foreach (Control c in Master.Controls)
  2.             {
  3.                     foreach (UpdatePanel up in c.Controls)
  4.                     {
  5.                         if (up is UpdatePanel)
  6.                         {
  7.                             up.Update();
  8.                         }
  9.                     }
  10. }
Any ideas!
Oct 3 '07 #5
Plater
7,872 Expert 4TB
How do you know it doesn't work?
That update() function might be getting called (unless you set a breakpoint and it didn't) but the results of that function call get overwritten by something else happening, like maybe in the page_load() function?
Oct 3 '07 #6
rsdev
149 100+
Hi Plater,

Thanks for responding.

In debug mode I get:

Unable to cast object of type 'System.Web.UI.HtmlControls.HtmlTitle' to type 'System.Web.UI.UpdatePanel'.

For {UpdatePanel up}

And 'up' is subsequnetly null. I think it loops infinitley unable to cast the objects.

I don't understand enough to know how to rectify this problem and if I do whether it will then update all the updatepanels on a page?!

Thanks for your help.
Oct 3 '07 #7
Shashi Sadasivan
1,435 Expert 1GB
Expand|Select|Wrap|Line Numbers
  1. //invoke this method from a method of yours by using this.updateControl(this);
  2. private void updateControl(Control cntrl)
  3. {
  4.    foreach(Control c in cntrl)
  5.    {
  6.        if(c.HasChildren)
  7.           this.updateControl(c);
  8.       else
  9.       {
  10.           UpdatePanel up;
  11.           if(c.getType() == up.GetType())
  12.           {
  13.               up = (UpdatePanel)c;
  14.               up.Update();
  15.           } 
  16.       }        
  17.    }
  18. }
Try using this....
I have found issues with casting...so i declare a variable of that type i amlooking for and let the system return its type.

Hope this works

cheers
Oct 3 '07 #8
rsdev
149 100+
Hi Shashi,

Thank you for your response.

How do you get round the error that the up variable is unassigned?

This is close.
Oct 3 '07 #9
rsdev
149 100+
Hi Shashi,

Thanks again I got it work. Excellent! You star.

Expand|Select|Wrap|Line Numbers
  1. private void updateControl(Control cntrl)
  2.         {
  3.             UpdatePanel up = new UpdatePanel();
  4.             foreach (Control c in cntrl.Controls)
  5.             {
  6.                 if (c.HasControls())
  7.                 {
  8.                     if (c.GetType() == up.GetType())
  9.                     {
  10.                         up = (UpdatePanel)c;
  11.                         up.Update();
  12.                     }
  13.                     this.updateControl(c);
  14.                 }
  15.                 else
  16.                 {
  17.                     if (c.GetType() == up.GetType())
  18.                     {
  19.                         up = (UpdatePanel)c;
  20.                         up.Update();
  21.                     }
  22.                 }
  23.             }
  24.         }
Expand|Select|Wrap|Line Numbers
  1. //to call method this.updateControl(Master);
I suppose the only draw back is creating a new UpdatePanel object everytime I want to refresh the panels. A little messy. Maybe I can find another way.

Thanks though excellent code:)
Oct 3 '07 #10
rsdev
149 100+
Then to answer my original post. Check this out:


Expand|Select|Wrap|Line Numbers
  1. //Find Control recursively
  2.  
  3.         public static Control findControl(string id, Control col)
  4.         {
  5.             foreach (Control c in col.Controls)
  6.             {
  7.                 Control child = findControlRecursive(c, id);
  8.                 if (child != null)
  9.                     return child;
  10.             }
  11.             return null;
  12.         }
  13.  
  14.         private static Control findControlRecursive(Control root, string id)
  15.         {
  16.             if (root.ID != null && root.ID == id)
  17.                 return root;
  18.  
  19.             foreach (Control c in root.Controls)
  20.             {
  21.                 Control rc = findControlRecursive(c, id);
  22.                 if (rc != null)
  23.                     return rc;
  24.             }
  25.             return null;
  26.         }
Oct 3 '07 #11
public static Control FindControlRecursive ( ref Control root , string id )
{
try
{
if (root.ID == id)
{
return root;
} //eof if

for (int i = 0; i < root.Controls.Count; i ++ )
{

Control c = new Control ();
c = root.Controls [ i ];
Control t = FindControlRecursive ( ref c , c.ID.ToString() );
if (t != null)
{
return t;
} //eof if
} //eof foreach
} //eof try
catch (Exception e)
{
Utils.Debugger.WriteIf ( "The following Exception occured : \n" + e.Message );
return null;
} //eof catch

return null;
} //eof FindControlRecursive
May 8 '08 #12
Plater
7,872 Expert 4TB
I don't see any reason to pass it by reference, you're not making changes to it, and even if you did, they would still "take".
Unless you want to maybe cut down on memory usage?
May 8 '08 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: BobJohnson | last post by:
Just started learning C++ and I need some help with my homework, shouldn't take long for people around here. I need to create a simple money calculator but I don't know how to make the output...
1
by: James G. Beldock | last post by:
I have seen the following behavior: when issuing a Page.FindControl() for a control which exists in an item template (from within an ItemDataBound() event, for example), I get nulls back...
2
by: christof | last post by:
How to do it: My page: <asp:DataList ID="dataListRoleMembers" ...> .... <FooterTemplate> <asp:LinkButton ID="btnAddMember" runat="server"...
2
by: encoad | last post by:
Hi everyone, I'm slowly going mad with Masterpages and the FindControl. After a couple days of figuring out how to use the FindControl command from within a Masterpage, I still can't explain...
5
by: daniel.hedz | last post by:
I am generating a usercontrol dynamically successfully, but when I try to find that usercontrol I get a type mismatch. This is what I am doing: //Loading my usercontrol...
14
by: =?Utf-8?B?QWxleCBNYWdoZW4=?= | last post by:
Hi. I have created a UserControl ("MyUC"). I've put a bunch of instances of that control on a Page ("Defaul.aspx"). The control works fine. Now, I want to be able to use "FindControl()" from...
7
by: AAaron123 | last post by:
Me.FindControl("MissionScheduleID"), below returns null. Do you know what I'm doing wrong? Thanks ***In my .aspx file I have: asp:Content ID="Content3"...
4
by: Hillbilly | last post by:
Maybe this is or isn't some kind of bug but it sure is goofy and remains a mystery that really has me puzzled for two reasons... // goofy syntax functions as expected... Panel finalStepButton =...
9
by: AAaron123 | last post by:
I'm this far in determining the correct code to find a textbox I need to set. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.