views:

477

answers:

2

Hello,

I'm having problems trying to populate a dropdownlist from the database. When I'm trying to set the datasource I can't find the dropdown control, it's in a DetailsView so I think it might have something to do with it only being created when it's in edit mode. It still says it's in current mode when I'm editing though, so not sure what's going on there.

Here's the code from the aspx file:

<asp:DetailsView id="DetailsView1" runat="server" AutoGenerateRows="false" DataSourceID="myMySqlDataSrc"  DataKeyNames="id" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" AutoGenerateInsertButton="False" >
         <Fields>
            <snip>    
            <asp:TemplateField HeaderText="Region">
                <ItemTemplate><%# Eval("region_name") %></ItemTemplate>
                <EditItemTemplate>
                    <asp:DropDownList ID="RegionDropdownList" runat="server" SelectedValue='<%# Bind("region_id")%>'>

                    </asp:DropDownList>
                </EditItemTemplate>
            </asp:TemplateField>        
         </Fields>
         </asp:DetailsView>

And this is from the code behind:

ArrayList regionsList = BPBusiness.getRegions();

            if (DetailsView1.CurrentMode == DetailsViewMode.Edit)
            {
                DropDownList ddlRegions = (DropDownList)DetailsView1.FindControl("RegionDropdownList");
                if (ddlRegions != null)
                {
                    ddlRegions.DataSource = regionsList;
                    ddlRegions.DataBind();
                }
            }

It's .net 2.0 if that helps.

Thanks,

Annelie

A: 

try doing it in the itemcreated method

protected void DetailsView1_ItemCreated(object sender, EventArgs e)
{
        ArrayList regionsList = BPBusiness.getRegions();

            if (DetailsView1.CurrentMode == DetailsViewMode.Edit)
            {
                DropDownList ddlRegions = (DropDownList)DetailsView1.FindControl("RegionDropdownList");
                if (ddlRegions != null)
                {
                    ddlRegions.DataSource = regionsList;
                    ddlRegions.DataBind();
                }
            }


}

remember to set onitemcreated="DetailsView1_ItemCreated"

Glennular
Thanks, this does find the control, however it fails on ddlRegions.DataBind() throwing the error "Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.".
annelie
Use DataBinder.Eval(Container.DataItem,"region_name") Method Instead of Eval(“region_name”)
Glennular
A: 

If it isn't already, place the sample from your code behind inside the DetailsView1_ModeChanged or DetailsView1_DataBound method. If it is in the DetailsView1_ModeChanging method, the mode has not actually changed yet.

EDIT: Also, make sure you set up the DataTextField and DataValueField like so:

DropDownList1.DataTextField = "TextFieldName";
DropDownList1.DataValueField = "ValueFieldName";

Also remove the SelectedValue bind; it does nothing except throw errors.

EDIT 2: If you really need to select a particular value of the dropdownlist when it first is databind, you could do something like this:

if(DropDownList1.Items.Contains(DropDownList1.Items.FindByValue("Value")))
{
    DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByValue("Value));
}
Matthew Jones
I tried to place it in the DetailsView_DataBound method, it throws an error on my SelectedValue, but if I remove that the list gets populated. Although only with System.Collections.ArrayList instead of the values I wanted so I guess I need to set the text and value properties somehow! :) I'll try to set those then test it again with the SelectedValue in there.
annelie
@annelie See my edit :).
Matthew Jones
Thanks, I'll try that! I had an arraylist of arraylists so just need to change that first. Then is should hopefully work! :)
annelie
Right, works fine, thank you so much! For some reason it's not posting the value when I make an update so it gets set to null, but that's a whole different story I guess. Might post another question if I don't sort it out myself. :)
annelie
Actually, could the reason it's no longer updating the region be that I removed the SelectedValue='<%# Bind("region_id")%>' from the dropdownlist so it no longer is associated with the region_id and therefore does not know how to update it?
annelie