I need to know how to populate a dropdownlist and how do i put a title/heading above my listbox? i've been trying and my listbox heading just keep sitting next to listbox.. Ooh! almost forgot how to insert groupview

Dani AI

Generated

Quick practical guidance for the exact scenario described (parent list with “Bread/Milk”, child list showing milk types). Good call from to focus on one problem at a time; ’s suggestion (AJAX Control Toolkit) is one viable option — below are two straightforward options plus a fast fix for your heading positioning.

Server-side (simple, zero-extra-libs): put the parent DropDownList in markup with AutoPostBack="true" and handle SelectedIndexChanged in the code‑behind. Only bind data on first load (if (!IsPostBack)) so the event will fire reliably; wrap the child list in an UpdatePanel if you want a partial refresh (no full page flash). Example markup + C# handler:

<asp:DropDownList ID="ddlCategory" runat="server" AutoPostBack="true"
    OnSelectedIndexChanged="ddlCategory_SelectedIndexChanged">
  <asp:ListItem Value="bread">Bread</asp:ListItem>
  <asp:ListItem Value="milk">Milk</asp:ListItem>
</asp:DropDownList>

<asp:DropDownList ID="ddlType" runat="server" />
protected void ddlCategory_SelectedIndexChanged(object sender, EventArgs e)
{
    ddlType.Items.Clear();
    if (ddlCategory.SelectedValue == "milk")
    {
        ddlType.Items.Add(new ListItem("Full cream","full"));
        ddlType.Items.Add(new ListItem("Low fat","low"));
        ddlType.Items.Add(new ListItem("Fat free","fatfree"));
    }
}

Checkpoints: set AutoPostBack="true", don’t re-bind the parent on every postback (use IsPostBack), and use an UpdatePanel/triggers for asynchronous updates if you don’t want a full page postback. (learn.microsoft.com)

AJAX Control Toolkit (recommended if you want a built-in cascading behavior): add a ScriptManager, register the toolkit, and attach a CascadingDropDown extender that calls a web service (ServicePath/ServiceMethod) to populate the dependent list. This avoids manual postbacks and delegates population to a server method. Example extender markup:

<ajaxToolkit:CascadingDropDown ID="ccdType" runat="server"
  TargetControlID="ddlType" ParentControlID="ddlCategory"
  ServicePath="CascadingService.asmx" ServiceMethod="GetItems"
  PromptText="Select..." />

The toolkit requires a ScriptManager and a server method that returns the items. (learn.microsoft.com)

Heading above the listbox: <label> is inline by default, so it will sit beside the control unless you force block layout or wrap elements. Either wrap each label+control in a <div> or set the label to display:block; in CSS, or use <fieldset><legend> for a true group title. Example:

<label for="ddlType" style="display:block;margin-bottom:4px;">Type</label>
<asp:DropDownList ID="ddlType" runat="server" />

For accessibility and proper semantics prefer explicit for labels or fieldset/legend. (developer.mozilla.org)

If “groupview” was meant to indicate a grid/list control (GridView/ListView), use the appropriate server control (GridView) or a grouped ListView — implement grouping in the data source or with a template after you decide which control you want.

Recommended Answers

All 3 Replies

I would say that you have too many questions without providing any detail/sample code, etc...

You should consider dedicating one thread per question.. My suggestion is to start with your first question.

I need to know how to populate a dropdownlist

You can do this from the dropdown list control in your design view by attaching a data source to it, or you can do it from your code behind page say on Page Load. Or you can simply provide the drowndown list with a static list of entries on the control itself in the aspx page.

What do you want to populate this control with? static data, data from a datasource (XML file, database table)?

Static data, let say i have a dropdownlist with bread and milk and when a user select milk the other dropdownlist must populate the types of milk maybe provide the user options like full cream milk, or low fat milk, or fat free milk..

For this type of population it is better to use the cascadingdropdownlist using ajax control tool kit

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.