Pages

Saturday, June 2, 2012

Finding Control Inside Grid View on Button Click

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

No comments:

Post a Comment