Adding dynamic controls with non-fixed names

  • Thread starter Thread starter Dunc
  • Start date Start date
D

Dunc

I'm writing an app that uses a lot of time-date functions, so I'm trying to
create a calendar-type user control that adds a day, month, and year box to
a number of places on my page.

I can successfully dynamically create the controls, and add them to my page,
but I can only access them on postback if they've got a fixed name (so I can
declare them at the top of the page with the other web UI controls). If I
pass a value to create a name based on a string, I can't find any way of
accessing the postback values of them.

Has anyone got any ideas on how to get around this?
 
Can you have a public value in the User Control to identify that control
from the other User Controls that you add, and then in the Postback, loop
through the Controls array of the container until you find the right type of
(typeof) control, cast it, get the identifier and do what you need to?

-Darrin
 
Not that I've been able to, as you still need to declare a variable, ergo
defeating the purpose of creating the controls at runtime with a name based
on a string parameter.

Duncan
 
I may not be understanding your question correctly, but this is what I was
thinking:

Assume your UserControl is of type: MyNameSpace.MyControl

In your postback on the aspx page that the controls were added to you can
loop through the controls on the web form:

//Loop through all controls on the page.
foreach(Control ctrl in this.Controls)
{
//Check to see if it is the UserControl.
if(ctrl.GetType() == typeof(MyNameSpace.MyControl))
{
//Cast it as the UserControl to expose any methods or properties and
do what you need to do.
((MyNameSpace.MyControl)ctrl).GetProperyOrMethod();
}

}
 
Back
Top