Asked by:
UserControl ? Dynamically create Usercontol AND get variable value from usercontrol to another user control

Question
-
User-1433691514 posted
Hi Guys,
I quite new to Asp. net C#, but i need to know how to handle usercontrol.
Introduction
MyUserControl1 -> connect to db and display values in gridview
MyUserContol2 -> Need to use the variable in MyUserControl1Questions
1) I need to dynamically create MyUserControl1 IN MyUserConrtol1 and load it to my MasterPage (Report.aspx).
MyUserControl1 will create the no. of usercontrol base on the record rows retrieved.
Is ther another way to do it?2) Next in MyUserControl2 would need to use a variable (uc1Variable) in MyUserControl1
is it possible?If there is any articles or links please post them.
Thank you guys. I really appreciate your assistance.
Saturday, June 21, 2008 12:24 AM
All replies
-
User-1136466523 posted
Hi,
I need to dynamically create MyUserControl1 IN MyUserConrtol1Not sure what you mean by that. If you do that, it will lead to an infinite loop definitely. As you mentioned, you want to get the variable from usercontrol1 while coding in usercontrol2, actually you can create an object instance of usercontrol1 and initialize it, and then retrieve the corresponding variable from that object.
Besides, you mentioned MyUserControl1 will create the no. of usercontrol base on the record rows retrieved. You can get the row number first, and then make a loop against that.
For more information on user controls, you can try the following stepbystep guide.
http://www.dotnetjunkies.com/quickstart/aspplus/doc/quickstart.aspx
Thanks.
Tuesday, June 24, 2008 11:19 PM -
User481221548 posted
Hello stevensim83
1. that depents on what the Usercontrols have to do.
for example you can use a Repeater instead of UserControl1, who knows.2. Yes, define a property and access to it
@Nai-Dong Jin
Infinite loop - why?
Dont agree with you.Wednesday, June 25, 2008 3:45 AM -
User-1136466523 posted
Hi,
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p> </o:p>I need to dynamically create MyUserControl1 IN MyUserConrtol1<o:p> </o:p>Well, here’s my understanding towards his problem:
<o:p> </o:p>I think user control cannot contain themselves. For example, just drag a user control onto the user control form, an error message “Cycle detected …” should be exited.
If i've misundertood your problem, please feel free to reply.
<o:p> </o:p>Thanks.
Thursday, June 26, 2008 5:47 AM -
User481221548 posted
Hi Nai-Dong Jin
Your right in the "out-of-the-box"-case.
That give your written error and at runtime with declared (ASPX) Code an similar error.
If you load the control dynamically in itself, that results - as you thought - in a stack overflow error.It works if you manually check the Cycle, for example like:
Or over HttpContext.Current.Items[.... like:
(Page_Load of the UserControl)
protected void Page_Load(object sender, EventArgs e)
{
object o = HttpContext.Current.Items["loaded"];
bool flag;
if(o != null) {
flag = (bool)o;
} else {
flag = false;
}if (!flag)
{
Control c = this.LoadControl("~/UserControls/TestControl.ascx");
this.Controls.Add(c);
HttpContext.Current.Items["loaded"] = true;
}
}Keywords: 44 Words
Thursday, June 26, 2008 10:24 AM -
User-573318918 posted
hi,just try this one,it may help you http://aspalliance.com/67_Accessing_Page_Properties
Tuesday, January 26, 2010 5:44 AM