In his article I will explain how to find (access) control in GridView in RowDataBound and RowCommand events of ASP.Net GridView using C# and VB.Net.
This article will explain how to find controls like TextBox, DropDownList, CheckBox, RadioButton, ListBox, Label, RadioButtonList, CheckBoxList, etc. and also get and set values of controls inside RowDataBound and RowCommand events of ASP.Net GridView.
HTML Markup
The following HTML Markup consists of an ASP.Net GridView with two TemplateField columns, one consisting of a TextBox and other a DropDownList control.
The GridView also contains a Command Button and is assigned OnRowDataBound and OnRowCommand event handlers.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound" OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField HeaderText="Name" ItemStyle-Width="150">
<ItemTemplate>
<asp:TextBox ID="txtName" runat="server" Text='<%# Eval("Name") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Country" ItemStyle-Width="150">
<ItemTemplate>
<asp:DropDownList ID="ddlCountries" runat="server">
<asp:ListItem Text="United States" Value="United States" />
<asp:ListItem Text="India" Value="India" />
<asp:ListItem Text="France" Value="France" />
<asp:ListItem Text="Russia" Value="Russia" />
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:ButtonField CommandName="Select" Text="Select" ButtonType="Button" />
</Columns>
</asp:GridView>
Namespaces
You will need to import the following namespaces.
C#
VB.Net
Binding the GridView
I have created a dynamic DataTable with some dummy data and it has been bind to the GridView control in Page Load event.
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id"), new DataColumn("Name"), new DataColumn("Country") });
dt.Rows.Add(1, "John Hammond", "United States");
dt.Rows.Add(2, "Mudassar Khan", "India");
dt.Rows.Add(3, "Suzanne Mathews", "France");
dt.Rows.Add(4, "Robert Schidner", "Russia");
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As New DataTable()
dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Id"), New DataColumn("Name"), New DataColumn("Country")})
dt.Rows.Add(1, "John Hammond", "United States")
dt.Rows.Add(2, "Mudassar Khan", "India")
dt.Rows.Add(3, "Suzanne Mathews", "France")
dt.Rows.Add(4, "Robert Schidner", "Russia")
GridView1.DataSource = dt
GridView1.DataBind()
End If
End Sub
Find (Access) control inside GridView in RowDataBound event of ASP.Net GridView
Below is the GridView RowDataBound event handler. First a check is performed to make sure that the GridView Row is a DataRow.
Note: GridView also contains other type of Rows such has Header Row and Footer Row, hence if such check is not performed then you might get error while finding a control.
Then the TextBox and DropDownList controls are referenced using the FindControl method of the GridView Row by passing the ID of the control as parameter.
FindControl method returns the TextBox and DropDownList as an object of Control class and hence we need
to type cast them to their respective types in order to access their properties.
In similar way you can find other controls like CheckBox, RadioButton, ListBox, Label, RadioButtonList, CheckBoxList, etc. and type cast the control according to its type.
C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Find the TextBox control.
TextBox txtName = (e.Row.FindControl("txtName") as TextBox);
//Find the DropDownList control.
DropDownList ddlCountries = (e.Row.FindControl("ddlCountries") as DropDownList);
string country = (e.Row.DataItem as DataRowView)["Country"].ToString();
ddlCountries.Items.FindByValue(country).Selected = true;
}
}
VB.Net
Protected Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
'Find the TextBox control.
Dim txtName As TextBox = TryCast(e.Row.FindControl("txtName"), TextBox)
'Find the DropDownList control.
Dim ddlCountries As DropDownList = TryCast(e.Row.FindControl("ddlCountries"), DropDownList)
Dim country As String = TryCast(e.Row.DataItem, DataRowView)("Country").ToString()
ddlCountries.Items.FindByValue(country).Selected = True
End If
End Sub
The following screenshot displays the controls being accessed in RowDataBound event.
Find (Access) control inside GridView in RowCommand event of ASP.Net GridView
The row index can be easily determined using the CommandArgument property of GridViewCommandEventArgs object and using the row index, the GridView Row is determined.
The TextBox and DropDownList controls are referenced using the FindControl method of the GridView Row by passing the ID of the control as parameter.
FindControl method returns the TextBox and DropDownList as an object of Control class and hence we need has to be type cast to them to their respective types in order to access their properties.
Finally the values of TextBox and DropDownList is displayed using JavaScript Alert message box.
In similar way you can find other controls like CheckBox, RadioButton, ListBox, Label, RadioButtonList, CheckBoxList, etc. and type cast the control according to its type.
C#
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
//Determine the RowIndex of the Row whose Button was clicked.
int rowIndex = Convert.ToInt32(e.CommandArgument);
//Reference the GridView Row.
GridViewRow row = GridView1.Rows[rowIndex];
//Find the TextBox control.
TextBox txtName = (row.FindControl("txtName") as TextBox);
//Find the DropDownList control.
DropDownList ddlCountries = (row.FindControl("ddlCountries") as DropDownList);
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Name: " + txtName.Text + "\\nCountry: " + ddlCountries.SelectedItem.Value + "');", true);
}
VB.Net
Protected Sub GridView1_RowCommand(sender As Object, e As GridViewCommandEventArgs)
'Determine the RowIndex of the Row whose Button was clicked.
Dim rowIndex As Integer = Convert.ToInt32(e.CommandArgument)
'Reference the GridView Row.
Dim row As GridViewRow = GridView1.Rows(rowIndex)
'Find the TextBox control.
Dim txtName As TextBox = TryCast(row.FindControl("txtName"), TextBox)
'Find the DropDownList control.
Dim ddlCountries As DropDownList = TryCast(row.FindControl("ddlCountries"), DropDownList)
ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('Name: " + txtName.Text + "\nCountry: " + ddlCountries.SelectedItem.Value + "');", True)
End Sub
The following screenshot displays the controls values displayed using RowCommand event.
Demo
Downloads
Download Code