Some developer emailed me and asked that how can he extract TextBox text when the TextBox is inside the GridView control. So, I implemented few lines of code to access DropDownList, TextBox and also ListBox which are inside the GridView control.
First you need to populate the DropDownList and ListBox object.
// This method also Populates the ListBox should be called PopulateDropDownAndListBox() :)
public DataSet BindDropDownList()
{
SqlConnection myConnection = new SqlConnection(GetConnectionString());
SqlDataAdapter ad = new SqlDataAdapter("SELECT [Name] FROM tblPerson", myConnection);
DataSet ds = new DataSet();
ad.Fill(ds, "tblPerson");
return ds;
}
Now you need to call this function from the HTML source so it populates when the page binds.
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" DataTextField="Name" DataValueField = "Name" DataSource= '<%# BindDropDownList() %>' runat="server">
</asp:DropDownList>
</ItemTemplate>
Finally, here is the simple code to access different controls inside the GridView control.
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);
}
}}
Pretty easy and simple right! And for all the
VB.NET GEEKS here is the code in VB.NET. I just used the C# to VB.NET
convertor for this conversion.
Public DataSet BindDropDownList()
{
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
}
protected void Button1_Click(Object sender, EventArgs e)
{
' Iterates through the rows of the GridView control
Dim row As GridViewRow
For Each row 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)
Dim selectedItem As ListItem
For Each 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)
}
Next
Next