如何在动态加载的用户控件上创建和调用 JavaScript 函数?

发布于 2024-09-03 07:15:49 字数 2540 浏览 3 评论 0原文

当我尝试在动态加载的用户控件中调用 JavaScript 函数时,它找不到指定的函数。我有一个organizationDetails.ascx 的用户控件。在那里,我使用模式弹出扩展程序从用户那里获取一些信息,然后我通过谷歌验证地址并建议正确的格式。我正在尝试使用 java 脚本来调用文本框 onBlur 的函数,但 mozilla firebug 说该函数未定义,但事实并非如此。如果有人能在这里帮助我,那就太好了。


编辑: javascript 位于开头的 ascx 页面上,并使用基类加载用户控件以显示组织详细信息并对其进行编辑。

ascx 页面在面板中具有以下标记,由模式弹出窗口调用。

                <padrap:DropDownList    ID                  ="ddlStateID"
                                        onChange            ="setContextKey2()"
                                        runat               ="server" />

                            <br />                  
                <padrap:Label           ID                  ="lCity"
                                        Text                ="City: "
                                        AssociatedControlID ="tbrCity" 
                                        runat               ="server" 
                                        CssClass            ="col2"/>                       


                <asp:TextBox            ID                  ="tbrCity"
                                        onBlur              ="setContextKey2()"
                                        runat               ="server" />                        


              <br />

<script type="text/javascript">
    alert('i hate javascript');

     var dependentBehaviors = [
            'tbCity_ace_b'
        ];

        var address = {};
    function setContextKey2(){
    //some javascript
            address.addressLine1 = $get('<%=tbrAddressID1.ClientID %>').value;
            address.addressLine2 = $get('<%=tbrAddressID1.ClientID %>').value;
            address.addressLine3 = $get('<%=tbrAddressID1.ClientID %>').value;
            address.countryISO2 = $get('<%=ddlCountryID.ClientID%>').value;
            address.stateCode = $get('<%=ddlStateID.ClientID%>').value;
            address.city = $get('<%=tbrCity.ClientID%>').value;
            address.postalCode = $get('<%=tbrPostalCode.ClientID%>').value; */

            var contextStr = Sys.Serialization.JavaScriptSerializer.serialize(address)

            var behavior = $find('tbCity_ace_b')
            behavior.set_contextKey(contextStr);

            var behavior2 = $find('tbPostalCode_ace_b')
            behavior2.set_contextKey(contextStr);
    }
 </script>

setContextKey2() 的函数在页面的开头定义。如果您需要更多信息,请告诉我。

谢谢, 拉坦

when i try to call a javascript function in a dynamically loaded user control, it cannot find the function specified. I have a user control for organizationDetails.ascx. In there, I am using modal popup extender to get some information from the user and then i verify the address through google and suggests the proper format. I am trying to use a java script for a function called for onBlur of a textbox but the mozilla firebug says that the function is not defined, which is not the case. It would be great if someone can help me out here.


EDITED:
The javascript is on the ascx page in the beginning and the usercontrol is loaded using the base class to show the organization details and edit them.

The ascx page has the following tags in the panel which is called by the modal popup.

                <padrap:DropDownList    ID                  ="ddlStateID"
                                        onChange            ="setContextKey2()"
                                        runat               ="server" />

                            <br />                  
                <padrap:Label           ID                  ="lCity"
                                        Text                ="City: "
                                        AssociatedControlID ="tbrCity" 
                                        runat               ="server" 
                                        CssClass            ="col2"/>                       


                <asp:TextBox            ID                  ="tbrCity"
                                        onBlur              ="setContextKey2()"
                                        runat               ="server" />                        


              <br />

<script type="text/javascript">
    alert('i hate javascript');

     var dependentBehaviors = [
            'tbCity_ace_b'
        ];

        var address = {};
    function setContextKey2(){
    //some javascript
            address.addressLine1 = $get('<%=tbrAddressID1.ClientID %>').value;
            address.addressLine2 = $get('<%=tbrAddressID1.ClientID %>').value;
            address.addressLine3 = $get('<%=tbrAddressID1.ClientID %>').value;
            address.countryISO2 = $get('<%=ddlCountryID.ClientID%>').value;
            address.stateCode = $get('<%=ddlStateID.ClientID%>').value;
            address.city = $get('<%=tbrCity.ClientID%>').value;
            address.postalCode = $get('<%=tbrPostalCode.ClientID%>').value; */

            var contextStr = Sys.Serialization.JavaScriptSerializer.serialize(address)

            var behavior = $find('tbCity_ace_b')
            behavior.set_contextKey(contextStr);

            var behavior2 = $find('tbPostalCode_ace_b')
            behavior2.set_contextKey(contextStr);
    }
 </script>

The function for setContextKey2() is defined in the beginning of the page. Please let me know if you need some more information.

Thanks,
Ratan

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只是我以为 2024-09-10 07:15:49

嘿这个链接可能有帮助。这是一个非常相似的问题

Jquery 和 Ajax Post 问题


已编辑

问题是您的 ajax 调用没有注册您的脚本标记。如果您将脚本放在主页上而不是 ascx 上,那么这将起作用。或者你可以从ajax响应中获取script标签,然后将其注册到dom中。

Hey this link might help. It is a very simular question

Jquery and Ajax Post issue


EDITED

The problem is your ajax call is not registering your script tag. If you put the script on your main page and not the ascx then this will work. Or you can get the script tag from the ajax response and then register it with the dom.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文