我有一个 GridView 包含 Formview,如:
<asp:GridView ID="gv1" runat="server" AutoGenerateColumns="false"
OnRowCommand="gv1_RowCommand" DataKeyNames="employeeID"
DataSourceID="ds">
<EmptyDataTemplate>
<asp:FormView ID="fv1" runat="server" DataKeyNames="employeeID"
DataSourceID="ds" DefaultMode="Insert">
<InsertItemTemplate>
// insert mode table
</InsertItemTemplate>
<EditItemTemplate>
// edit mode table
</EditItemTemplate>
</asp:FormView>
</EmptyDataTemplate>
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:Button ID="btnNew" runat="server" Text="New Record" CommandName="New" />
</HeaderTemplate>
<ItemTemplate>
<asp:Button ID="btnEdit" runat="server" Text="Edit" CommandName="Edit" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
我想要的是当单击按钮时,页面显示 EmptyDataTemplate,所以在代码隐藏中我尝试:
protected void gv1_RowCommand(object sender, GridViewCommandEventArgs e) {
switch(e.CommandName){
case "New":
gv1.DataSourceID = null;
break;
case "Edit":
var row = ((Control)e.CommandSource).NamingContainer as GridViewRow;
if (row != null) {
var fv = row.FindControl("fv1") as FormView;
fv.ChangeMode(FormViewMode.Edit);
}
gv1.DataSourceID = null;
break;
}
}
但是我一直在获取 FormView,它发生 NullReferenceException 错误。任何建议表示赞赏。