Geeks With Blogs
AzamSharp Some day I will know everything. I hope that day never comes.

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

 

powered by IMHO

 

Posted on Wednesday, October 5, 2005 3:06 PM | Back to top


Comments on this post: Accessing different controls inside the GridView control

# re: Accessing different controls inside the GridView control
Requesting Gravatar...
Works great! Thanks!!! You'd think the big "M" would have made the GridView easier to use. But, what do you do.
Left by thisguy78 on Feb 08, 2006 10:47 AM

# re: Accessing different controls inside the GridView control
Requesting Gravatar...
The Big M ???
Left by azam on Feb 08, 2006 11:41 AM

# re: Accessing different controls inside the GridView control
Requesting Gravatar...
So what is the correct code for when one uses SelectedIndexChanged and just gets the text from a single textbox in a grid?

I tried the following, but it doesn't seem to work.
int sh = int.Parse(GridView_Companies.Rows[GridView_Companies.SelectedIndex].FindControl("TextBox_Price").ToString());

Thanks,
Paul
Left by Paul on Feb 27, 2006 6:45 PM

# re: Accessing different controls inside the GridView control
Requesting Gravatar...
The textbox entries are deleted on postback. How do i get them to stay as they are?
Left by JB on Mar 13, 2007 4:45 PM

# re: Accessing different controls inside the GridView control
Requesting Gravatar...
I still get an error on the button click event. The error i get is a null exception. Can you tell me what iam missing
thanks.
Left by Smita on May 30, 2007 3:05 PM

Your comment:
 (will show your gravatar)


Copyright © Mohammad Azam | Powered by: GeeksWithBlogs.net