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();
}
}
}
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();
}
}
}