admin管理员组文章数量:1356770
I am using a user control in my website which performs the functionality of auto plete textbox. I have used JavaScript for the keydown and onfocus client events. This is the code:
<script language="JavaScript" type="text/javascript">
function TriggeredKey(e) {
var keycode;
if (window.event) keycode = window.event.keyCode;
if (keycode == 9) {
document.getElementById("<%=pnlSearch.ClientID %>").style.visibility = 'hidden';
document.getElementById("<%=pnlSearch.ClientID %>").style.display = 'none';
}
else {
document.getElementById("<%=hdfkey.ClientID %>").value = keycode;
}
_dopostback();
}
function pasteIntoInput(el) {
var text = document.getElementById("<%=txtSearch.ClientID %>").value;
if (typeof text != "undefined" && text != "") {
el.focus();
el.value = el.value;
if (typeof el.selectionStart == "number" && typeof el.selectionEnd == "number") {
var val = el.value;
var selStart = el.selectionStart;
el.value = val.slice(0, selStart) + val.slice(el.selectionEnd);
el.selectionEnd = el.selectionStart = selStart + text.length;
}
else if (typeof document.selection != "undefined") {
el.focus();
}
}
}
When I use a single instance of this control in my aspx page it works fine but when I use more than one instances in my aspx page the JavaScript of all of the controls is overwritten by the last instance of the control in my page and no other control works.
I am using a user control in my website which performs the functionality of auto plete textbox. I have used JavaScript for the keydown and onfocus client events. This is the code:
<script language="JavaScript" type="text/javascript">
function TriggeredKey(e) {
var keycode;
if (window.event) keycode = window.event.keyCode;
if (keycode == 9) {
document.getElementById("<%=pnlSearch.ClientID %>").style.visibility = 'hidden';
document.getElementById("<%=pnlSearch.ClientID %>").style.display = 'none';
}
else {
document.getElementById("<%=hdfkey.ClientID %>").value = keycode;
}
_dopostback();
}
function pasteIntoInput(el) {
var text = document.getElementById("<%=txtSearch.ClientID %>").value;
if (typeof text != "undefined" && text != "") {
el.focus();
el.value = el.value;
if (typeof el.selectionStart == "number" && typeof el.selectionEnd == "number") {
var val = el.value;
var selStart = el.selectionStart;
el.value = val.slice(0, selStart) + val.slice(el.selectionEnd);
el.selectionEnd = el.selectionStart = selStart + text.length;
}
else if (typeof document.selection != "undefined") {
el.focus();
}
}
}
When I use a single instance of this control in my aspx page it works fine but when I use more than one instances in my aspx page the JavaScript of all of the controls is overwritten by the last instance of the control in my page and no other control works.
Share Improve this question edited Feb 9, 2020 at 9:38 halfer 20.4k19 gold badges109 silver badges202 bronze badges asked Mar 24, 2012 at 12:03 rainaraina 911 gold badge3 silver badges8 bronze badges2 Answers
Reset to default 5Here's how I've dealt with problems like this in the past...
A block like this goes in an external js file referenced in the control ascx.
function UserControl() {
}
UserControl.prototype = {
DoStuff : function() {
var x = this.clientID;
window.alert(this.pnlSearchClientID);
},
TriggeredKey : function(e) {
var keycode;
if (window.event) keycode = window.event.keyCode;
if (keycode == 9) {
document.getElementById(this.pnlSearchClientID).style.visibility = 'hidden';
document.getElementById(this.pnlSearchClientID).style.display = 'none';
}
_dopostback();
},
pasteIntoInput : function() {
var text = document.getElementById(this.txtSearchClientID).value;
}
};
A block like this goes in the ascx file:
<script type="text/javascript">
function UserControl<%=this.ClientID%>() {
this.pnlSearchClientID = <%=pnlSearch.ClientID%>;
this.txtSearchClientID = <%=txtSearch.ClientID%>;
}
UserControl<%=this.ClientID%> = UserControl.prototype;
</script>
And then in the page including the user control:
<script type="text/javascript">
var inst1 = new UserControl<%=instance.ClientID %>();
inst1.DoStuff();
</script>
The idea is that you have a base class with the functionality you need, shared across all instances of the user control. Then a derived class per instance of the user control, with a new constructor setting properties for all of the instance-specific date (ie the ids of the controls posing the user control). The base class references these properties. The derived class is named using the ClientID of the user control, making it unique on the page.
I don't have access to an asp ATM so there are probably errors in here...
First off i would define your javascript functins in one place, perhaps the parent page or even a globally referenced file. That way you dont have the same functions rendered over and over again when you use multiple instances of your user control on a single page.
Then instead of embedding the client IDs of your pnlSearch and txtSearch controls into the JavaScript functions I would remend passing them into the functions whenever they are called.
The way you have it set up the JavaScript functions on the last instance of your user control that is rendered will be the ones that will be invoked every time, which will cause the functions in the previously rendered user control instances to be ignored.
本文标签: aspnetSame JavaScript for multiple instances of user control not workingStack Overflow
版权声明:本文标题:asp.net - Same JavaScript for multiple instances of user control not working - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743943186a2565940.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论