Search HtmlGenericControl

   System.Web.UI.HtmlControls.HtmlGenericControl div = (System.Web.UI.HtmlControls.HtmlGenericControl)pnl.Controls[0].FindControl("divMessage");

I try to find divMessagesomethign as above, but I get null ...:

below shows how my div is located.

 <mobile:Panel ID="pnl" Runat="server">
   <mobile:DeviceSpecific ID="device" runat="server">
            <ContentTemplate>
          <div id="divMessage" runat="server">test.....</div>
       </ContentTemplate>
          </mobile:DeviceSpecific>
 </mobile:Panel>
+3
source share
3 answers

Here you need to find divwhich one you need:

var div = (HtmlGenericControl)pnl.Controls[0].FindControl("divMessage");

I created a new page and tested it:

<%@ Page Language="C#" Inherits="System.Web.UI.MobileControls.MobilePage" %>
<%@ Register TagPrefix="mobile"
    Namespace="System.Web.UI.MobileControls"
    Assembly="System.Web.Mobile" %>

<script runat="server">

    protected void Page_Load(object sender, EventArgs e)
    {
        var div = (HtmlGenericControl)pnl.Controls[0].FindControl("divMessage");
    }

</script>
<body>
    <mobile:form id="form1" runat="server">

    <mobile:panel id="pnl" runat="server">
        <mobile:DeviceSpecific ID="device" runat="server">
            <Choice>
                <ContentTemplate>
                    <div id="divMessage" runat="server">test.....</div>
                </ContentTemplate>
            </Choice>
        </mobile:DeviceSpecific>
    </mobile:panel>

    </mobile:form>
</body>
</html>

The variable divcontains the necessary control.

+5
source

The easiest way to find a control is to do a recursive search, as your current method probably doesn't work because the controls are nested.

/// <summary>
/// Recursive FindControl method, to search a control and all child
/// controls for a control with the specified ID.
/// </summary>
/// <returns>Control if found or null</returns>
public static Control FindControlRecursive(Control root, string id)
{
    if (id == string.Empty)
        return null;

    if (root.ID == id)
        return root;

    foreach (Control c in root.Controls)
    {
        Control t = FindControlRecursive(c, id);
        if (t != null)
        {
            return t;
        }
    }
    return null;
}

You can use this method as follows:

HtmlGenericControl div = (HtmlGenericControl) FindControlRecursive(pnl, "divMessage");
+3
source

, , pnl divMessage. , .

0

All Articles