Frage

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.

War es hilfreich?

Lösung

As other have suggested you can do this in your ascx:

    public string myParam { get; set; }
    public void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ddl_CustomerCode.DataSource = OrderDefinitionData.GetCustomers(myParam);
            ddl_CustomerCode.DataTextField = "CustomerCode";
            ddl_CustomerCode.DataValueField = "CustomerName";                
            ddl_CustomerCode.DataBind();
         }
    }

Then do this in your ASPX:

WebControl1 x = (WebControl1)Page.LoadControl("Controls/OrderDefinition.ascx");
x.myParam = "customerNo1";
Panel1.Controls.Add(x);

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.

Andere Tipps

You should create property(ies) in your user control and pass the value through it.

Just create a public property in your webcontrol like

private int myid=0;

public int MyID
{
    set { myid = value; }
}

then set it on pageLoad of your user control. In your defaultpage.aspx you can access property like

myUserControl.MyId=32;

You can create public methods in the controls, and access them through the pages, simple. or also you can set the properties, you have both options.

Using Session like that makes it extremely difficult to encapsulate your control properly and it is not very intuitive to use, since the Session key will not visible appear on the control (or through intellisense for that sake). Use parameters on the control as others here suggest.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top