为什么我的 JavaScript 不与我的 UserControl 交互?
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>
<script type="text/javascript">
function Incrementer() {
var txtBox = document.getElementById('MainContent_TextBox1').value;
var i = parseInt(txtBox);
i = i + 1;
var v = i + "";
document.getElementById('MainContent_TextBox1').value = v;
return false;
}
function Decrementer() {
var txtBox = document.getElementById('MainContent_TextBox1').value;
var i = parseInt(txtBox);
i = i - 1;
var v = i + "";
document.getElementById('MainContent_TextBox1').value = v;
return false;
}
</script>
<asp:TextBox ID="TextBox1" runat="server" Text="0"></asp:TextBox><br />
<asp:Button ID="Button1" runat="server" Text="Up" OnClientClick="return Incrementer();"/>
<asp:Button ID="Button2" Text="Down" runat="server" OnClientClick="return Decrementer();"/>
Default.aspx 如下:-
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Src="~/WebUserControl.ascx" TagName="IncrementerUserControl" TagPrefix="uc1" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<uc1:IncrementerUserControl ID="Incrementer1" runat="server"/>
</asp:Content>
当我将增量函数包含在用户控件中时,它现在不起作用。为什么?如果我在没有用户控制的情况下使用增量器和减量器,它可以正常工作。
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>
<script type="text/javascript">
function Incrementer() {
var txtBox = document.getElementById('MainContent_TextBox1').value;
var i = parseInt(txtBox);
i = i + 1;
var v = i + "";
document.getElementById('MainContent_TextBox1').value = v;
return false;
}
function Decrementer() {
var txtBox = document.getElementById('MainContent_TextBox1').value;
var i = parseInt(txtBox);
i = i - 1;
var v = i + "";
document.getElementById('MainContent_TextBox1').value = v;
return false;
}
</script>
<asp:TextBox ID="TextBox1" runat="server" Text="0"></asp:TextBox><br />
<asp:Button ID="Button1" runat="server" Text="Up" OnClientClick="return Incrementer();"/>
<asp:Button ID="Button2" Text="Down" runat="server" OnClientClick="return Decrementer();"/>
Default.aspx as follows:-
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Src="~/WebUserControl.ascx" TagName="IncrementerUserControl" TagPrefix="uc1" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<uc1:IncrementerUserControl ID="Incrementer1" runat="server"/>
</asp:Content>
The incrementer function now doesn't work when I have it included in the usercontrol. Why? If I use the incrementer and decrementer without the user control it works fine.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
MainContent_TextBox1
就是问题所在。当您在用户控件中包含 TextBox 时,由于runat=server
,它的名称会被破坏。尝试使用:
document.getElementById('%=TextBox1.ClientID %')
请参阅:
http://jagregory.com/writings/how-to-use-clientids-in-javascript-without-the-uginess/
MainContent_TextBox1
is the problem. When you include the TextBox in the user control, its name gets mangled due torunat=server
Try using:
document.getElementById('%=TextBox1.ClientID %')
See:
http://jagregory.com/writings/how-to-use-clientids-in-javascript-without-the-ugliness/