为什么在 ASP.NET 用户控件中文本框 onchange 事件不能正确触发?
我有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里有一些事情:
“文档”之前开头的“+”无效。
width
和height
将是字符串。您需要将它们设置为整数:另外 - 请您的 HTML 输出,而不是您的 .NET 源。问题出在客户端,而不是服务器。
A few things here:
The "+" at the beginning before "document" is invalid.
width
andheight
are going to be strings. You need to make them integers:Also - please how your HTML output, not your .NET source. The problem is on the client, not the server.
我偶然发现了这个问题,它给了我答案:
Javascript object "未定义”错误后 ScriptManager.RegisterClientScriptIninclude
我将我的 javascript 函数移动到外部文件中,并在后面的用户控件代码中引用它,如下所示:
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: