Search code examples
c#asp.netentity-frameworkdynamic-data

Replace checkboxes in Dynamic Data with gridview


As you know when you have a lot of elements in a database table and you also have a relationship between this table and another (let's say many-to-many) , when you are trying to insert a new item in Dynamic Data, for the foreign key option you have a lot of checkboxes you can check.

For example, if you have Category and Products and you want to insert a new Category you will have something like this:

 Product1    Product2   Product3   Product4
 Product5    Product6   ...

When you have few products it looks good, but when you have over 50 000 it doesn't.

How can I replace them with a grid view ( where I load product entities ) having a checkbox before every product name. If I check that checkbox it means I want to add that product to this category (that I'm creating in this moment ).

Hope you understand what I am trying to say. I'm thinking to make a custom field ( or a custom page with custom field ) but I don't know how to make that grid view.

Thank you


Solution

  • How about you make a Gridview:

        <asp:GridView runat="server" ID="gvMyGrid">
             <Columns>
                <asp:BoundField DataField="MyField" HeaderText="HeaderForMyField />
                ... You have a few of these 
                 <asp:TemplateField HeaderText="Products">
                      <ItemTemplate>
                             <asp:Repeater runat="server" id="rptListOfProducts" OnItemDataBound="ProductsDataBound">
                                 <ItemTemplate>
                                      <asp:HiddenField id="hfProductId" runat="server"
                                            Value='<%# DataBinder.Eval(Container.DataItem, "ProductId") %>'/>
                                      <asp:CheckBox id="cbProduct" runat="Server" 
                                          Text='<%# DataBinder.Eval(Container.DataItem, "ProductName") %>'
                                          Checked='<%# DataBinder.Eval(Container.DataItem, "IsThisProductSelected") %>' />  //Or some kind of flag that is true or false
                                 </ItemTemplate>
                             </asp:Repeater>
                      </ItemTemplate>
             </Columns>
        </asp:GridView>
    

    Then in your C# code you can something like this to find out what the user changed.

        foreach (Control c in rptMonitors.Items)
        {
                 var productId = int.Parse(((HiddenField)c.FindControl("hfProductId")).Value);
                 //And get the check box too, generate a list of ProductIds, and if they're checked or not
        }
    

    Not sure if this is what you're looking for, the description was a little vague

    Edit: Oh and if you're expecting loads of entries in the Repeater, you could wrap it in an AjaxControlToolkit Collapsible Panel, so it doesn't spam up your screen.