473,897 Members | 3,371 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem access usercontrol added to placeholder

Hi

I have this that adds some usercontrol (UCTodays.ascx) to a placeholder

foreach(A a in B){

UCTodays ucline = (UCTodays )LoadControl("U CTodays.ascx");
ucline.Initiali zecontrol(line, alternate);
Placeholder1.Co ntrols.Add(ucli ne);
}

On the mainpage i have a save button when pressed much call the add
usercontrol´s SaveValues() method which of course is public.

If I had manually added the controll then this would be easy. But how do i
get access the controls added? I dont now their id. I have tried
Page.findcontro l("_ctl0") etc. but I dont get the usercontrol.

I have also tried to retain a collection of references to the usercontrols
added but when I call these references i get a usercontrol which contains
the oldvalues before postback.

Can´t i do this?

Anders, Denmark
Nov 19 '05 #1
9 2459
Anders,

You are most likely accessing the control too early. I would suggest
tracing out when your controls are loaded and when you are attempting to
access the data within. You may need to create some sort of event to notify
the observer that the data has been loaded.

Thanks,
Ian Suttle
http://www.IanSuttle.com
http://www.NewsFuel.com - Aggregated Technology News
"Anders K. Jacobsen [DK]" wrote:
Hi

I have this that adds some usercontrol (UCTodays.ascx) to a placeholder

foreach(A a in B){

UCTodays ucline = (UCTodays )LoadControl("U CTodays.ascx");
ucline.Initiali zecontrol(line, alternate);
Placeholder1.Co ntrols.Add(ucli ne);
}

On the mainpage i have a save button when pressed much call the add
usercontrol´s SaveValues() method which of course is public.

If I had manually added the controll then this would be easy. But how do i
get access the controls added? I dont now their id. I have tried
Page.findcontro l("_ctl0") etc. but I dont get the usercontrol.

I have also tried to retain a collection of references to the usercontrols
added but when I call these references i get a usercontrol which contains
the oldvalues before postback.

Can´t i do this?

Anders, Denmark

Nov 19 '05 #2
> You are most likely accessing the control too early. I would suggest
tracing out when your controls are loaded and when you are attempting to
access the data within. You may need to create some sort of event to
notify
the observer that the data has been loaded.


Im not quite sure i follow. To recap.

I have my main page. To this i add usercontrols which contains textboxes
with initial values. Now user changes these values in the usercontrols which
was added to the mainpage through a placeholder.

Now the user push a save button on the main page.

1. How do a GET the reference to these usercontrols?

2. Reagarding that i access the usercontrol before load. The usercontrols
are loaded correctly, and I guess the new values in the usercontrols
textboxes should be available in alle events on the mainpage...righ t?

Anders

Nov 19 '05 #3
Anders,

Sorry for not being more specific the first time.

You may be attempting to access the user control data before the viewstate
information has been updated to the controls. If this were the case, you
would get the default values of the user control instead of the updated input.

To ensure that your timing is correct, add an event handler to the main page
that subscribes to the user control's save button (you may have to declare
the button in the control as public). The code would resemble the following:

this.userContro lName.btnSave.C lick += new
System.EventHan dler(this.handl eSaveClick);

With this example, the parent page has a function called handleSaveClick
that will get called when the user control's save button is clicked. You can
then access the information in the user control at the proper time.

If this is still a bit confusing, don't feel bad. Let me know if you
require further explanation.

Thanks,
Ian Suttle
http://www.IanSuttle.com
http://www.NewsFuel.com - Aggregated Technology News

"Anders K. Jacobsen [DK]" wrote:
You are most likely accessing the control too early. I would suggest
tracing out when your controls are loaded and when you are attempting to
access the data within. You may need to create some sort of event to
notify
the observer that the data has been loaded.


Im not quite sure i follow. To recap.

I have my main page. To this i add usercontrols which contains textboxes
with initial values. Now user changes these values in the usercontrols which
was added to the mainpage through a placeholder.

Now the user push a save button on the main page.

1. How do a GET the reference to these usercontrols?

2. Reagarding that i access the usercontrol before load. The usercontrols
are loaded correctly, and I guess the new values in the usercontrols
textboxes should be available in alle events on the mainpage...righ t?

Anders

Nov 19 '05 #4
> Sorry for not being more specific the first time.

No problem!
You may be attempting to access the user control data before the viewstate
information has been updated to the controls. If this were the case, you
would get the default values of the user control instead of the updated
input.
Ok
To ensure that your timing is correct, add an event handler to the main
page
that subscribes to the user control's save button (you may have to declare
the button in the control as public). The code would resemble the
following:
But. Mu user control dont have a save button. The problem is that they are
self contained. Each usercontrol fetch its own data from a datastore into
its textboxes. Now. if the user presses the one and only save button on the
mainform (where the usercontrols are added to) i need to comunicate to the
usercontrol ( by its public SaveData() method ). My problem is. If I held
the references to the usercontrol in an arrayList of usercontrols references
(In a session var )and called these on OnSaveClick, so when debugging its
textbox value was not updated. Maybe because of the viewstate problem you
refered to.

But again. the save button is the main page which has the placeholder which
has the usercontrols.

Thanks for your time until now!

Anders, Denmark

this.userContro lName.btnSave.C lick += new
System.EventHan dler(this.handl eSaveClick);

With this example, the parent page has a function called handleSaveClick
that will get called when the user control's save button is clicked. You
can
then access the information in the user control at the proper time.

If this is still a bit confusing, don't feel bad. Let me know if you
require further explanation.

Thanks,
Ian Suttle
http://www.IanSuttle.com
http://www.NewsFuel.com - Aggregated Technology News

"Anders K. Jacobsen [DK]" wrote:
> You are most likely accessing the control too early. I would suggest
> tracing out when your controls are loaded and when you are attempting
> to
> access the data within. You may need to create some sort of event to
> notify
> the observer that the data has been loaded.


Im not quite sure i follow. To recap.

I have my main page. To this i add usercontrols which contains textboxes
with initial values. Now user changes these values in the usercontrols
which
was added to the mainpage through a placeholder.

Now the user push a save button on the main page.

1. How do a GET the reference to these usercontrols?

2. Reagarding that i access the usercontrol before load. The usercontrols
are loaded correctly, and I guess the new values in the usercontrols
textboxes should be available in alle events on the mainpage...righ t?

Anders

Nov 19 '05 #5
Ah, I see your issue now. You are definately dealing with a timing issue.
My suggestion would be to try creating an event handler for the Load event of
the user control from the parent page. You know that the view state
information has been updated at that point. Check out this list of events in
order of execution (taken from someone else):

1. Instantiate
2. Initialize
3. TrackViewState
4. LoadViewState (postback)
5. Load postback data (postback, IPostBackDataha ndler.LoadPostd ata)
6. Load
7. Load postback data for dynamical controls added on Page_Load
8. Raise Changed Events (postback, IPostBackDataha ndler.RaisePost DataChanged)
9. Raise postback event (postback, IPostBackEventH andler.RaisePos tBackEvent)
10.PreRender
11. SaveViewState
12. Render
13. Unload
14. Dispose

If you create an event handler in the main page for the Load event, you
could then call the SaveData function at that time and you should have the
updated information. An example of this event declaration would look like:

myControl.Load += new System.EventHan dler(this.Contr ol_Load);
As a separate suggestion, when loading the user control dynamically, perhaps
set some sort of flag (property, variable, etc) in the user control. Then in
the Load event of the user control, check for the flag and process
accordingly.
Let me know if this doesn't cover it for you.

Ian Suttle
http://www.NewsFuel.com

"Anders K. Jacobsen [DK]" wrote:
Sorry for not being more specific the first time.


No problem!
You may be attempting to access the user control data before the viewstate
information has been updated to the controls. If this were the case, you
would get the default values of the user control instead of the updated
input.


Ok
To ensure that your timing is correct, add an event handler to the main
page
that subscribes to the user control's save button (you may have to declare
the button in the control as public). The code would resemble the
following:


But. Mu user control dont have a save button. The problem is that they are
self contained. Each usercontrol fetch its own data from a datastore into
its textboxes. Now. if the user presses the one and only save button on the
mainform (where the usercontrols are added to) i need to comunicate to the
usercontrol ( by its public SaveData() method ). My problem is. If I held
the references to the usercontrol in an arrayList of usercontrols references
(In a session var )and called these on OnSaveClick, so when debugging its
textbox value was not updated. Maybe because of the viewstate problem you
refered to.

But again. the save button is the main page which has the placeholder which
has the usercontrols.

Thanks for your time until now!

Anders, Denmark

this.userContro lName.btnSave.C lick += new
System.EventHan dler(this.handl eSaveClick);

With this example, the parent page has a function called handleSaveClick
that will get called when the user control's save button is clicked. You
can
then access the information in the user control at the proper time.

If this is still a bit confusing, don't feel bad. Let me know if you
require further explanation.

Thanks,
Ian Suttle
http://www.IanSuttle.com
http://www.NewsFuel.com - Aggregated Technology News

"Anders K. Jacobsen [DK]" wrote:
> You are most likely accessing the control too early. I would suggest
> tracing out when your controls are loaded and when you are attempting
> to
> access the data within. You may need to create some sort of event to
> notify
> the observer that the data has been loaded.

Im not quite sure i follow. To recap.

I have my main page. To this i add usercontrols which contains textboxes
with initial values. Now user changes these values in the usercontrols
which
was added to the mainpage through a placeholder.

Now the user push a save button on the main page.

1. How do a GET the reference to these usercontrols?

2. Reagarding that i access the usercontrol before load. The usercontrols
are loaded correctly, and I guess the new values in the usercontrols
textboxes should be available in alle events on the mainpage...righ t?

Anders


Nov 19 '05 #6
> As a separate suggestion, when loading the user control dynamically,
perhaps
set some sort of flag (property, variable, etc) in the user control. Then
in
the Load event of the user control, check for the flag and process
accordingly.


Oh my. You did it! Simple but effective! It now works like a charm.

I really appriciate your help! Other newsgroups could learn from this groups
profesionalism.

Thanks again Ian, I was almost giving up my appoach

Anders Jacobsen
Nov 19 '05 #7
Great! Good luck with the rest of the project.

Ian Suttle
http://www.NewsFuel.com
http://www.IanSuttle.com

"Anders K. Jacobsen [DK]" wrote:
As a separate suggestion, when loading the user control dynamically,
perhaps
set some sort of flag (property, variable, etc) in the user control. Then
in
the Load event of the user control, check for the flag and process
accordingly.


Oh my. You did it! Simple but effective! It now works like a charm.

I really appriciate your help! Other newsgroups could learn from this groups
profesionalism.

Thanks again Ian, I was almost giving up my appoach

Anders Jacobsen

Nov 19 '05 #8
If you have sample code for how you got this working it would be
greatly appriciated. Thanks!

Anders K. Jacobsen [DK] wrote:
As a separate suggestion, when loading the user control dynamically, perhaps
set some sort of flag (property, variable, etc) in the user control. Then in
the Load event of the user control, check for the flag and process
accordingly.
Oh my. You did it! Simple but effective! It now works like a charm.

I really appriciate your help! Other newsgroups could learn from this

groups profesionalism.

Thanks again Ian, I was almost giving up my appoach

Anders Jacobsen


Nov 19 '05 #9
> If you have sample code for how you got this working it would be
greatly appriciated. Thanks!


I later on acounted problems after been working with the solutions. It´s
seems its quite hard to get the timings right when you need to speak TO the
dynamic added usercontrol. I never got it REALLY workrng. Always a new issue
when sloved another so a went a way from the model and implemented it in a
Repeater.

The logic for each of my lines was quite complex so I wanted to represent
each row as a usercontrol.

But sorry. Havent any code which can do it right.

Anders
Nov 19 '05 #10

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

Similar topics

2
9693
by: Lars Pedersen | last post by:
My app is adding a usercontrol at runtime. How is it possible to remove this usercontrol? Have tried Page.Controls.Remove(UserControl);, but that wont do the trick - any suggestions? -Lars
2
2676
by: bill yeager | last post by:
When trying to run my web project, I get the following error: Parser Error Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately. Parser Error Message: The base class includes the
7
3275
by: Tim T | last post by:
Hi, I have the need to use dynamically loaded user controls in a webform page. I have the controls loading dynamically, and that part works fine. this is the code used in a webform to dynamically load one of several controls: private void btnCategory_Click(object sender, System.EventArgs e) { Control myControl = LoadControl(DropDownList1.SelectedItem.Text + ".ascx"); PlaceHolder1.Controls.Add(myControl);
1
5383
by: Angel | last post by:
I have added controls to the placeholder control. All the controls that were added have EnableViewState = true including the placeholder. One of the controls has a button that performs a postback. My question is when the button is clicked and a postback is performed, the controls that were in the placeholder no longer appears on the screen! Am I doing something wrong here? Why are the controls back on the screen eventhough they have enable view...
1
2338
by: Kris van der Mast | last post by:
Hi, been a while since I posted a question myself instead of trying to help others out. I'm refactoring an existing web app that uses dynamic loading of user controls and a lot of response.redirects to the same page. Because I hate the overhead by doing this I'm searching for a cleaner option. But I'm having troubles (off course or I wouldn't be posting this).
6
2129
by: Valeri Hristov via .NET 247 | last post by:
I have a problem with a ASP.NET web site. I?m using Windows2000Server SP4, .NET Framework 1.1. The problem is that sometimes(relatively seldom) when browsing the Administration Console(see below) a redirect (unwanted and unexpected) is made to thePublic Site to a random page. Often (almost in every of thecases) the redirect is to a page of the UserPart of thePublicSite with a User logged in! I?m almost sure that the Userwas browsing the site in...
2
3454
by: Tim_Mac | last post by:
hi, i have an aspx page which dynamically loads a user control and adds it to a placeholder. the control is recreated and added to the placeholder for postbacks as well. the user control contains a datagrid and some buttons and textboxes. the button events fire correctly and execute their code. however, the datagrid events fire a postback, and execute Page_load in the usercontrol, but the Item_Command code is not executed. the code to...
0
1608
by: anandv81 | last post by:
Hi, I encountered a strange problem while working on an application, the problem goes like this. I am generating a few textboxes at runtime at the server side and added to a placeholder, a value is set for each of the textboxes. There is a button that causes the page to be posted back and the texboxes are re-rendered. When the textboxes are re-rendered the values are reset and these texboxes are again added to the placeholder. The names
2
1351
by: boomcreation | last post by:
Hi, When I load a userControl, I check if I have a variable session. If my variableSession1 = "A" and do nothing. but if my variableSession1 = "B", I want clear the html in my control and write: "You need to do this or that"
0
9990
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10941
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10478
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9656
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7185
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5873
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4698
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4293
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3300
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.