DropDownList inside a DetailsView, how to databind

May 12, 2009 at 9:10 AMJeffery

It's been far to long sense I blogged. Sorry about that.

An application I'm programming has a DetailsView with a DropDownList inside. I had a lot of trouble getting this to work so I thought that I would share this information to make it easier for someone else.

This may not be the best way but it seams to work ok. A lot of articles I've read about this use some sort of datasource in the aspx page. I thought the idea was to separate presentation from the business logic so I try to stay away from that.

Basically you have a DetailsView like this (I've removed a lot of extra code and left the important code to make it less confusing):

<asp:DetailsView ID="dvItem" runat="server" CssClass="detailsView" DataKeyNames="ItemIdPkey"  onDataBound="dvItem_DataBound">
    <HeaderTemplate>
            Title
    </HeaderTemplate>
        <Fields>
            <asp:TemplateField HeaderText="Category">
                <ItemTemplate>
                    <asp:Label ID="lblCategory" runat="server" Text='<%# Bind("ItemCategoryName") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:DropDownList ID="ddlCategory" runat="server">
                    </asp:DropDownList>
                </EditItemTemplate>
                <InsertItemTemplate>
                    <asp:DropDownList ID="ddlCategory" runat="server">
                    </asp:DropDownList>
                </InsertItemTemplate>
            </asp:TemplateField>
</Fields>
</asp:DetailsView

 The necessary parts are the onDataBound event and having the DropDownList in an Edit or insert template.

Of course you would have all the other events that go with editing, inserting, mode changing, etc...

Now for the C# code:

protected void dvItem_DataBound(object sender, EventArgs e)
{
    if (this.dvItem.CurrentMode == DetailsViewMode.Edit)
    {
        DropDownList ddlCategory = (DropDownList)this.dvItem.FindControl("ddlCategory");
        if (ddlCategory != null)
        {
             DataTable dt = ItemCategory.ItemCategorySelectAll().Tables[0];
             ddlCategory.DataSource = dt;
             ddlCategory.DataTextField = "itemCategoryName";
             ddlCategory.DataValueField = "itemCategoryId_Pkey";
             ddlCategory.DataBind();

             WishListItem li = (WishListItem)this.dvItem.DataItem;
             ddlCategory.Items.FindByValue(li.ItemCategoryFkey.ToString()).Selected = true;
           }
     }

}

 This is only showing the edit mode code but the Insert code would be about the same except for the finding of the value to make it selected. The last two lines should be deleted.

 I also found that when accessing the DataItem you need to use the type of datasourse that you gave it. In this I have a class WishListItem that was the datasourse. If you used a DataTable or DataSet then you would assign that to the DataItem. Hope that made sense.

If you have any questions please let me know.

Thanks,

Jeffery

 

 

Posted in: ASP.NET 2.0+ | C# | Programming | DetailsView

Tags:

Comments are closed