为什么在 ASP.NET 用户控件中文本框 onchange 事件不能正确触发?

发布于 2025-01-06 03:53:13 字数 1349 浏览 1 评论 0原文

我有 2 个文本框,我正在尝试捕获其 onchange 事件。这些文本框位于 asp.net updatepanel 内的用户控件内。每次更改文本框文本时,我都会收到“Microsoft JScript 运行时错误:需要对象”错误,该错误不提供任何其他有用的信息。

这是代码:

    <script type="text/javascript">
        function ResetOrientationImage() {
            var width = (+document.getElementById('<%= txtWidth.ClientID %>').value);
            var height = (+document.getElementById('<%= txtHeight.ClientID %>').value);
            if (width > height) {
                document.getElementById('<%= imgOrientation.ClientID %>').src = "images/buttons/Landscape.png";
            } else {
                document.getElementById('<%= imgOrientation.ClientID %>').src = "images/buttons/Portrait.png";
            }
        }
</script>
<asp:TextBox ID="txtWidth" runat="server" Width="50px" onchange="ResetOrientationImage()" Text="0"></asp:TextBox>
<asp:TextBox ID="txtHeight" runat="server" Width="50px" onchange="ResetOrientationImage()" Text="0"></asp:TextBox>
<asp:Image ID="imgOrientation" ImageUrl="~/images/buttons/Portrait.png" runat="server" />

我认为这一定是 javascript 的问题,但我将这段确切的代码放入独立的 .aspx 页面中,并且它执行正确,没有错误。

编辑 在Firebug中查看,真正的错误是页面执行时找不到ResetOrientationImage函数。如果我将 JavaScript 从用户控件移到页面本身,它就会按照我的预期工作。为什么 javascript 不能存在于用户控件中?

I have 2 textboxes that I'm trying to capture the onchange event for. These textboxes are within a user control that is within an asp.net updatepanel. Every time I change the textbox text, I receive a "Microsoft JScript runtime error: Object expected" error that doesn't provide any other helpful information.

Here is the code:

    <script type="text/javascript">
        function ResetOrientationImage() {
            var width = (+document.getElementById('<%= txtWidth.ClientID %>').value);
            var height = (+document.getElementById('<%= txtHeight.ClientID %>').value);
            if (width > height) {
                document.getElementById('<%= imgOrientation.ClientID %>').src = "images/buttons/Landscape.png";
            } else {
                document.getElementById('<%= imgOrientation.ClientID %>').src = "images/buttons/Portrait.png";
            }
        }
</script>
<asp:TextBox ID="txtWidth" runat="server" Width="50px" onchange="ResetOrientationImage()" Text="0"></asp:TextBox>
<asp:TextBox ID="txtHeight" runat="server" Width="50px" onchange="ResetOrientationImage()" Text="0"></asp:TextBox>
<asp:Image ID="imgOrientation" ImageUrl="~/images/buttons/Portrait.png" runat="server" />

I thought it must be a problem with the javascript, but I put this exact code into a standalone .aspx page and it executes correctly with no errors.

EDIT
Upon looking in Firebug, the real error is that the ResetOrientationImage function cannot be found when the page executes. If I move the javascript out of the user control onto the page itself, it works as I would expect. Why can't the javascript live in the user control?

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

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

发布评论

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

评论(2

︶葆Ⅱㄣ 2025-01-13 03:53:13

这里有一些事情:

var width = (+document.getElementById('<%= txtWidth.ClientID %>').value);
var height = (+document.getElementById('<%= txtHeight.ClientID %>').value);

“文档”之前开头的“+”无效。

widthheight 将是字符串。您需要将它们设置为整数:

var width = parseInt(document.getElementById('<%= txtWidth.ClientID %>').value);

另外 - 请您的 HTML 输出,而不是您的 .NET 。问题出在客户端,而不是服务器。

A few things here:

var width = (+document.getElementById('<%= txtWidth.ClientID %>').value);
var height = (+document.getElementById('<%= txtHeight.ClientID %>').value);

The "+" at the beginning before "document" is invalid.

width and height are going to be strings. You need to make them integers:

var width = parseInt(document.getElementById('<%= txtWidth.ClientID %>').value);

Also - please how your HTML output, not your .NET source. The problem is on the client, not the server.

酒儿 2025-01-13 03:53:13

我偶然发现了这个问题,它给了我答案:

Javascript object "未定义”错误后 ScriptManager.RegisterClientScriptIninclude

我将我的 javascript 函数移动到外部文件中,并在后面的用户控件代码中引用它,如下所示:

    ScriptManager sm = ScriptManager.GetCurrent(Page);
    ScriptReference sr = new ScriptReference("~/scripts/ResetOrientationImage.js");
    if (!sm.Scripts.Contains(sr))
       sm.Scripts.Add(sr);

I stumbled upon this question which gave me my answer:

Javascript object "is not defined" error after ScriptManager.RegisterClientScriptInclude

I moved my javascript function into an external file and referenced it in the user control code behind like this:

    ScriptManager sm = ScriptManager.GetCurrent(Page);
    ScriptReference sr = new ScriptReference("~/scripts/ResetOrientationImage.js");
    if (!sm.Scripts.Contains(sr))
       sm.Scripts.Add(sr);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文