Tuesday, November 4, 2014

Get Textbox Value Placed in Asp.net Gridview using C#

Introduction

Here I will explain how to find or get text box value placed in 
gridview in asp.net or how to find controls (textbox, dropdownlist, checkbox,radio button etc..) in inside of asp.net gridview using c#.

Description: 

In previous posts I explained bind data to textbox control in asp.net gridview, Cascading Dropdownlist in inside of asp.net gridview, populate one dropdown based on another dropdown in asp.net, bind data to dropdownlist in asp.net gridview and many articles relating to 
gridview, asp.net, c#. Now I will explain how to find or get text box value placed in gridview in asp.net.

Generally if we want to find or get value from controls which is inside of gridview we will write the code like as shown below


DropDownList ddl = (DropDownList)gvUserInfo.FindControl("yourControlName");
string ddlValue = ddl.SelectedItem.Value;
TextBox txtUserInfo = (TextBox) gvUserInfo.FindControl("yourControlName");
string strValue = txtUserInfo.Text;

In case if we want to find controls inside gridview in rowdatabound condition we will write code like this


protected void gvUserInfo_RowDataBound(object sender, GridViewRowEventArgs e)
{
DropDownList ddl = (DropDownList)gvUserInfo.FindControl("yourControlName");
string ddlValue = ddl.SelectedItem.Value;
TextBox txtUserInfo = (TextBox) gvUserInfo.FindControl("yourControlName");
string strValue = txtUserInfo.Text;
}

Suppose if we have controls in each row of gridview then we need find each row of controls for that we need to write a code like this


protected void gvUserInfo_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddl = (DropDownList)e.Row.FindControl("ddlCity");
int CountryId = Convert.ToInt32(ddl.SelectedItem);
}
}

If you want to see it in example check below articles to bind controls in gridview and get control values.

No comments:

Post a Comment