473,320 Members | 1,979 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Dynamically load user control?

Hi, All
I need to dynamically load a user control and change some properties on
the user control, and display in the aspx page. Unfortunately I failed,
nothing appears.

Please help me look at, thanks.

Comment c = (Comment) Page.LoadControl("Comment.ascx");

Label time = new Label();

time.Text = "5/11/2005";

Label comment = new Label();

comment.Text = "Test ;)";

LinkButton lb = new LinkButton();

lb.Text = "test button";
c.DateTime = time;

c.Comments = comment;

c.Who = lb;

CommentHolder.Controls.Add(c);


Nov 19 '05 #1
1 1508
I'd love to see how comments.aspx.cs is written...

sounds like you have:

public class Comment : UserControl{

public Label DateTime;
void page_load(){}
}

and in the ascx part you have
<asp:Label id="DateTime" runat="server" />


you need to understand control instantiation. <asp:label id="dateTime"
runat="server" /> creates a new instance...

when you do:

c.DateTime = new Label(); you are creating a new instance, which overwrites
the ascx one and never gets rendered.

instead you should be doing osmethin glike:
public class Comment : UserControl{

protected Label dt;
public string DateTime{
get { return dt.Text;}
set { dt.Text = value;}
}
void page_load(){}
}
....
<asp:Label id="dt" runat="server" />

and simply do

c.DateTime = "5/11/2005"; //should be a DateTime instead of a string??

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"Green" <ma*********@yahoo.com> wrote in message
news:eg**************@TK2MSFTNGP15.phx.gbl...
Hi, All
I need to dynamically load a user control and change some properties on
the user control, and display in the aspx page. Unfortunately I failed,
nothing appears.

Please help me look at, thanks.

Comment c = (Comment) Page.LoadControl("Comment.ascx");

Label time = new Label();

time.Text = "5/11/2005";

Label comment = new Label();

comment.Text = "Test ;)";

LinkButton lb = new LinkButton();

lb.Text = "test button";
c.DateTime = time;

c.Comments = comment;

c.Who = lb;

CommentHolder.Controls.Add(c);

Nov 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Tim T | last post by:
Hi, I have the need to use dynamically loaded user controls in a webform page. I have the controls loading dynamically, and that part works fine. this is the code used in a webform to dynamically...
8
by: Donald Xie | last post by:
Hi, I noticed an interesting effect when working with controls that are dynamically loaded. For instance, on a web form with a PlaceHolder control named ImageHolder, I dynamically add an image...
1
by: Kamal Jeet Singh | last post by:
Hi Friends !! I am have facing problem in controlling the dynamically created controls on web page. The problem Scenario is Scenario:- My requirement is to load the web user controls on the...
1
by: Kamal Jeet Singh | last post by:
Hi Friends !! I am facing problem in controlling the dynamically created controls on web page. The problem Scenario is Scenario:- My requirement is to load the web user controls on the web...
4
by: Harry | last post by:
Hello, I have a page with a RadioButtonList and a PlaceHolder control. The RadioButtonList's AutoPostBack attribute is set to TRUE and its SelectedIndexChanged event loads one of three...
1
by: Reza Nabi | last post by:
Bakground: I have a webform (LoadCtl.aspx) which loads the user control to a placeholder dynamically based on the ctlName querystring passed in the URL. Webform (LoadCtl.aspx) also passes a...
6
by: Steve Booth | last post by:
I have a web form with a button and a placeholder, the button adds a user control to the placeholder (and removes any existing controls). The user control contains a single button. I have done all...
2
by: A.Wussow | last post by:
Hi Everybody, i want to load dynamically content from some user controls (with forms, or some data-controls) using atlas. So i use an UpdatePanel for loading the user control into a placeholder....
2
by: Nick | last post by:
I have two navigation user controls and based on a query string will load one or the other. On the page that will load the user control I cannot do the following Dim uc As FlashNavigation (The...
6
by: | last post by:
I have made some user controls with custom properties. I can set those properties on instances of my user controls, and I have programmed my user control to do useful visual things in response to...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.