Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Professional
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
ASP.NET 2.0 Professional If you are an experienced ASP.NET programmer, this is the forum for your 2.0 questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Professional section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old October 1st, 2007, 10:27 PM
Authorized User
 
Join Date: Jun 2005
Location: Nashville, TN, USA.
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Jayahar Send a message via Yahoo to Jayahar
Default Dynamic User Controls....

Hi,

    I've got a requirement, where I'm forced to add the same user control(.ascx file) numerous times in a page(.aspx file) dynamicaly(i.e., on click of a button at runtime). Could any one suggest how that can be done.....

Thanks in advance....

Jay
__________________
Regards,
Jayahar Aruchamy.
Life is all about expecting the unexpected.
  #2 (permalink)  
Old October 2nd, 2007, 01:39 AM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Hi Jay,

You can use LoadControl to load a control and then add it to another control, like a Panel or a PlaceHolder. Here's a quick example:

for (int i = 0; i < (numberOfUserInfoControls); i++)
{
  UserInfo userInfo = (UserInfo)LoadControl("~/Controls/UserInfo.ascx");
  userInfo.ID = "UserInfo" + i.ToString();
  myPlaceHolder.Controls.Add(userInfo);
}

In this example, UserInfo is the User Control that is added to a PlaceHolder called myPlaceHolder.

You may need a Control reference in the markup to access properties of your control:

<%@ Register src="Controls/UserInfo.ascx" tagname="UserInfo" tagprefix="uc1" %>

Hope this helps,

Imar

---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
  #3 (permalink)  
Old October 4th, 2007, 02:41 PM
Authorized User
 
Join Date: Mar 2004
Location: , , .
Posts: 74
Thanks: 0
Thanked 0 Times in 0 Posts
Default

the above solution might work for web app project but not for web site if you r working on VS 2005 and you multiple folders.

so you need to have a base control in AppCode folder and the user control needs to inherit from that control. when loading the user control dynamically, you can cast to that base control which is visible to every one in that web site.
  #4 (permalink)  
Old October 4th, 2007, 03:04 PM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Quote:
quote:Originally posted by everestthe above solution might work for web app project but not for web site if you r working on VS 2005 and you multiple folders.
Heuh? Says who?

The example I posted almost works for a web site project. All you need to do to is wrap the control class in a namespace and refer to it by its full name in the code that adds the control. Alternatively, you can refer to the UC by its default ASP namespace name.

Can you give an example where this doesn't work?

Imar

---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
  #5 (permalink)  
Old October 10th, 2007, 08:55 AM
Authorized User
 
Join Date: Jun 2005
Location: Nashville, TN, USA.
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Jayahar Send a message via Yahoo to Jayahar
Default

Thanks Imar.

Regards,
Jayahar Aruchamy.
Life is all about expecting the unexpected.
  #6 (permalink)  
Old October 10th, 2007, 03:40 PM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

You;'re welcome.

Everest: would you like to comment on your remarks about this solution not working in a Web Folder Project?

Imar


---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
  #7 (permalink)  
Old October 11th, 2007, 11:50 PM
Authorized User
 
Join Date: Mar 2004
Location: , , .
Posts: 74
Thanks: 0
Thanked 0 Times in 0 Posts
Default

i had a MyControls folder and i put all the user controls in it. In one of the pages, I had to load user controls dynamically based on a selection from a dropdown list.

 if i used the approach that you mentioned on your first reply, that did not work. I used to do the same way that you said when i was using .net 1.1 with web app project. The site i was building with the dynamical controls was WEB SITE.
  #8 (permalink)  
Old October 11th, 2007, 11:54 PM
Authorized User
 
Join Date: Mar 2004
Location: , , .
Posts: 74
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I don't see any namespace in website project. do you add manually or is there other way?

thanks
  #9 (permalink)  
Old October 12th, 2007, 01:27 AM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

You can simply wrap the control's class in a namespace:

namespace WhatEver
{
  public class MyControl : UserControl
}

should do the trick. It also allows you to use my approach in a web folder project.

Imar

---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
User controls' content: Chapter 2 User Controls AGS BOOK: Professional ASP.NET 2.0 Server Control and Component Development ISBN: 978-0-471-79350-2 10 July 26th, 2007 05:36 AM
handling dynamic load of user controls spacky001 ASP.NET 1.0 and 1.1 Basics 1 December 16th, 2006 06:14 PM
Dynamic .NET Controls. rupen ASP.NET 1.0 and 1.1 Basics 3 February 21st, 2006 05:30 AM
Dynamic Controls [xx(] koneruvijay VS.NET 2002/2003 0 November 12th, 2003 12:02 AM
Dynamic Controls tommyuk VB How-To 2 June 26th, 2003 12:54 PM



All times are GMT -4. The time now is 10:08 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.