I have an template field in a GridView :
<asp:TemplateField HeaderText="Value">
<ItemTemplate>
<asp:TextBox CssClass="form-field small" ID="txtPhoneNo" Text="" EnableViewState="true"
runat="server" />
>
</ItemTemplate>
</asp:TemplateField>
I wrote this code to access textbox value in rows :
foreach (GridViewRow row in grvPhoneType.Rows) {
string PhoneNumber = (((row.FindControl("txtPhoneNo")) as TextBox).Text);
// Do what ever with PhoneNumber
}
although user enters value on txtPhoneNo textbox but variable PhoneNumber is empty.
What’s the problem?
Use the FindConrol method in the cell that holds the control. The following example would work if your template column is the first column in your GridView.