How to get the textbox value of the template field in gridview using C# asp.net

Posted by Mahe under C# on 10/18/2012 | Points: 10 | Views : 21219 | Status : [Member] | Replies : 2
Hi all,

How to get the textbox value of the template field in gridview using C# asp.net
Here the are around 5 textbox, on user enter the value in it, on post back, i want the entered value

Regards,
Mahe




Responses

Posted by: Self-Innovator on: 10/18/2012 [Member] Bronze | Points: 25

Up
0
Down
Try this below example with editing dropdown list under gridview

<asp:GridView ID="gvPerson" ShowFooter="true" RowStyle-VerticalAlign="top" DataKeyNames="id" runat="server" AutoGenerateColumns="False" OnRowEditing="gvPerson_RowEditing" OnRowDataBound="gvPerson_RowDataBound" OnRowUpdating="gvPerson_RowUpdating">
<Columns>
<asp:TemplateField HeaderText="Country">
<ItemTemplate><%#Eval("PersonCountry") %></ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="NewddlCountry" AutoPostBack="true" OnSelectedIndexChanged="NewddlCountry_SelectedIndexChanged" runat="server"></asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" ValidationGroup="v2" runat="server" ControlToValidate="NewddlCountry" ErrorMessage="Select Country"></asp:RequiredFieldValidator>
</EditItemTemplate>
<FooterTemplate>
<asp:Button ID="btnAddNew" OnClick="btnAddNew_Click" runat="server" Text="AddNew" />
</FooterTemplate>
</asp:TemplateField>
</asp:GridView>


Code Behind
protected void gvPerson_RowDataBound(object sender, GridViewRowEventArgs e)

{
DataRowView drv = e.Row.DataItem as DataRowView;
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
DropDownList NewddlCountry = (DropDownList)e.Row.FindControl("NewddlCountry");
DataSet tempCountries = BAL.GetCountries();
NewddlCountry.DataSource = tempCountries;
NewddlCountry.DataTextField = "CountryName";
NewddlCountry.DataValueField = "CountryCode";
NewddlCountry.DataBind();
for (int i = 0; i < NewddlCountry.Items.Count; i++)
{
if (NewddlCountry.Items.Text.Trim().CompareTo(SelectedCounty.Trim()) == 0)
{
NewddlCountry.SelectedIndex = i;
break;
}
}
}


Join Hands Change lives
Thanks & Regards
Straight Edge Society

Mahe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sourabh07 on: 10/18/2012 [Member] Starter | Points: 25

Up
0
Down
hi..

In case, if you want to get the Textbox value in any where rather than the RowDataBound

then you may use the following code....

string value = (GridView1.Rows[Row_Index].FindControl("TextBox_Id") as TextBox).Text;


In this way, you can get any text value for the Textbox by specifying the TextBox Id used in the template field of the GridView.

Row_Index, here specifies the row index of the gridview means to say for which row you want to get the value of the TextBox for eg: You want to get the value of the TextBox1 used in the 1st row and 1st column of the GridView.

Sourabh07

Mahe, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response