locked
ClientIDMode=Static, Still Cannot Access UserControl from Javascript RRS feed

  • Question

  • User-2019299038 posted

    Hi all,

    I've read several posts which all point to ClientIDMode, however, unless I'm just too sleepy, I can't seem to understand why I can't get at my UserControl from javascript. Here's the user control markup. The code-behind just has an empty Page_Load block.

    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="UserControlTest.WebUserControl1" %>
    
    <asp:Table ID="Table1" runat="server">
        <asp:TableRow>
            <asp:TableCell>
                <asp:TextBox ID="TextBox1" runat="server">
                </asp:TextBox>
            </asp:TableCell>
            <asp:TableCell>
                <asp:TextBox ID="TextBox2" runat="server" Rows="5">
                </asp:TextBox>
            </asp:TableCell>
        </asp:TableRow>
    </asp:Table>
    

    Here is the markup for Default.aspx, the code-behind for this page is practically empty as well.

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="UserControlTest.Default" %>
    <%@ Register Src="~/WebUserControl1.ascx" TagName="WebUserControl1" TagPrefix="uc" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <script type="text/javascript">
            function OnClick() {
                var _uc = document.getElementById('WebUserControl1');
    
                alert(_uc === null); // always true
            }
        </script>
    
        <title>
        </title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <uc:WebUserControl1 ID="WebUserControl1" runat="server" ClientIDMode="Static" />
    
                <asp:HyperLink ID="HyperLink1" runat="server" Text="Click" NavigateUrl="javascript:OnClick()"></asp:HyperLink>
            </div>
        </form>
    </body>
    </html>
    

    What am I missing?

    Friday, June 7, 2019 8:06 PM

Answers

  • User-158764254 posted

    the usercontrol does not exist in the html markup sent to the client - its contents do.

    try pointing your javascript at the table's id: 

    ID="Table1"

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 7, 2019 10:31 PM

All replies

  • User-158764254 posted

    the usercontrol does not exist in the html markup sent to the client - its contents do.

    try pointing your javascript at the table's id: 

    ID="Table1"

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 7, 2019 10:31 PM
  • User-2019299038 posted
    #facepalm.

    Thank you. This brings me to another question, but I'll raise that in a separate thread.
    Saturday, June 8, 2019 1:22 AM