473,386 Members | 1,846 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,386 software developers and data experts.

Dynamically Find Control

149 100+
Hi,

Can't get my head round this one!

I have incrementally named PlaceHolders and I want to dynamically load a control into each one. All fine if I strongly type the holder name. But how do I reference the holder name dynamically.

Here's what I have;

Expand|Select|Wrap|Line Numbers
  1. List<PageCompData> compType = PageCompData.GetCompType(pageId);
  2.             int compLength = compType.Count;
  3.             for (int i = 0; i < compLength; i++)
  4.             {
  5.                 string compPath = "~/CMS/_admin/_components/" + compType[i].ComponentType + ".ascx";
  6.                 Control compControl = Page.LoadControl(compPath);
  7.                 (PlaceHolder)FindControl("PlaceHolder" + i).Controls.Add(compControl);
  8.             }
Thanks.
Jul 31 '07 #1
11 1892
Plater
7,872 Expert 4TB
Does that work?

.FindControl() only checks imediate children by default.
So for example, if you have a form, with a groupbox in it and then have 2 textboxes inside that groupbox:
myForm
mygroupbox
mytextbox1
mytextbox2

doing myForm.FindControl("mytextbox1"); will not work, you would need to do
mygroupbox.FindControl("mytextbox1"); to have it return the obbject.

Now, .FindControl() is overloaded to take a second parameter that will tell it to search the children of it's children, so with that 2nd parameter you could search in myForm for mytextbox1 and have it work.
Jul 31 '07 #2
rsdev
149 100+
Hi Plater,

Thanks for your reply!

No it's not working. Findcontrol is the wrong call. I'll explain further.

I want to a list of control dynamically to my placeholders. The placeholder id's are PlaceHolder1, PlaceHolder2 ... etc. So I can loop through database results and add User Controls to the placeholders depending on the results. I usually script client-side Javascript/Actionscript and can't undertsand how to dynamically set/get the PlaceHolder id so I can add the control.

Any ideas?

Really struggling with this.
Jul 31 '07 #3
Plater
7,872 Expert 4TB
So you want to do this clientside and not in the backend code?
Jul 31 '07 #4
rsdev
149 100+
Nope! Just trying to explain my ignorance.

How do I make this work;

Expand|Select|Wrap|Line Numbers
  1. PlaceHolder plcName = "PlaceHolder"+i;
  2. plcName.Controls.Add(compControl);
Thanks
Jul 31 '07 #5
Plater
7,872 Expert 4TB
You can use FindControl to get object references to your placeholders.
Then create and add new controls to it.
For sake of argument:
Expand|Select|Wrap|Line Numbers
  1. PlaceHolder p = myowningcontrol.FindControl("PlaceHolder"+i.ToString());
  2. //should check for null
  3. //p.Controls.Clear();//uncomment this class to clear out any previous controls in there?
  4. Label myl1=new Label();
  5. myl1.ID=newUniqueID;//fix this to a unique ID somehow
  6. myl1.text=displayDataFromDataBase1;//
  7. p.Controlls.Add(myl1);
  8.  
  9. Label myl2=new Label();
  10. myl2.ID=newUniqueID;//fix this to a unique ID somehow
  11. myl2.text=displayDataFromDataBase2;//
  12. p.Controlls.Add(myl2);
  13.  
Jul 31 '07 #6
rsdev
149 100+
Thanks again!

I am very close now except I don't have an ownercontrol. So I tried this;

Expand|Select|Wrap|Line Numbers
  1. PlaceHolder plcName = (PlaceHolder)Page.FindControl("PlaceHolderName"+i.ToString());
  2.                 plcName.Controls.Add(compControl);
But I get an error?
Jul 31 '07 #7
rsdev
149 100+
Actually I've managed to make his work.

Expand|Select|Wrap|Line Numbers
  1. PlaceHolder plcName = (PlaceHolder)this.Master.FindControl("MainContent").FindControl("PlaceHolderName0);
  2. plcName.Controls.Add(compControl);

But this doesn't


Expand|Select|Wrap|Line Numbers
  1. PlaceHolder plcName = (PlaceHolder)this.Master.FindControl("MainContent").FindControl("PlaceHolderName"+i.ToString());
  2. plcName.Controls.Add(compControl);
I don't undertsand?!
Jul 31 '07 #8
rsdev
149 100+
Okay even this works.

Expand|Select|Wrap|Line Numbers
  1. PlaceHolder plcName = (PlaceHolder)this.Master.FindControl("MainContent").FindControl("PlaceHolderName" + 0.ToString());
So why doesn't it recognise i as the int 0?

Sorry to bug you.
Jul 31 '07 #9
rsdev
149 100+
I'm missing PLACEHOLDER. What an idiot. Thanks for your help. You did help me to solve this.
Jul 31 '07 #10
Plater
7,872 Expert 4TB
Do you need to search for MainContent or even placeholder at all?
That code you posted seem liks you coulda just done:
PlaceHolderName0.Controlls.Add(<whatever>);

Can't you just do
Expand|Select|Wrap|Line Numbers
  1. //the "i" was because you said you wanted to itterate through them.
  2. PlaceHolder plcName = (PlaceHolder)MainContent.FindControl("PlaceHolderName"+i.ToString());
  3. plcName.Add(<whatever>);
  4.  
Jul 31 '07 #11
rsdev
149 100+
Sorry I think I have confused you. The problem is solved thanks to your earlier post.

I am using masterpages so I needed to first find MainContent content holder. Then I loop through using i interger and add user controls to all the placeholders. Works well. Here's the final code;

Expand|Select|Wrap|Line Numbers
  1.  List<PageCompData> compType = PageCompData.GetCompType(pageIDS.Title);
  2.             int compLength = compType.Count;
  3.             int i=0;
  4.             while(i<compLength)
  5.             {
  6.                 string compPath = "~/CMS/_admin/_components/" + compType[i].ComponentType + ".ascx";
  7.                 Control compControl = Page.LoadControl(compPath);
  8.                 PlaceHolder plcName = (PlaceHolder)this.Master.FindControl("MainContent").FindControl("PlaceHolderName"+i.ToString());
  9.                 if (plcName == null)
  10.                 {
  11.                     plcName = (PlaceHolder)this.Master.FindControl("RightColumn").FindControl("PlaceHolderName" + i.ToString());
  12.                 }
  13.                 plcName.Controls.Add(compControl);
  14.                 i++;
  15.             }
Thanks again for your help:)
Jul 31 '07 #12

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

Similar topics

0
by: Billy | last post by:
I have a web page with a control (Control#A) on it. The control (#A) has a control (Control#B) on it which displays an exception message when set and displayed. I am creating a simple datagrid...
8
by: Donald Xie | last post by:
Hi, I noticed an interesting effect when working with controls that are dynamically loaded. For instance, on a web form with a PlaceHolder control named ImageHolder, I dynamically add an image...
1
by: Earl Teigrob | last post by:
PROBLEM: When a user control is loaded into a PlaceHolder control more than once, the events do not fire on the first click of a control on the dynamically loaded user control. In other words, the...
1
by: Reza Nabi | last post by:
Bakground: I have a webform (LoadCtl.aspx) which loads the user control to a placeholder dynamically based on the ctlName querystring passed in the URL. Webform (LoadCtl.aspx) also passes a...
1
by: Carol | last post by:
Hi, I am working in C# I have created one usercontrol. Now added a panel to this control. In a panel i have added another control. Now, i have added this usercontrol to a form. Now, on button...
4
by: sydney.luu | last post by:
Hello, I would greatly appreciate if someone can show me how to dynamically build a Repeater with unknown number of columns at design time. I have looked various threads in this newsgroup,...
2
by: A.Wussow | last post by:
Hi Everybody, i want to load dynamically content from some user controls (with forms, or some data-controls) using atlas. So i use an UpdatePanel for loading the user control into a placeholder....
4
by: Craig Buchanan | last post by:
I dynamically add data-bound templates to a gridview in my ascx control. while this works correctly when the gridview is databound to the datatable, i'm having issues on postback. i would like...
7
by: RichB | last post by:
I am trying to get to grips with the asp.net ajaxcontrol toolkit, and am trying to add a tabbed control to the page. I have no problems within the aspx file, and can dynamically manipulate a...
3
by: Allen Chen [MSFT] | last post by:
Hi Richard, Quote from Richard================================================== However I also want to be able to remove the panes. I have tried to include this, but find that when I first...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.