Find CheckBox from GridView in Content Page/Master Page

How to find a control from GridView which resides in Content Page

Here the example using to find the CheckBox, hope this will help you all...

   
   .aspx code
    
    <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
        <asp:GridView ID="GridView1" runat="server">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:CheckBox ID="chkID" runat="server" />
                    </ItemTemplate>
               </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </asp:Content>

     
    codebehind

    Dim ChkSel As New CheckBox
    Dim CellValue As New StringCollection
    Dim tmpGV As New GridView

    tmpGV = FindControlRecursive(Page.Master, "GridView1")
    For i = 0 To tmpGV.Rows.Count - 1
        ChkSel = DirectCast(DirectCast(tmpGV.Rows(i), GridViewRow).Cells(0), TableCell).Controls(1)
        If ChkSel.Checked = True Then
             CellValue.Add(DirectCast(tmpGV.Rows(i), GridViewRow).Cells(1).Text)
        End If
    Next
    
   private Control FindControlRecursive(Control root, string id)
    {
        if (root.ID == id)
        {
            return root;
        }

        foreach (Control c in root.Controls)
        {
            Control t = FindControlRecursive(c, id);
            if (t != null)
            {
                return t;
            }
        }
        return null;
    } 
 
  
Published Thursday, April 29, 2010 1:55 AM by Suthish Nair

Comments

# Find CheckBox from GridView in Content Page/Master Page | OOP - Object Oriented Programing

Pingback from  Find CheckBox from GridView in Content Page/Master Page | OOP - Object Oriented Programing

# re: Find CheckBox from GridView in Content Page/Master Page

Wednesday, May 05, 2010 11:36 AM by xrum

where does ChkSel  come into play?

# re: Find CheckBox from GridView in Content Page/Master Page

Thursday, May 06, 2010 5:46 AM by Suthish Nair

code updated....

Leave a Comment

(required) 
(required) 
(optional)
(required)