Question

I'm trying to set two variables to two respective controls, and my current code isn't working. What is the correct syntax?

ascx code:

<ul>
    <li runat="server" id="searchListItem">
       <a href="javascript:void(0)">Search Keywords</a>
    </li>
</ul>
<input runat="server" id="searchBox" type="text" />

ascx.vb code:

Private Sub Page_Load (...
    `other code

    Dim jobsLi As HtmlGenericControl
    Dim jobsBox As HtmlGenericControl
    jobsLi = CType(Page.FindControl("searchListItem"), HtmlGenericControl)
    jobsBox = CType(Page.FindControl("searchBox"), HtmlGenericControl)

    `other code
End Sub

When I step through the code jobsLi and jobsBox remain nothing.

Was it helpful?

Solution

.FindControl() doesn't perform the search recursively, so one way to solve it is writing a custom method to do it.

Something around these lines:

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; 
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top