Custom Control - DropDownList losing Items

F

fwirtanen

I am building a custom composite control consisting of two drop downs,
with parent/child dependancy.

The child dropdownlist is updated through client callback when the
parent index changes.

An 'All' list item is added to the child dropdown during the
CreateControlHeirarchy(bool useDataSource) method.

Somewhere between the CreateControlHeirarchy method executing and the
Render event the added ListItem disappears. This only happens on the
first page load. Refreshing the browser or doing a postback, everything
works fine.

I put breakpoints all over the code, nothing that touches ddlChild is
executing between the OnDataBinding & Render events.

I must be missing something.

Anyone have any ideas?

Thx.

Here is the relevant sections of the code

protected void CreateControlHeirarchy(bool useDataSource)
{
ddl1 = new DropDownList();
ddlChild = new DropDownList();

ddlParent.ID = this.UniqueID + "parent";
ddlChild.ID = this.UniqueID + "child";

//viewstate gets out of sync on child due to callbacks, so
don't use it
ddlChild.EnableViewState = false;

ddlParent.Attributes.Add("onchange",
"UpdateChild(this.options[this.selectedIndex].value,'parent');");
if (useDataSource)
{
//bind datasource to Parent - maintained by viewstate
ddlParent.DataSource = ParentDataSource;
ddlParent.DataTextField = ParentDataTextField;
ddlParent.DataValueField = ParentDataValueField;

if (objParentList != null)
ddlParent.DataBind();
}

//always reload & bind child, viewstate is disabled
if (ChildDataSource != null)
{
ddlChild.DataSource = ChildDataSource;
ddlChild.DataTextField = ChildDataTextField;
ddlChild.DataValueField = ChildDataValueField;
ddlChild.DataBind();

}

//**** ListItem added here - stepping through code in debug shows
the ListItem has been added ****

if (hasAllItem)
{
ddlChild.Items.Insert(0, new
ListItem(AllChildItemText,AllChildItemValue));
}

Label lblParent = new Label();
lblParent.Text = ParentTitle;
lblParent.EnableViewState = false;
lblParent.Width = labelWidth;

Label lblChild = new Label();
lblChild.Text = ChildTitle;
lblChild.EnableViewState = false;
lblChild.Width = labelWidth;

ddlParent.Width = dropDownWidth;
ddlChild.Width = dropDownWidth;

Controls.Add(lblParent);
Controls.Add(ddlParent);
Controls.Add(new LiteralControl("<br>"));
Controls.Add(lblChild);
Controls.Add(ddlChild);

RegisterClientSideScripts();
}

protected override void Render(HtmlTextWriter writer)
{
//******* Watch on ddlChild.Items shows added ListItem has
disappeared ********

EnsureChildControls();
base.Render(writer);
}

protected override void CreateChildControls()
{
//This event doesn't fire on first page load
Controls.Clear();
CreateControlHeirarchy(false);

}

protected override void OnDataBinding(EventArgs e)
{
base.OnDataBinding(e);

Controls.Clear();
CreateControlHeirarchy(true);
if (HasChildViewState)
{
ClearChildViewState();
}
ChildControlsCreated = true;
if (!IsTrackingViewState)
{
TrackViewState();
}

//******* Watch on ddlChild.Items shows added ListItem is still here
when this event exits********

}
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top