I have a web user control (OrderDefinition.ascx) which has a dropdownlist (ddl_CustomerCode), which is populated by web user control’s page_load function
public void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ddl_CustomerCode.DataSource = OrderDefinitionData.GetCustomers();
ddl_CustomerCode.DataTextField = "CustomerCode";
ddl_CustomerCode.DataValueField = "CustomerName";
ddl_CustomerCode.DataBind();
}
}
In my default.aspx page I dynmically add this web user control
Control x = LoadControl("Controls/OrderDefinition.ascx");
Panel1.Controls.Add(x);
I want to pass parameter to this web user control from my default.aspx, and I come up with the idea of using a session or viewstate or cache to use. So now my default.aspx looks like
Session["myParam"] = "customerNo1";
Control x = LoadControl("Controls/OrderDefinition.ascx");
Panel1.Controls.Add(x);
And my Page_Load will look like
public void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string myParam = Session["myParam"];
ddl_CustomerCode.DataSource = OrderDefinitionData.GetCustomers(myParam);
ddl_CustomerCode.DataTextField = "CustomerCode";
ddl_CustomerCode.DataValueField = "CustomerName";
ddl_CustomerCode.DataBind();
}
}
The problem is I am not sure about the way I code above, Should I trust this architecture? Please clarify me if this is the correct way to pass a parameter to a web user control.
As other have suggested you can do this in your ascx:
Then do this in your ASPX:
Note the casting to your TYPE of web control when you instantiate your web control through the LoadControl function – its important to do this cast so that your properties and methods are accessible to you.