Editable Datagrid? Repeater?
I have a list of items in display order in asp.net - each item (row) will have 3 text boxes for the user to view and modify that piece of data (shipping cost, processing cost, price).
What would be the "recommended" way to do this? With a relayer, I assume that I would have to iterate over the form values ββon postback, and with the gridview control, I would have to override a different method onrowdatabound and Postback.
I'm sure both will work, but what do you, as a developer, choose in this situation?
0
source to share
1 answer
What I've done in the past is to use the 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 (for example, click the Save button), you can loop through the rows in the GridView and rip the values ββout of the text boxes:
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;
}
}
+1
source to share