.NET 2.0 Composite Control properties aren't accessible

Joined
Sep 14, 2006
Messages
5
Reaction score
0
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();
}
}
}
 
Joined
Sep 14, 2006
Messages
5
Reaction score
0
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().
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top