How do you assign and ID to a user control created programmatically?
Messages   Related Types
This message was discovered on ASPFriends.com 'aspngbeta' list.


Michael Bunger
Hi all,

I am considering creating user controls programmatically, using code similar
to what I saw in another thread on this newsgroup:

foreach (Product p in allProducts)
{
// load in the User Control template
ProductControl productControl &# (;ProductControl)
LoadControl("ProductControl.ascx");

// now set the properties of the user control
((ProductControl_ascx)productControl).Price &# p;.Price;
((ProductControl_ascx)productControl).PurchaseMethod &# p;.PurchaseMethod
;
((ProductControl_ascx)productControl).Quantity &# p;.Qty ;

// add this product to the page
Page.Add(productControl);
}

However, my user control will allow the user to change some values (ie.
dropdown, text box values), so I need to be able to read these values back
for each programmatically created control once the user submits the page.
How do I reference each user control so that I can access its properties
that will give me the data I need? I figured I could use FindControl but I
need an ID as a parameter. Is there a way to set the ID for a user control?
Or, if it is automatically assigned, is there a way to retrieve it?

In case you are wondering, unfortunately, I don't think I can use a repeater
because I have read that you can't change the template and each of my rows
may have a different number of controls shown (some may have extra graphic
icons, notes, etc.) and a grid really doesn't work for me in my particular
situation.

Any suggestions and/or comments will be greatly appreciated.

Thanks,
Michael Bunger
Reply to this message...
 
    
Michael \(Raecom Inc\)

Hi all,

I am considering creating user controls programmatically, using code similar
to what I saw in another thread on this newsgroup:

foreach (Product p in allProducts)
{
// load in the User Control template
ProductControl productControl = (ProductControl)
LoadControl("ProductControl.ascx");

// now set the properties of the user control
((ProductControl_ascx)productControl).Price = p.Price;
((ProductControl_ascx)productControl).PurchaseMethod = p.PurchaseMethod
;
((ProductControl_ascx)productControl).Quantity = p.Qty ;

// add this product to the page
Page.Add(productControl);
}

However, my user control will allow the user to change some values (ie.
dropdown, text box values), so I need to be able to read these values back
for each programmatically created control once the user submits the page.
How do I reference each user control so that I can access its properties
that will give me the data I need? I figured I could use FindControl but I
need an ID as a parameter. Is there a way to set the ID for a user control?
Or, if it is automatically assigned, is there a way to retrieve it?

In case you are wondering, unfortunately, I don't think I can use a repeater
because I have read that you can't change the template and each of my rows
may have a different number of controls shown (some may have extra graphic
icons, notes, etc.) and a grid really doesn't work for me in my particular
situation.

Any suggestions and/or comments will be greatly appreciated.

Thanks,
Michael Bunger

Reply to this message...
 
    
Susan Warren
Michael,

Just assign the ID... it's a property of all controls (of type String):

productControl.ID = "foo1";

You might wish to consider using a for loop (rather than foreach) so you
can "uniquify" your IDs with a number:

productControl.ID = "foo" + i.ToString();

hth,
Susan

-----Original Message-----
From: Michael Bunger [mailto:Click here to reveal e-mail address]
Sent: Sunday, April 29, 2001 4:58 AM
To: aspngbeta
Subject: [aspngbeta] How do you assign and ID to a user control created
programmatically?

Hi all,

I am considering creating user controls programmatically, using code
similar to what I saw in another thread on this newsgroup:

foreach (Product p in allProducts)
{
// load in the User Control template
ProductControl productControl = (ProductControl)
LoadControl("ProductControl.ascx");

// now set the properties of the user control
((ProductControl_ascx)productControl).Price = p.Price;
((ProductControl_ascx)productControl).PurchaseMethod p.PurchaseMethod ;
((ProductControl_ascx)productControl).Quantity = p.Qty ;

// add this product to the page
Page.Add(productControl);
}

However, my user control will allow the user to change some values (ie.
dropdown, text box values), so I need to be able to read these values
back for each programmatically created control once the user submits the
page. How do I reference each user control so that I can access its
properties that will give me the data I need? I figured I could use
FindControl but I need an ID as a parameter. Is there a way to set the
ID for a user control? Or, if it is automatically assigned, is there a
way to retrieve it?

In case you are wondering, unfortunately, I don't think I can use a
repeater because I have read that you can't change the template and each
of my rows may have a different number of controls shown (some may have
extra graphic icons, notes, etc.) and a grid really doesn't work for me
in my particular situation.

Any suggestions and/or comments will be greatly appreciated.

Thanks,
Michael Bunger

| [aspngbeta] member Click here to reveal e-mail address = YOUR ID |
http://www.asplists.com/asplists/aspngbeta.asp = JOIN/QUIT
Reply to this message...
 
 
System.Web.UI.Page




Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
 
 Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us - Terms of Use - Privacy Policy - www.dotnet247.com