简体   繁体   English

查找对象时出现Javascript错误

[英]Javascript-error on finding object

im stuck with Javascript in Asp.net... I made a TextBox named tbValidFrom and another named tbValidTo. 我在Asp.net中停留在Javascript上。 I also made two ModalPopups. 我还制作了两个ModalPopups。 Then i try to open the ModalPopupExtenders when the TextBoxes get focus: 然后,当文本框获得焦点时,我尝试打开ModalPopupExtenders:

<script type="text/javascript">
$('#tbValidTo').focus(function () {
   $find('ModalPopupExtenderNV1').show();
})
$('#tbValidFrom').focus(function () {
    $find('ModalPopupExtenderNV2').show();
})
</script>

but it doesnt finds tbValidTo or ModalPopUpExtender ? 但找不到tbValidTo或ModalPopUpExtender吗?

Runtime-Error in Microsoft JScript: Object expected Microsoft JScript中的运行时错误:预期对象

Here is one of the two ModalPopupExtenders and TextBoxes: 这是两个ModalPopupExtenders和TextBoxes之一:

<asp:TextBox ID="tbValidFrom" runat="server"></asp:TextBox>
<asp:UpdatePanel ID="UpdatePanelNV2" runat="server" RenderMode="Inline" UpdateMode="Conditional">
   <ContentTemplate>
      <cc1:ModalPopupExtender ID="ModalPopupExtenderNV2" runat="server" TargetControlID="HiddenField6"
       PopupControlID="PanelNewVersion" BackgroundCssClass="modalBackground" BehaviorID="ModalPopupExtenderNV2"
       Enabled="True" />
      <asp:HiddenField ID="HiddenField6" runat="server" />
   </ContentTemplate
</asp:UpdatePanel>

The same as above is with the other ModalPopupExtender and TextBox... 其他ModalPopupExtender和TextBox与上述相同...

Help would be really nice. 帮助将是非常好的。 Thanks 谢谢

Edit: Yes i use a masterpage! 编辑:是的,我使用母版!

在此处输入图片说明

Fails where it is marked yellow. 标记为黄色的地方失败。

your jQuery syntax is wrong. 您的jQuery语法错误。

change your script to: 将脚本更改为:

<script type="text/javascript">
$('#tbValidTo').focus(function () {
   $(this).find('#<%=ModalPopupExtenderNV1.ClientID%>').show();
});
$('#tbValidFrom').focus(function () {
    $(this).find('#<%=ModalPopupExtenderNV2.ClientID%>').show();
})
</script>

elements that has runat="server" will eventually end with a different ID than you entered in the aspx file. 带有runat="server"元素最终将以与您在aspx文件中输入的ID不同的ID结尾。

i used the <%= %> to get the 'final' id. 我使用<%= %>来获取“最终” ID。

hope that helps 希望能有所帮助

ASP.NET Can modify the ID's of elements, if you use a Masterpage the element id's will be modified to something like the following : ctl001_ContentPlaceHolder1_tbValidFrom ASP.NET可以修改元素的ID,如果您使用母版页,则元素的ID将被修改为如下所示: ctl001_ContentPlaceHolder1_tbValidFrom

To get around this ASP.NET modification of your elements you can use ASP.NET Inline Expressions to get the rendered ID from the object. 要绕过元素的ASP.NET修改,可以使用ASP.NET内联表达式从对象获取呈现的ID。

See the answer to the following question for more information on ASP.NET Inline Expressions 有关ASP.NET内联表达式的更多信息,请参见以下问题的答案。

You can look at changing the Client ID Mode (The way ASP.NET Modifies IDs when the page is rendered) here : 您可以在此处查看更改客户端ID模式(ASP.NET在呈现页面时修改ID的方式):

ASP.NET Client ID Modes ASP.NET客户端ID模式

EDIT : 编辑

The AjaxControlToolkit's ModalPopupExtender is not rendered on the page as a html element, therefore you cannot find this item using the ClientID of the ModalPopupExtender. AjaxControlToolkit的ModalPopupExtender不会以html元素的形式呈现在页面上,因此,您无法使用ModalPopupExtender的ClientID找到此项目。

You must use the BehaviourID to call the show function on. 您必须使用BehaviourID来调用show函数。

The below code should work correctly : 下面的代码应该可以正常工作:

$('#<%= tbValidTo.ClientID %>').focus(function () {
   $find('ModalPopupExtenderNV1').show();
})
$('#<%= tbValidFrom.ClientID %>').focus(function () {
   $find('ModalPopupExtenderNV2').show();
});

This will find the textbox object using the ASP.NET Inline expressions to find the client ID and will then find the behaviour ID and execute the Show method. 这将使用ASP.NET内联表达式查找文本框对象,以查找客户端ID,然后查找行为ID,并执行Show方法。

Example Markup : 标记示例:

<script type="text/javascript">
$(document).ready(function() {

    $('#<%= tbValidFrom.ClientID %>').focus(function () {
       $find('ModalPopupExtenderNV2').show();
    });
});
</script>

<asp:TextBox ID="tbValidFrom" runat="server"></asp:TextBox>
<asp:UpdatePanel ID="UpdatePanelNV2" runat="server" RenderMode="Inline" UpdateMode="Conditional">
    <ContentTemplate>
        <cc1:modalpopupextender id="ModalPopupExtenderNV2" runat="server" targetcontrolid="HiddenField6"
            popupcontrolid="PanelNewVersion" backgroundcssclass="modalBackground" behaviorid="ModalPopupExtenderNV2"
            enabled="True" />
        <asp:HiddenField ID="HiddenField6" runat="server" />
    </ContentTemplate>
</asp:UpdatePanel>

<asp:Panel ID="PanelNewVersion" runat="server">
    testing panel
</asp:Panel>

Seeing that markup there is two chances for the issues. 看到这种标记,有两个机会解决这些问题。

  1. With the ModalPopupExtender. 使用ModalPopupExtender。 Where you have given the panel "PanelNewVersion" . 您在其中指定了面板“ PanelNewVersion”的位置。 Seems like its missing in the markup. 好像它在标记中丢失了。

  2. In the Javascript code. 在Javascript代码中。 If you want to hide that Popup, give the name of the panel. 如果要隐藏该弹出窗口,请输入面板名称。 NOt the name of the ModalPopupextender. 不要使用ModalPopupextender的名称。

Also you have given an update panel there in page, so you can easily handle the modalpopup in any srever side event of your textbox. 此外,您还在页面中提供了一个更新面板,因此您可以在文本框的任何严重副事件中轻松处理modalpopup。

Otherwise, if you want to use the Modal popup ijn client side, use jQuery Modalpopup. 否则,如果要使用Modal popup ijn客户端,请使用jQuery Modalpopup。 That will the better option. 那将是更好的选择。

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

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