[英]Why doesn't textbox onchange event fire correctly within asp.net user control?
I have 2 textboxes that I'm trying to capture the onchange event for. 我有2个试图捕获onchange事件的文本框。 These textboxes are within a user control that is within an asp.net updatepanel. 这些文本框位于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. 每次更改文本框文本时,都会收到一个“ Microsoft JScript运行时错误:预期对象”错误,该错误不提供任何其他有用的信息。
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. 我以为javascript一定有问题,但是我将这个确切的代码放到了独立的.aspx页面中,并且可以正确执行,没有任何错误。
EDIT Upon looking in Firebug, the real error is that the ResetOrientationImage function cannot be found when the page executes. 编辑在Firebug中查看时,真正的错误是在执行页面时找不到ResetOrientationImage函数。 If I move the javascript out of the user control onto the page itself, it works as I would expect. 如果我将javascript从用户控件中移到页面本身上,它将按预期运行。 Why can't the javascript live in the user control? 为什么Javascript无法显示在用户控件中?
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. width
和height
将是字符串。 You need to make them integers: 您需要将它们设置为整数:
var width = parseInt(document.getElementById('<%= txtWidth.ClientID %>').value);
Also - please how your HTML output , not your .NET source . 另外,请-如何输出 HTML,而不是.NET 源 。 The problem is on the client, not the server. 问题出在客户端上,而不是服务器上。
I stumbled upon this question which gave me my answer: 我偶然发现了这个问题,这给了我答案:
Javascript object "is not defined" error after ScriptManager.RegisterClientScriptInclude ScriptManager.RegisterClientScriptInclude之后出现Javascript对象“未定义”错误
I moved my javascript function into an external file and referenced it in the user control code behind like this: 我将我的javascript函数移到了一个外部文件中,并在后面的用户控制代码中引用了它,如下所示:
ScriptManager sm = ScriptManager.GetCurrent(Page);
ScriptReference sr = new ScriptReference("~/scripts/ResetOrientationImage.js");
if (!sm.Scripts.Contains(sr))
sm.Scripts.Add(sr);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.