.NETGURU
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




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification