473,404 Members | 2,137 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,404 software developers and data experts.

Dynamic Loading of a User Control

Hi,

I've create a usercontrol with a Calendar Control and a Textbox. The
SelectionChanged event of the calendar populates the textbox with the
selected date.

Also, I've created an aspx page with an ASP Table on the page.

Finally, I have class called PageControls.cs, where I generate cells for the
table on the aspx page.

These cells contain different html and asp controls. Now, I want to add my
usercontrol to one of the cells as well. But I keep on getting errors in
doing so. Is there someone that can please assist?

Here are code extracts to do the above mentioned.

=========================================

//file: MyUserControlCollection.ascx.cs

=========================================

public class DateBox : System.Web.UI.UserControl

{

protected System.Web.UI.WebControls.Calendar oCalendar;

protected System.Web.UI.WebControls.TextBox oDateBox;

private void Page_Load(object sender, System.EventArgs e)

{

}

#region Web Form Designer generated code

override protected void OnInit(EventArgs e)

{

InitializeComponent();

base.OnInit(e);

}

private void InitializeComponent()

{

this.oCalendar.SelectionChanged += new
System.EventHandler(this.oCalendar_SelectionChange d);

}

#endregion

private void oCalendar_SelectionChanged(object sender,
System.EventArgs e)

{

oDateBox.Text = oCalendar.SelectedDate.ToString("dd
MMMM yyyy");

}

}

=======================

//file: TestForm.aspx

=======================

public class TestForm : System.Web.UI.Page

{

protected System.Web.UI.WebControls.Table Table1;

private void Page_Load(object sender, System.EventArgs e)

{

MyUtils.ControlUtils oUtils = new
MyUtils.ControlUtils();

oUtils.PopulateTable(ref Table1);

}

#region Web Form Designer generated code

override protected void OnInit(EventArgs e)

{

InitializeComponent();

base.OnInit(e);

}

private void InitializeComponent()

{

this.Load += new System.EventHandler(this.Page_Load);

}

#endregion

}



=====================

//file: TestForm.cs

=====================

public class ControlUtils

{

public ControlUtils()

{

}

public void PopulateTable(ref System.Web.UI.WebControls.Table
oFormTable)

{

//Variables

TableRow oRow = new TableRow();

TableCell oCell = new TableCell();

Control oControl = new Control();

// Create DateBox object

MyUserControlCollection.DateBox oControl = new
MyUserControlCollection.DateBox();

oControl.EnableViewState=true;

oControl.Visible=true;

// Add Control To Cell

oCell.Width=Unit.Percentage(100);

oCell.Controls.Add(oControl);

//Add Cell to Row object

oRow.Cells.Add(oCell);

//Add Row to tabe from aspx Form

oFormTable.Rows.Add(oRow);

}

}
Regards
Danny
Dec 23 '05 #1
1 1108
you can use place holder to load your control in it..
--
regards,
"Danny" wrote:
Hi,

I've create a usercontrol with a Calendar Control and a Textbox. The
SelectionChanged event of the calendar populates the textbox with the
selected date.

Also, I've created an aspx page with an ASP Table on the page.

Finally, I have class called PageControls.cs, where I generate cells for the
table on the aspx page.

These cells contain different html and asp controls. Now, I want to add my
usercontrol to one of the cells as well. But I keep on getting errors in
doing so. Is there someone that can please assist?

Here are code extracts to do the above mentioned.

=========================================

//file: MyUserControlCollection.ascx.cs

=========================================

public class DateBox : System.Web.UI.UserControl

{

protected System.Web.UI.WebControls.Calendar oCalendar;

protected System.Web.UI.WebControls.TextBox oDateBox;

private void Page_Load(object sender, System.EventArgs e)

{

}

#region Web Form Designer generated code

override protected void OnInit(EventArgs e)

{

InitializeComponent();

base.OnInit(e);

}

private void InitializeComponent()

{

this.oCalendar.SelectionChanged += new
System.EventHandler(this.oCalendar_SelectionChange d);

}

#endregion

private void oCalendar_SelectionChanged(object sender,
System.EventArgs e)

{

oDateBox.Text = oCalendar.SelectedDate.ToString("dd
MMMM yyyy");

}

}

=======================

//file: TestForm.aspx

=======================

public class TestForm : System.Web.UI.Page

{

protected System.Web.UI.WebControls.Table Table1;

private void Page_Load(object sender, System.EventArgs e)

{

MyUtils.ControlUtils oUtils = new
MyUtils.ControlUtils();

oUtils.PopulateTable(ref Table1);

}

#region Web Form Designer generated code

override protected void OnInit(EventArgs e)

{

InitializeComponent();

base.OnInit(e);

}

private void InitializeComponent()

{

this.Load += new System.EventHandler(this.Page_Load);

}

#endregion

}



=====================

//file: TestForm.cs

=====================

public class ControlUtils

{

public ControlUtils()

{

}

public void PopulateTable(ref System.Web.UI.WebControls.Table
oFormTable)

{

//Variables

TableRow oRow = new TableRow();

TableCell oCell = new TableCell();

Control oControl = new Control();

// Create DateBox object

MyUserControlCollection.DateBox oControl = new
MyUserControlCollection.DateBox();

oControl.EnableViewState=true;

oControl.Visible=true;

// Add Control To Cell

oCell.Width=Unit.Percentage(100);

oCell.Controls.Add(oControl);

//Add Cell to Row object

oRow.Cells.Add(oCell);

//Add Row to tabe from aspx Form

oFormTable.Rows.Add(oRow);

}

}
Regards
Danny

Dec 24 '05 #2

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

Similar topics

4
by: Yan Wang | last post by:
Hi!: I encounter this problem when I do some tests: I have one user control with datalist in it. The ID for datalist is "dlTest". Then in this control code behind class, I declare a protected...
1
by: Shourie | last post by:
I've noticed that none of the child controls events are firing for the first time from the dynamic user control. Here is the event cycle. 1) MainPage_load 2) User control1_Load user clicks a...
0
by: Phl | last post by:
Hi, I am trying to create an webform which loads usercontrols dyanamically. I know exactly what to load for some of these controls but for some, I dont want to load it until the user has press a...
7
by: John | last post by:
Hi all, I need finality on this once and for all please. I have a main page which contains a couple of placeholders and within these placeholders, depending on what the user presses, I load...
0
by: Prodip K Saha | last post by:
Many Programmers stay away from loading User Control dynamically on the Web Form because of the complexity associated with it's state management and event handling. It is easy to load a User...
1
by: Stanley Cheung | last post by:
Dear all, how can I pass a parameter to dynamic user control? As I know we can pass parameter into traditional user control. e.g: add pageid into user control and write a property from user...
2
by: Dave A | last post by:
I just don't get this... If I need to dynamically load controls into a web page I simply need to go PlaceHolder1.Controls.Add(new Button()); or similar. However when I need to dynamically...
5
by: Gui | last post by:
Hi, I'm working in C# .net 2005 with Ajax. I have a page that loads dynamic user controls depending on the scenario. In those user controls, I create dynamic linkbuttons. The user controls are...
9
by: pbd22 | last post by:
Hi. This is just a disaster management question. I am using XMLHTTP for the dynamic loading of content in a very crucial area of my web site. Same as an IFrame, but using XMLHTTP and a DIV. I...
4
Frinavale
by: Frinavale | last post by:
Introduction Sometimes, when developing web applications, we need to be able to dynamically load controls based on user selections. The following article describes a simple scenario where TextBox...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.