你所追求的是一个嵌套的 ListView。嵌套这些东西会变得混乱,但这应该可行:
<asp:GridView runat="server" ID="EntityGridView" AutoGenerateColumns="False"
EditIndex="0" OnRowUpdating="EntityGridView_RowUpdating">
<Columns>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="NameLabel" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="NameTextBox" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:GridView runat="server" DataSource='<%# Eval("Fields") %>'/>
</ItemTemplate>
<EditItemTemplate>
<asp:ListView runat="server" DataSource='<%# Eval("Fields") %>' ID="FieldListView">
<LayoutTemplate>
<asp:PlaceHolder ID="itemPlaceholder" runat="server" />
</LayoutTemplate>
<ItemTemplate>
<asp:TextBox Text='<%# Container.DataItem %>' runat="server" ID="FieldValue"/><br />
</ItemTemplate>
</asp:ListView>
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
</Columns>
</asp:GridView>
您的更新方法将如下所示:
protected void EntityGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
var updatedEntity = new Entity {Fields = new List<string>()};
var row = EntityGridView.Rows[e.RowIndex];
var nameTextBox = (TextBox) row.FindControl("NameTextBox");
updatedEntity.Name = nameTextBox.Text;
var fieldListView = (ListView) row.FindControl("FieldListView");
foreach (var dataItem in fieldListView.Items)
{
var fieldValueTextBox = (TextBox)dataItem.FindControl("FieldValue");
updatedEntity.Fields.Add(fieldValueTextBox.Text);
}
// Do your save etc here
}
然后,您只需将实体列表绑定到 GridView。