C# .NET - how to pass parameters to a user Control?

Asked By Johan Ee on 10-Jan-06 07:48 AM
Hi, I have a User Control contained in a normal aspx-page. I want to pass parameters to this UserControl, depending on some events on the aspx page. How can I do this in the best way?

thnx!
Johan

Easiest way is to create public fields - Asked By Peter Bromberg on 10-Jan-06 08:41 AM

in the UserControl. These can be populated with dynamic loading of the usercontrol, e.g. here is an example of a usercontrol with a string Message Field:

PagedData.Header h=(PagedData.Header)Page.LoadControl("Header.ascx");
h.Message="TESTING";
this.PlaceHolder1.Controls.Add(h);


in the control, it has a label:

public class Header : System.Web.UI.UserControl
	{
		protected System.Web.UI.WebControls.Label lblMessage;
		public string Message;

		private void Page_Load(object sender, System.EventArgs e)
		{
			this.lblMessage.Text=this.Message ;
		}

ok thnks - Asked By Johan Ee on 10-Jan-06 08:49 AM

seems like a good approach