views:

336

answers:

1

I've got a list of items in an order to show in asp.net -- each item (row) will have 3 textboxes so the user can both view and change that piece of data (shipping cost, handling cost, price).

What would be one "recommended" way to accomplish this? With a repeater, I assume I'd then have to loop through the form values on postback, and with a gridview control I'd have to override onrowdatabound and on Postback some other method.

I'm sure both would work, but what do YOU as a developer choose in this situation?

+1  A: 

What I've done in the past is use data-bound GridView TemplateColumns:

<asp:GridView runat="server" ID="grdRoster" AutoGenerateColumns="false">
    <Columns>
        <asp:TemplateField HeaderText="First Name">
            <ItemTemplate>
                <asp:TextBox runat="server" ID="txtFirstName" Columns="10" Text='<%# Eval("RosterFirstName") %>' />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Middle Name">
            <ItemTemplate>
                <asp:TextBox runat="server" ID="txtMiddleName" Columns="10" Text='<%# Eval("RosterMiddleName") %>' />
            </ItemTemplate>
        </asp:TemplateField>                       
        <asp:TemplateField HeaderText="Last Name">
            <ItemTemplate>
                <asp:TextBox runat="server" ID="txtLastName" Columns="10" Text='<%# Eval("RosterLastName") %>' />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Then, on postback (say, a "Save" button click), you can loop through the rows in the GridView and pluck the values out of the textboxes:

foreach ( GridViewRow row in grdRoster.Rows )
{
    if ( row.RowType == DataControlRowType.DataRow )
    {
        string firstName = ( ( TextBox ) row.FindControl( "txtRosterFirstName" ) ).Text;
        string middleName = ( ( TextBox ) row.FindControl( "txtRosterMiddleName" ) ).Text;
        string lastName = ( ( TextBox ) row.FindControl( "txtRosterLastName" ) ).Text;
    }
}
Matt Peterson
Thanks -- so the GridView is what you use, evidently. I was just wondering. I've used both, but was never happy with either solution.
Matt Dawdy
I think I do like the gridview method because then you get give your for fields sane ids, not some odd name of "recid|field" hokey thing. Findcontrol eliminates this since you are limited in scope to that particular row. Thanks Matt.
Matt Dawdy