How to find FormView within GridView EmptyDataTemplate

ozkaan

New Member
I have a GridView contains Formview like:\[code\]<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>\[/code\]What I want is when the button been clicked, then page displays the EmptyDataTemplate, so in codebehind I try:\[code\]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; }}\[/code\]But I stuck in getting FormView, it occurs NullReferenceException error.Any suggestion is appreciated.
 
Back
Top