Of course you can use a dropdown list in a datalist, you just can't do
it without writing a little code.

All you need to do is put a little code in the ItemDataBound event of the list.

Something like this...

 private void DataList1_ItemDataBound(object sender,
System.Web.UI.WebControls.DataListItemEventArgs e)
 {
       DropDownList ddl = null;
       DataRowView drv;
       if (e.Item.ItemType == ListItemType.Item ||e.Item.ItemType ==
ListItemType.AlternatingItem)
        {
                drv = (DataRowView)e.Item.DataItem;
                ddl = (DropDownList)e.Item.FindControl("ddlStates");
                BindStateDropDown(ddl);
                //Set current value
                ddl.SelectedValue = drv["ID"] + "";

        }
 }

private void BindStateDropDown(DropDownList ddl){

       ddl.DataSource = FunctionThatReturnsDataTableWithStates();
       ddl.DataTextField = "StateName";
       ddl.DataValueField = "ID";
       dll.DataBind();


}

Just remember to set the ItemDataBound handler, usually in InitializeComponent()

this.DataList1.ItemDataBound += new
System.Web.UI.WebControls.DataListItemEventHandler(this.DataList1_ItemDataBound);

And when the grid binds, you'll fill the dropdown list.

On 5/11/05, cylabii <[EMAIL PROTECTED]> wrote:
> How do you dynamically populate either a <select id="states"
> runat="server"> or <asp:DropDownList id="states"> control
> in the EditItemTemplate of a DataList control?
> 
> Basically, the user clicks an "Edit" button which fires
> the Sub MyDataList_Edit() event for a datalist. Then
> I want to allow the user to select his current address state
> from a drop-down list which defaults to his current state.
> 
> I read somewhere that the <select> and <asp:DropDownList>
> controls are not supported in the DataList templates while
> simple contols like textboxes are.
> 
> With a textbox control you use the
> <input id="schCity" type="text" value='<%# DataBinder.Eval
> (Container.DataItem,"city") %>' size="51" maxlength="50"
> runat="server" /></td> syntax to populate the control from
> a data table.
> 
> How do you accomplish the same thing using either the <Select>
> or <asp:DropDownList> controls?
> 
> Thanks,
> Dallas Martin
> 
> 
> Yahoo! Groups Links
> 
> 
> 
> 
> 


-- 
Dean Fiala
Very Practical Software, Inc
http://www.vpsw.com


 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/AspNetAnyQuestionIsOk/

<*> To unsubscribe from this group, send an email to:
    [EMAIL PROTECTED]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 


Reply via email to