I have formview control. Inside formview control their is one gridview. In gridview i have FooterTemplate(In footertemplate i have textbox control to add text)to add text. To find that control i have written code as follows:
GridView mygridview = (GridView)FVAddCustomer.FindControl("mygridview");
TextBox txtFName1 = (TextBox)mygridview.FooterRow.FindControl("txtFName1");
Is this right or any other way? because when i find textbox value then its getting null?
Please help me?
<asp:GridView ID="mygridview" runat="server" AutoGenerateColumns="False" ShowFooter="True" BackColor="White" BorderColor="#336666" BorderWidth="3px" CellPadding="4" GridLines="Horizontal" BorderStyle="Double" Width="100%" OnRowEditing="mygridview_RowEditing" OnRowCancelingEdit="mygridview_RowCancelingEdit" OnRowUpdating="mygridview_RowUpdating" Visible="false">
<FooterStyle BackColor="White" ForeColor="#333333" />
<PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#339966" ForeColor="White" Font-Bold="True" />
<HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:TemplateField HeaderText="First Name">
<ItemTemplate>
<asp:Label ID="lblFName1" runat="server" Text='<%#Eval("FirstName")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtFName1Edit" runat="server" Width="90px"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtFName1" runat="server" Width="90px"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<asp:LinkButton ID="lnkEdit" runat="server" Text="Edit" CommandName="Edit" CausesValidation="false" OnClick="lnkEdit_Click"></asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="lnkUpdate" runat="server" Text="Update" CommandName="Update"></asp:LinkButton>
<asp:LinkButton ID="lnkCancel" runat="server" Text="cancel" CommandName="Cancel"></asp:LinkButton>
</EditItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="lnkAdd" runat="server" Text="Add" CommandName="Add" Width="90px" OnClick="lnkAdd_Click"></asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
Thank you.
Asp.net C#
From the comments:
Do you have called FormView.DataBind/GridView.DataBind before?
Remember also that FormView has 3 different FormViewModes. If it is currently in Edit-Mode and the GridView is in ReadOnly ItemTemplate, then you cannot access it.
Try this to get the TextBox from the LinkButton’s click-event handler:
Edit: if this does not work, you can try to handle the GridView’s RowCommand:
Edit2:
You should bind your GridView to it’s DataSource only if
!IsPostBack. The best place to do this is in the FormView’s DataBound Event:On this way you ensure that the GridView will be rebound automatically when the FormView gets databound. If the FormView changes it’s mode to
Insert, you have toDataBindit even if the DataSource isnull!