I thought I'd save myself some time by throwing down a GridView in an admin tool I was working on. This particular page is for editing Categories that “widgets” can then be assigned to. For organizational purposes, categories can (optionally) also be grouped (assigned to a CategoryGroup). There were already existing business logic and objects, so I decided to use an ObjectDataSource (rather than the SqlDataSource that I saw in many examples). It all seemed to come together pretty well, and I even put a DropDownList in the edit template to choose the CategoryGroup.
The problem is that the value of the dropdown doesn’t get passed in on the update. I’m sure I’m missing the simplest thing, but I’ve double-checked everything I can think of. OK, on to the code:
The Gridview:
<asp:GridView ID="GridView1" runat="server" DataSourceID="CategoryDataSource" DataKeyNames="Id"
AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None">
<RowStyle BackColor="#EFF3FB" />
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
<asp:BoundField DataField="Id" HeaderText="CategoryId" InsertVisible="false" ReadOnly="true"
Visible="false" />
<asp:BoundField DataField="Name" HeaderText="Name" ReadOnly="true" />
<asp:BoundField DataField="DisplayName" HeaderText="DisplayName" />
<asp:BoundField DataField="SiteName" HeaderText="Site" />
<asp:TemplateField HeaderText="Category Group">
<EditItemTemplate>
<asp:DropDownList ID="CategoryGroupId" runat="server" DataSourceID="CategoryGroupDataSource" DataTextField="Name" DataValueField="Id" SelectedValue='<%# Eval("CategoryGroupId") == null ? 0 : Eval("CategoryGroupId") %>'></asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label runat="server" Text='<%# Bind("CategoryGroupName") %>' ID="CategoryGroupName"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
The DataSources:
<asp:ObjectDataSource ID="CategoryDataSource" runat="server" TypeName="Microsoft.Com.Forums.Web.Admin.CategoryManager"
SelectMethod="GetCategoryList" DeleteMethod="DeleteCategory" InsertMethod="AddCategory"
UpdateMethod="EditCategory">
<DeleteParameters>
<asp:Parameter Name="Id" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="Name" Type="String" />
<asp:Parameter Name="DisplayName" Type="String" />
<asp:Parameter Name="SiteName" Type="String" />
<asp:Parameter Name="CategoryGroupId" Type="Int32" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="Id" />
<asp:Parameter Name="DisplayName" Type="String" />
<asp:Parameter Name="SiteName" Type="String" />
<asp:Parameter Name="CategoryGroupId" Type="Int32" />
</UpdateParameters>
</asp:ObjectDataSource>
<asp:ObjectDataSource ID="CategoryGroupDataSource" runat="server" TypeName="Microsoft.Com.Forums.Web.Admin.CategoryManager"
SelectMethod="GetCategoryGroupList"></asp:ObjectDataSource>
The Objects being bound:
public class Category
{
public Guid Id { get; set; }
public string Name { get; set; }
public string DisplayName { get; set; }
public string SiteName { get; set; }
public int? CategoryGroupId { get; set; }
public string CategoryGroupName { get; set; }
}
public class CategoryGroup
{
public int Id { get; set; }
public string Name { get; set; }
public string DisplayName { get; set; }
public string SiteName { get; set; }
}
The signature of the Update Method:
public static bool EditCategory(Guid Id, string DisplayName, string SiteName, int CategoryGroupId)
One thing I did try was to change the Id field in the CategoryGroup to CategoryGroupId to match the field being passed in. It wasn’t a long term fix, but didn’t work anyways.
What am I missing here?
Thanks