Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspx_professional thread: RE: Help with retreiving values from dynamica- lly added controls


Message #1 by "Michael Bleed" <mbleed@y...> on Wed, 27 Feb 2002 21:38:31
Hello,

I think that the way I have solved this problem is very messy and that there must be a better way to go about it.
However I have other pressing things and I am sticking with this solution for now.

An easy, but clunky, way of getting at the result is to capture the Page_PreRender event.
This is where all of the controls values are updated/restored.
Place you dynamically created controls in a placeholder or table cell which you set the ID of.
Now in the Page_PreRender(Object sender,EventArgs e) event handler you can use the following code to find your controls.

Object myHolder = Page.FindControl("MyHolder");
foreach (Object myObject in myHolder.Controls)
{
	switch (myObject.ToString())
	{
		case "TextBox":
			TextBox myBox = (TextBox) myObject;
			string myString = myBox.Text
			break;
	}
}

The reason I have used the .ToString() command to get the type of the object so that I could write this quickly.
There are proper ways of determining the type of an Object.

I have not looked at using the ViewState to get the values of the controls.  However all controls and their values are kept in the
ViewState.  I am sure that there is some way to retrieve these values.


Possibly if you know that there is only going to be one TextBox and one CheckBox etc you could give them an ID when you create them.
Then you may be able to use the ViewState to access the information.
Just an idea,  as I said before I have not looked too far into this problem.  If you manage to use one of these methods, or if
anyone out there has a better idea, then please let me know.

Regards.

Aiden Montgomery

Industrial Trainee
Fujitsu-Siemens Computers
Web Development Team
Aiden.Montgomery@F...

-----Original Message-----
From: Michael Bleed [mailto:mbleed@y...]
Sent: 27 February 2002 21:39
To: ASPX_Professional
Subject: [aspx_professional] RE: Help with retreiving values from
dynamica- lly added controls


I am having the same problem retrieving values from dynamically created 
controls.  In my case, I create a radiobuttonlist, checkboxlist, or 
textbox based on info in the Db and one group is created for each line of 
a datagrid.  The controls show up perefectly and can be interacted with, 
but I cannot read them to find out which of the items has been checked.  I 
think that your problem was similar, maybe you could let me know what you 
did?

> Hey there,
> 
> I was wondering if anyone had found a nice solution to this?
> 
> If not then I have come up with some thing that solve this to a certain 
extent.
> 
> As I could not find the values within the controls on the postback 
within the event handler functions,
> I had to find another way to retrieve the values.
> 
> I managed to do this by using the Page_PreRender event.  Within this 
event I could access the values within
> the controls.
> 
> If anyone wants to know anything more about it, let me know.
> 
> Aiden
> 
> -----Original Message-----
> From: Montgomery, Aiden 
> Sent: 02 November 2001 10:47
> To: ASPX_Professional
> Subject: [aspx_professional] Help with retreiving values from
> dynamically added controls
> 
> 
> Sorry if this has been covered before but it is frustrating me. 
> 
> I am going to take a moment to set the scene. 
> 
> I have a page that has a textbox and a button on it. 
> You enter a value in the textbox then click the button. 
> 
> The page reloads with the textbox and the button. 
> Also now the page has generated a number of placeholders and entered 
tables into each one. Also another set of textboxes appear. 
> The number of placeholders, and therefore tables, and extra text is 
determined by the number you entered into the textbox. 
> 
> The extra text boxes are for you to enter the number of cells you 
require in each of the sections. 
> 
> This is as far as I can get. I simply cannot find a way to get at the 
values which are typed into the generated textboxes and therefore cannot 
determine how many cells are required in the sections. 
> 
> What is it that I am over looking? 
> 
> I have tried accessing the controls through using the findcontrol and 
the ID which I have sepcified. Also I have tried to access them by using a 
foreach and going through the controls collection. 
> 
> I have managed to get the actual controls. However not the values which 
they hold. Using the stated methods I found my way to the actual control 
objects. Once there I cast them to (textbox) and then tried to access the 
textbox.value 
> This returned nothing. 
> I thought about the fact that because they were created dynamically they 
may have to be added to the page again after post back to be able to 
access them. This helped in no way at all. I also enabled viewstate and 
again nothing. 
> 
> Someone out there must know where I am going wrong. 
> 
> Aiden 
> 
> ---
> VBug Winter Conference 2001 
> 
> Make no mistake - there's a great learning curve 
> for developers moving from COM to .NET.  So why 
> would you want to be a pioneer and cross the bridges 
> first?  Attend the .NET Developer's Conference 
> (28th to 30th November 2001) In London and realise 
> the benefits of adopting the technology early.  By 
> attending you'll understand the key challenges and 
> be left with a thorough understanding of the major 
> .NET fundamentals.   If you're already working 
> with .NET this is a must-attend event.  
> 
> http://www.vbug.co.uk/redirect.asp?url=39&id=17
> 
> ---
> You are currently subscribed to 
> aspx_professional as: aiden.montgomery@f...
> $subst('Email.Unsub')


  Return to Index