473,406 Members | 2,259 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,406 software developers and data experts.

Finding a dynamically added control - Quite Urgent :-(

Hi All.

In one of my user controls I add a textbox to a placeholder sitting on the
user control.

txtUsername = new TextBox();
txtUsername.ID = "txtUsername";
phUsernamePlaceholder.Controls.Add(txtUsername);

Then in an event handler for the pages save changes button I try to find the
control via:

TextBox = ((Textbox)this.FindControl("txtUsername")).Text;

Unfortunately, this isn't working - it can't find the control.

Can anyone tell me where my control has gone?

Thanks all!

Simon
Nov 18 '05 #1
3 7398
Hi,

you would need to look inside the naming container of the TextBox, that
would be the user control e.g run FindControl against the user control.

Another way could be exposing the TextBox outside the user control as an
property (create a member that references the TextBox, when you instantiate
it and then create a property which accesses this member).

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist
http://blogs.aspadvice.com/joteke

"Simon Harvey" <sh856531@microsofts_free_email_service.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Hi All.

In one of my user controls I add a textbox to a placeholder sitting on the
user control.

txtUsername = new TextBox();
txtUsername.ID = "txtUsername";
phUsernamePlaceholder.Controls.Add(txtUsername);

Then in an event handler for the pages save changes button I try to find the control via:

TextBox = ((Textbox)this.FindControl("txtUsername")).Text;

Unfortunately, this isn't working - it can't find the control.

Can anyone tell me where my control has gone?

Thanks all!

Simon

Nov 18 '05 #2
You have to recreate the control every time when you open the page ( no
matter the page is viewed for first time or it is submitted )
Check this : http://support.microsoft.com/default...b;EN-US;317794

Regards
Martin

"Simon Harvey" <sh856531@microsofts_free_email_service.com> wrote in message
news:#C**************@tk2msftngp13.phx.gbl...
Hi All.

In one of my user controls I add a textbox to a placeholder sitting on the
user control.

txtUsername = new TextBox();
txtUsername.ID = "txtUsername";
phUsernamePlaceholder.Controls.Add(txtUsername);

Then in an event handler for the pages save changes button I try to find the control via:

TextBox = ((Textbox)this.FindControl("txtUsername")).Text;

Unfortunately, this isn't working - it can't find the control.

Can anyone tell me where my control has gone?

Thanks all!

Simon

Nov 18 '05 #3
The FindControl() method is part of the System.Web.UI.Control base class,
and is therefore common to all System.Web.UI.Controls. It will find a
Control by the given ID in the Controls Collection of the Control for which
it is called. It doesn't work recursively. That is, it only searches the
Controls Collection of the Control for which it is called; it doesn't search
the Controls Collection of every child Control of that Control.

According to your message, you most likely have a WebForm, which is an ASPX
page with a runat=server form on it. Inside that form, you have a User
Control. Inside that, you have a PlaceHolder. Inside the PlaceHolder, you
have a textbox. Let's diagram the hierarchy of Controls:

Page
Form
UserControl
PlaceHolder
TextBox

When you call this.FindControl() in the context of a Page class, you are
searching the Controls Collection of the Page. Note that, according to our
hierarchy diagram, the TextBox is NOT a member of the Page's Controls
Collection. It is a member of the PlaceHolder's Controls Collection. you
would have to call the FindControl() method of your PlaceHolder to get to
the TextBox.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"Simon Harvey" <sh856531@microsofts_free_email_service.com> wrote in message
news:#C**************@tk2msftngp13.phx.gbl...
Hi All.

In one of my user controls I add a textbox to a placeholder sitting on the
user control.

txtUsername = new TextBox();
txtUsername.ID = "txtUsername";
phUsernamePlaceholder.Controls.Add(txtUsername);

Then in an event handler for the pages save changes button I try to find the control via:

TextBox = ((Textbox)this.FindControl("txtUsername")).Text;

Unfortunately, this isn't working - it can't find the control.

Can anyone tell me where my control has gone?

Thanks all!

Simon

Nov 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

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...
6
by: Ken Varn | last post by:
I want to add my own custom <STYLE> section in the <HEAD> section of my ASP.NET page within a custom control. Can someone tell me how I can have my custom control add tags to the <HEAD> section of...
3
by: Egbert Nierop \(MVP for IIS\) | last post by:
Hi, I have a complex asp.net 1.1 program that dynamically needs to load controls (LoadControl) The textboxes etc inside these controls keep viewstate and values but not within Page_Load() ...
4
by: Harry | last post by:
Hello, I have a page with a RadioButtonList and a PlaceHolder control. The RadioButtonList's AutoPostBack attribute is set to TRUE and its SelectedIndexChanged event loads one of three...
7
by: Samuel | last post by:
Hi, I am building a page that makes use of user control as a templating technique. The following is that I have in mind and it is actually working: Root/ -- login.aspx -- login.aspx.vb --...
1
by: jelle.huygen | last post by:
Hello, I have a problem in ASP.NET 2.0 with the viewstate of my dynamically added user control. I have reproduced the problem with a very simple user control and a very simple page. On my...
5
by: Andrew Robinson | last post by:
I have a page that can load a number of different user controls. Each of these user controls inherits from a common base class and the controls are loaded based on application state, status, etc...
4
by: karthik25 | last post by:
Hi All, I have a problem in finding control in a dynamically created updated panel. I have given the code below. Following is just a starting effort in a completely dynamic user control. I am...
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: 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?
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
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
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...
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,...

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.