asp.nethtml-tablerowfindcontrol

How come FindControl can find a control that doesn't belong to current table row?


I have a table. I need to find all the DropDownList controls in the table. I wrote following code to iterate the table rows to find DropDownList controls. How come in each loop, I could find the DropDownList, even I only have on in row 4?

    for (int i = 0; i < table1.Rows.Count; i++)
                {                
                    foreach (string UserID in UserIDs)
                    {
                        string ddlID = "ddlEditings" + UserID;
                        DropDownList ddl = (DropDownList)table1.Rows[i].FindControl(ddlID);
                        if (ddl != null)
                        {
                            if (!updateableUsers.ContainsKey(Int32.Parse(UserID)))
                                updateableUsers.Add(Int32.Parse(UserID), Int32.Parse(ddl.SelectedValue));
                        }

                    }
                }

Markup

    <table id="ctl00_ContentPlaceHolder1_table1" border="1" width="100%">
            <tr>
                <td align="center" width="100%" colspan="3" bgcolor="#C0C0C0"><br /></td>
            </tr>
            <tr>
                <td align="center" width="7%" bgcolor="#C0C0C0"></td>
                <td align="center" width="50%" bgcolor="#CCE6FF">User Name</td>

                <td align="center" width="43%" bgcolor="#CCE6FF">Editing Privileges</td>
            </tr>
            <tr>
                <td align="center" width="7%" bgcolor="#C0C0C0"></td>
                <td width="50%" bgcolor="#CCE6FF">Ferguson, Gene</td>
                <td align="left" width="43%" bgcolor="#CCE6FF">functional manager</td>
            </tr>

            <tr>
                <td align="center" width="7%" bgcolor="#C0C0C0"><input type="submit" name="ctl00$ContentPlaceHolder1$btnDelete5439" value="Delete" onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;ctl00$ContentPlaceHolder1$btnDelete5439&quot;, &quot;&quot;, true, &quot;&quot;, &quot;&quot;, false, false))" id="ctl00_ContentPlaceHolder1_btnDelete5439" /></td>
                <td width="50%" bgcolor="#CCE6FF">Lento, Jackie</td>
                <td align="left" width="43%" bgcolor="#CCE6FF"><select name="ctl00$ContentPlaceHolder1$ddlEditings5439" id="ctl00_ContentPlaceHolder1_ddlEditings5439">
                    <option selected="selected" value="2">comments only</option>
                    <option value="1">view</option>

                </select></td>

            </tr>
            <tr bgcolor="#C0C0C0">
                <td width="100%" align="center" colspan="3"><input type="submit" name="ctl00$ContentPlaceHolder1$btnSubmit" value="Update" onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;ctl00$ContentPlaceHolder1$btnSubmit&quot;, &quot;&quot;, true, &quot;&quot;, &quot;&quot;, false, false))" id="ctl00_ContentPlaceHolder1_btnSubmit" /><input type="submit" name="ctl00$ContentPlaceHolder1$btnReset" value="Reset" onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;ctl00$ContentPlaceHolder1$btnReset&quot;, &quot;&quot;, true, &quot;&quot;, &quot;&quot;, false, false))" id="ctl00_ContentPlaceHolder1_btnReset" /><br /></td>
            </tr>
        </table>

Solution

  • you can do like this... to find all controls in html table ....

            foreach(HtmlTableRow tr in Table1.Rows)
            {
                foreach(HtmlTableCell td in tr.Cells)
                {
                    foreach(Control c in td.Controls)
                    {
                        Response.Write(c.ID);
                    }
                }
            }