.NET 2.0 Composite Control properties aren't accessible

Discussion in '.NET' started by jeff_suhr, Sep 15, 2006.

  1. jeff_suhr

    jeff_suhr

    Joined:
    Sep 14, 2006
    Messages:
    5
    I created a .Net 2.0 composite control that is designed to preload a Dropdownlist with data from a webservice call. The 'CreateChildControls()' function executes correctly, and I created a property for the DropDownList to allow access, but the DDL is null when I attempt to access it in another function within the composite control.

    I've tried to reference the DDL directly (from the control's DDL property), and I've tried a Page.FindControl("DDLName"). In both cases, the DDL is null. What am I missing? The main control code snippets are displayed below:

    1) Define Property

    /* AmountDDL */
    [Bindable(true)]
    [Category("Appearance")]
    [DefaultValue("")]
    [Localizable(true)]
    public DropDownList AmountDDL
    {
    get
    {
    DropDownList ddlAmt = (DropDownList)ViewState["AmountDDL"];
    return ((ddlAmt == null) ? null : ddlAmt);
    }
    set
    { ViewState["AmountDDL"] = value; }
    }


    2) Execute 'CreateChildControls()' to build DDL

    protected override void CreateChildControls()
    {
    //Add Amount DDL
    DropDownList AmountDDL = new DropDownList();
    AmountDDL.CssClass = "AmtDDLText";
    AmountDDL.Attributes.Add("onChange", "AmountDDLSetIndex()");
    Controls.Add(new LiteralControl(str4Tab));
    Controls.Add(AmountDDL);

    //Add Submit Button
    Button SubmitButton = new Button();
    SubmitButton.CssClass = "SubmitButton";
    SubmitButton.Text = "Add To Cart";
    SubmitButton.Attributes.Add("onClick", "SubmitButton_Click");
    SubmitButton.Attributes.Add("onmouseover", "this.className='ButtonsOver'");
    SubmitButton.Attributes.Add("onmouseout", "this.className='SubmitButton'");
    Controls.Add(SubmitButton);

    base.CreateChildControls();

    //Call function to load DDL
    SetAmountDDL();

    }


    3) Execute the function to load the newly created DDL

    protected void SetAmountDDL()
    {
    Page objPage = this.Page;

    //Note: I've built a DataTable (objDT-code to this build is hidden)
    DataView objDV = new DataView(objDT);

    if (objPage != null)
    {
    DropDownList objDDL = (DropDownList)objPage.FindControl("AmountDDL");
    if (objDDL != null)
    {
    objDDL .DataSource = objDV;
    objDDL .DataTextField = "Denomination";
    objDDL .DataValueField = "SKU_and_Denomination";
    objDDL .DataBind();
    }
    }
    }
     
    jeff_suhr, Sep 15, 2006
    #1
    1. Advertising

  2. jeff_suhr

    jeff_suhr

    Joined:
    Sep 14, 2006
    Messages:
    5
    I found a snippet of code in this article that resolved the issue:

    'Creating Composite Server Controls in ASP.NET 2.0 - Make new components from old'

    By: Wes Weeks
    Jan. 25, 2006 08:30 PM

    The problem was that I was defining the DropDownList control in the Properties region, when they should have been defined within the class itself, as follows:

    [DefaultProperty("Text")]
    [ToolboxData("<{0}:Sku_Selector runat=server></{0}:Sku_Selector>")]
    public class Sku_Selector : CompositeControl, INamingContainer
    {

    #region DECLARATIONS

    //Every .NET control that your composite control uses must be
    defined here 1st--BEFORE the call to CreateChildControls() is made.

    private DropDownList AmountDDL;
    private TextBox QtyTB;
    private Label lblPageErr;
    private Label lblQtyErr;
    private Label labDivError;
    private Label Error_Label_Table;
    private CustomValidator AddToCartValidator;
    private Button SubmitButton;
    private HiddenField hdnSelectedAmountOpt;

    #endregion

    Only then, is it possible for ancillary functions within the composite control to reference the controls that are instantiated in CreateChildControls().
     
    jeff_suhr, Sep 22, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sleigh
    Replies:
    1
    Views:
    2,741
    sleigh
    Feb 12, 2004
  2. BenB
    Replies:
    1
    Views:
    361
    =?Utf-8?B?UHJha2FzaC5ORVQ=?=
    Jan 4, 2005
  3. laredotornado
    Replies:
    1
    Views:
    1,354
    markspace
    Dec 29, 2009
  4. Chad
    Replies:
    0
    Views:
    255
  5. Ufit
    Replies:
    0
    Views:
    154
Loading...

Share This Page