How to access different controls within the GridView control in Asp.Net(C#)

In this blog i am going to show you how to find different controls within the GridView control e.g. TextBox control, Button control, ListBox control, DropDown, etc.

On the Button click event, I will print the values that are either entered (TextBox) or selected (DropDownList and ListBox). Let's see how this can be done ::

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);
            }
        }
    }

Comments

Popular posts from this blog

Search data in Gridview on Textbox Key press event using JQuery in Asp.Net- C#

StateCode and StatusCode Values for mostly used entities in Microsoft Dynamics CRM 2013

Dumps for Microsoft Dynamics CRM MB2-703 Practice Exam Questions Free

How to import CSV files into DataTable in C#

How to show enlarge image when mouse hover on image or link in Asp.Net(c#) using JavaScript

go to top image