I receive many emails asking that how can we access particular control which resides inside the GridView control. In this article I will show you how you can access different controls inside the GridView control. We will see how we can access the TextBox control, DropDownList control and the ListBox control. If you are working with ASP.NET 1.X than you might want to check out my article Accessing Different Controls Inside DataGrid.

Introduction:

I receive many emails asking that how can we access particular control which resides inside the GridView control. In this article I will show you how you can access different controls inside the GridView control. We will see how we can access the TextBox control, DropDownList control and the ListBox control. If you are working with ASP.NET 1.X than you might want to check out my article Accessing Different Controls Inside DataGrid.

Adding controls to the GridView control:

You can add different controls in the GridView control by simply using the <ItemTemplate> option. 

Populating ListBox and DropDownList:

Second first task is to populate the ListBox and the DropDownList control. Let's make a simple server side method that will populate both the ListBox and the DropDownList.

C# Code:

// This method populates the DropDownList and the ListBox control

public DataSet PopulateControls()

{

SqlConnection myConnection = new SqlConnection(GetConnectionString());

SqlDataAdapter ad = new SqlDataAdapter("SELECT [Name] FROM tblPerson", myConnection);

DataSet ds = new DataSet();

ad.Fill(ds, "tblPerson");

return ds;

}

VB.NET Code:

' This method populates the DropDownList and the ListBox control
Public Function PopulateControls() As DataSet
Dim myConnection As SqlConnection = New SqlConnection(GetConnectionString())
Dim ad As SqlDataAdapter = New SqlDataAdapter("SELECT [Name] FROM tblPerson",myConnection)
Dim ds As DataSet = New DataSet()
ad.Fill(ds, "tblPerson")
Return ds
End Function

Now we need to bind this method in the HTML view. Check out the code below for the DropDownList you can repeat the same procedure for ListBox control.

 <ItemTemplate>
<asp:DropDownList ID="DropDownList1" DataTextField="Name" DataValueField = "Name" DataSource= '<%# PopulateControls() %>' runat="server">
</asp:DropDownList>
</ItemTemplate>

Now your DropDownList and the ListBox control are populated with some data. Now let's see how we can access different controls inside the GridView.

Accessing different controls within GridView control:

We will try to print out the values on the Button click event that are either entered (TextBox) or selected (DropDownList and ListBox). Let's see how this can be done.

C# Code:

protected void Button1_Click(object sender, EventArgs e)

{

// Iterates through the rows of the GridView control

foreach (GridViewRow row in GridView1.Rows)

{

// Selects the text from the TextBox which is inside the GridView control

string textBoxText = ((TextBox)row.FindControl("TextBox1")).Text;

Response.Write(textBoxText);

// Selects the text from the DropDownList which is inside the GridView control

string dropDownListText = ((DropDownList)row.FindControl("DropDownList1")).SelectedItem.Value;

Response.Write(dropDownListText);

// Selects items from the ListBox which is inside the GridView control

ListBox myListBox = (ListBox)row.FindControl("ListBox1");

foreach(ListItem selectedItem in myListBox.Items)

{

// Checks if the item in the ListBox is selected or not

if (selectedItem.Selected)

{

// Print the value of the item if its selected

Response.Write(selectedItem.Value);

}

}

}

VB.NET Code:

Protected Sub Button1_Click(ByVal sender As ObjectByVal As EventArgs)
        
' Iterates through the rows of the GridView control
        
For Each row As GridViewRow In GridView1.Rows
            
' Selects the text from the TextBox which is inside the GridView control
            
Dim textBoxText As String = CType(row.FindControl("TextBox1"),TextBox).Text
            Response.Write(textBoxText)
            
' Selects the text from the DropDownList which is inside the GridView control
            
Dim dropDownListText As String = CType(row.FindControl("DropDownList1"),DropDownList).SelectedItem.Value
            Response.Write(dropDownListText)
            
' Selects items from the ListBox which is inside the GridView control 
            
Dim myListBox As ListBox = CType(row.FindControl("ListBox1"),ListBox)
            
For Each selectedItem As ListItem In myListBox.Items
                
' Checks if the item in the ListBox is selected or not 
                
If selectedItem.Selected Then
                    
' Print the value of the item if its selected
                    
Response.Write(selectedItem.Value)
                
End If
            Next
        Next
    End Sub

All we are doing in the code above is iterating through all the rows of the GridView control using the GridViewRow object. Next we find the control using the FindControl method and prints out the control's value.

I hope you liked the article, happy coding!