Dynamic Loading of a User Control

G

Guest

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_SelectionChanged);



}

#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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top