简体   繁体   English

无法在JavaScript中访问带有ClientIdMode =“ static”的Asp.net Web表单用户控件

[英]Asp.net web form user control with ClientIdMode = “static” cannot be accessed in javascript

I have a user control that is used inside an ASP.Net web form page. 我有一个在ASP.Net Web表单页面内使用的用户控件。 In the user control I have set the ClientIdMode = "static" eg 在用户控件中,我设置了ClientIdMode =“ static”,例如

<%@ Control Language="C#" AutoEventWireup="true"
CodeBehind="QuestionUserControl.ascx.cs"
Inherits="Kids.Math.Presentation.UserControls.QuestionUserControl"     ClientIDMode="Static" %>

In the form that I am using it I also set Id as: 在我使用它的形式中,我还将ID设置为:

 <uc2:QuestionUserControl runat="server" ID="QuestionUserControl1" ClientIDMode="Static" />

However, when run the page I am trying to access this user control in javascript either by getElementById("QuestionUserControl1") or using JQuery $("#QuestionUserControl1") ... it doesn't work. 但是,当运行该页面时,我试图通过getElementById(“ QuestionUserControl1”)或使用JQuery $(“#QuestionUserControl1”)来使用javascript访问此用户控件……它不起作用。 Actually, the user control doesn't seem to be appear in the source ... when checking the source i cannot find "QuestionUserControl1" however I can find the controls inside it eg ctl00$MainContent$QuestionUserControl1$txtQuestion ... 实际上,用户控件似乎没有出现在源代码中...当检查源代码时,我找不到“ QuestionUserControl1”,但是我可以在其中找到控件,例如ctl00 $ MainContent $ QuestionUserControl1 $ txtQuestion ...

Not sure why the user control Id is not appearing? 不确定为什么没有显示用户控件ID?

Any comments will be appreciated ... 任何意见将不胜感激...

Update: user control mark up (Please forgive the inline markup styles as it is only a test version ...) 更新:用户控件标记(请原谅内联标记样式,因为它只是一个测试版本...)

    <%@ Control Language="C#" AutoEventWireup="true"
    CodeBehind="QuestionUserControl.ascx.cs"
    Inherits="Kids.Math.Presentation.UserControls.QuestionUserControl" ClientIDMode="Static" %>

<style type="text/css">
    .auto-style1 {
        width: 100%;
    }
</style>

<div runat="server" style="Width:100%; Height:100%;" class="panel-body panel-question" ClientIDMode="Static" id="pnlQuestion">

    <div runat="server" style="Width:100%; Height:100%;" ClientIDMode="Static" ID="pnlTextbox">
        <asp:TextBox ID="lblTimer" runat="server" BackColor="Black" BorderColor="Black" BorderStyle="None" BorderWidth="0px" ClientIDMode="Static" ForeColor="White" Visible="False" Width="100%" Font-Bold="True" Font-Size="Large" ReadOnly="True"></asp:TextBox>
        <asp:TextBox CssClass="textarea" TextMode="MultiLine"
            Height="100%" ID="txtQuestion" runat="server"
            BackColor="Black" ReadOnly="True" Width="100%"
            Font-Size="X-Large" ForeColor="White"></asp:TextBox>
    </div>
    <div style="Width:100%; Height:30%;">

        <table class="auto-style1">
            <tr>
                <td>
                    <asp:Button ID="Answer1" runat="server" BorderStyle="Dashed"
                        CommandName="Answer1" Width="100%" OnCommand="Question_Answered" Enabled="False" BackColor="Black" Font-Size="X-Large" ForeColor="White" />
                </td>
                <td>
                    <asp:Button ID="Answer2" runat="server" BorderStyle="Dashed"
                        CommandName="Answer2" Width="100%" OnCommand="Question_Answered" Enabled="False" BackColor="Black" Font-Size="X-Large" ForeColor="White" />
                </td>
                <td>
                    <asp:Button ID="Answer3" runat="server" BorderStyle="Dashed"
                        CommandName="Answer3" Width="100%" OnCommand="Question_Answered" Enabled="False" BackColor="Black" Font-Size="X-Large" ForeColor="White" />`enter code here`
                </td>
            </tr>
        </table>

    </div>

    <asp:HiddenField ID="HiddenField1" runat="server" />
</div>

ctl00$MainContent$QuestionUserControl1$txtQuestion is from a asp tag having id txtQuestion or something similar. ctl00 $ MainContent $ QuestionUserControl1 $ txtQuestion来自具有ID txtQuestion或类似名称的asp标记。

This is not an answer, because poor markup was shared in question I will suggest to do this: 这不是答案,因为有问题的共享标记不佳,我建议您这样做:

Wrap you usercontrol 包装您的用户控件

<%@ Control Language="C#" AutoEventWireup="true"
CodeBehind="QuestionUserControl.ascx.cs"
Inherits="Kids.Math.Presentation.UserControls.QuestionUserControl"%>
<asp:Placeholder id="uniqueID" runat="server" ClientIDMode="static">
<!-- your asp tags inside user control -->
</asp:Placeholder>

Now check your html if you find a div with id="uniqueID" -> that's your first tag created by user control. 现在检查您的html,是否找到id为“ uniqueID”的div->这是用户控件创建的第一个标记。

I finally ended up with this word around: - Added a new button to the user control called it incorrect answer - In javascript once the countdown timer is over - I did a postback for this new button ie __doPostBack("InCorrectAnswer", ""); 我最终用这个词结束了:-向用户控件添加了一个新按钮,称为错误答案-倒数计时器结束后,在javascript中-我为此新按钮做了回发,即__doPostBack(“ InCorrectAnswer”,“”) ;

And then inside the user control I decided on what to be done when InCorrectAnswer is clicked ... which basically meant finalise the process. 然后,在用户控件中,我决定了单击InCorrectAnswer时要执行的操作……这基本上意味着完成该过程。

This worked for me Fine. 这对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM