Reputation: 19707
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?
Upvotes: 0
Views: 882
Reputation: 5749
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;
}
}
Upvotes: 1