tags:

views:

388

answers:

3

I'm coming from a Rails background and doing some work on a ASP.NET project (not ASP MVC). Newbie question: what's the easiest way to make a custom editor for a table of records?

For example: I have a bunch of data rows and want to change the "category" field on each -- maybe a dropdown, maybe a link, maybe the user types it in.

In Rails, I'd iterate over the rows to build a table, and would have a form for each row. The form would have an input box or dropdown, and submit the data to a controller like "/item/edit/15?category=foo" where 15 was the itemID and the new category was "foo".

I'm new to the ASP.NET model and am not sure of the "right" way to do this -- just the simplest way to get back the new data & save it off. Would I make a custom control and append it to each row? Any help appreciated.

A: 

There are a few controls that will do this for you, with varying levels of complexity depending on their relative flexibility.

The traditional way to do this would be the DataGrid control, which gives you a table layout. If you want something with more flexibility in appearance, the DataList and ListView controls also have built-in support for editing, inserting or deleting fields as well.

Check out Matt Berseth's blog for some excellent examples of asp.net controls in action.

Adam Lassek
+2  A: 

You can REALLY cheat nowadays and take a peek at the new Dynamic Data that comes with .NET 3.5 SP1. Scott Guthrie has a blog entry demoing on how quick and easy it'll flow for you here:

http://weblogs.asp.net/scottgu/archive/2007/12/14/new-asp-net-dynamic-data-support.aspx

Without getting THAT cutting edge, I'd use the XSD generator to generate a strongly typed DataSet that coincides with the table in question. This will also generate the TableAdapter you can use to do all your CRUD statements.

From there, bind it to a DataGrid and leverage all the standard templates/events involved with that, such as EditIndex, SelectedIndex, RowEditing, RowUpdated, etc.

I've been doing this since the early 1.0 days of .NET and this kind of functionality has only gotten more and more streamlined with every update of the Framework.

EDIT: I want to give a quick nod to the Matt Berseth blog as well. I've been following a lot of his stuff for a while now and it is great!

Dillie-O
A: 

Thanks for the answers guys. It looks like customizing the DataGrid is the way to go. For any ASP.NET newbies, here's what I'm doing

<asp:DataGrid ID="GridView1" runat="server" AutoGenerateColumns="False">
    <Columns>
        <asp:BoundColumn DataField="RuleID" Visible="False" HeaderText="RuleID"></asp:BoundColumn>
        <asp:TemplateColumn HeaderText="Category">
        <ItemTemplate>
            <!-- in case we want to display an image -->
            <asp:Literal ID="litImage" runat="server">
            </asp:Literal>
            <asp:DropDownList ID="categoryListDropdown" runat="server"></asp:DropDownList>
         </ItemTemplate>
        </asp:TemplateColumn>

    </Columns>
</asp:DataGrid>

This creates a datagrid. We can then bind it to a data source (DataTable in my case) and use things like

foreach (DataGridItem item in this.GridView1.Items)
    {
        DropDownList categoryListDropdown = ((DropDownList)item.FindControl("categoryListDropdown"));
        categoryListDropdown.Items.AddRange(listItems.ToArray());
    }

to populate the intial dropdown in the data grid. You can also access item.Cells[0].text to get the RuleID in this case.

Notes for myself: The ASP.NET model does everything in the codebehind file. At a high level you can always iterate through GridView1.Items to get each row, and item.findControl("ControlID") to query the value stored at each item, such as after pressing an "Update" button.

kurious