您好我想获得一个“UpDown”按钮,允许用户按住递增/递减按钮来快速轻松地递增/递减十进制值。我一直在尝试使用ajaxToolkit:NumericUpDownExtender,但这似乎只允许按钮点击增加/减少。由于价值是一个百分比,因此这很笨拙。任何想法更好的方式来处理这在ASP.NET内?带按钮保持支持的ASP.NET NumericUpDownExtender?
2
A
回答
1
我能够用JavaScript使用计时器来做到这一点很简单。 这里是它的ASPX部分:
<asp:TextBox ID="Factor" runat="server" MaxLength="6" Width="60"/>
<input id ="UpButton" value="▲" type="button" onmousedown="timerID = setInterval(function(){FactUp()},100);" onmouseup="clearInterval(timerID);"/>
<input id ="DownButton" value="▼" type="button" onmousedown="timerID = setInterval(function(){FactDown()},100);" onmouseup="clearInterval(timerID);"/>
和JavaScript:
var timerID = 0;
function FactDown() {
var obj = document.getElementById('Factor');
var num = parseFloat(obj.value)
if (isNaN(num)) {
return;
}
num -=0.01;
obj.value = num.toFixed(2);
}
function FactUp() {
var obj = document.getElementById('Factor');
var num = parseFloat(obj.value);
if (isNaN(num)) {
return;
}
num += 0.01;
obj.value = num.toFixed(2);
}
0
这个脚本ScriptManager控件的脚本集合添加引用:
Sys.Extended.UI.NumericUpDownBehavior.prototype._clearClickInterval = function() {
if (this._clickInterval != null) {
window.clearInterval(this._clickInterval);
this._clickInterval = null;
}
};
Sys.Extended.UI.NumericUpDownBehavior.prototype._setDownClickInterval = function() {
this._clearClickInterval();
this._clickInterval = window.setInterval(this._clickDownHandler, 200);
};
Sys.Extended.UI.NumericUpDownBehavior.prototype._setUpClickInterval = function() {
this._clearClickInterval();
this._clickInterval = window.setInterval(this._clickUpHandler, 200);
};
Sys.Extended.UI.NumericUpDownBehavior.prototype.addIntervalHandlers = function() {
this._clickInterval = null;
this._buttonMouseUpHandler = Function.createDelegate(this, this._clearClickInterval);
if (this._bUp) {
this._upButtonMouseDownHandler = Function.createDelegate(this, this._setUpClickInterval);
$addHandler(this._bUp, 'mousedown', this._upButtonMouseDownHandler);
$addHandler(window, 'mouseup', this._buttonMouseUpHandler);
}
if (this._bDown) {
this._downButtonMouseDownHandler = Function.createDelegate(this, this._setDownClickInterval);
$addHandler(this._bDown, 'mousedown', this._downButtonMouseDownHandler);
$addHandler(window, 'mouseup', this._buttonMouseUpHandler);
}
};
var legacyInitialize = Sys.Extended.UI.NumericUpDownBehavior.prototype.initialize,
legacyDispose = Sys.Extended.UI.NumericUpDownBehavior.prototype.dispose;
Sys.Extended.UI.NumericUpDownBehavior.prototype.initialize = function() {
legacyInitialize.apply(this);
this.addIntervalHandlers();
};
Sys.Extended.UI.NumericUpDownBehavior.prototype.dispose = function() {
legacyDispose.apply(this);
this._clearClickInterval();
if (this._upButtonMouseDownHandler) {
$removeHandler(this._bUp, 'mousedown', this._upButtonMouseDownHandler);
$removeHandler(window, 'mouseup', this._buttonMouseUpHandler);
this._upButtonMouseDownHandler = null;
}
if (this._downButtonMouseDownHandler) {
$removeHandler(this._bDown, 'mousedown', this._downButtonMouseDownHandler);
$removeHandler(window, 'mouseup', this._buttonMouseUpHandler);
this._downButtonMouseDownHandler = null;
}
this._buttonMouseUpHandler = null;
};
不知道,但可能需要在扩展器实例上明确地调用addIntervalHandlers()
函数。 如果您在此行末尾添加以下内容,您可以查看此脚本:$find("ctl00_SampleContent_NumericUpDownExtender4").addIntervalHandlers()
并从此页面的浏览器控制台执行此脚本:NumericUpDown Demonstration最后一个扩展程序将处理鼠标按钮保持。
这个链接可以帮助你..吗? http://www.c-sharpcorner.com/blogs/1372/up-down-extender-in-asp-net.aspx – MethodMan 2013-05-09 21:36:40
谢谢,但似乎并没有显示如何使这个控制按钮按住。我希望用户能够按住鼠标左键来使值连续变化。 – flerngobot 2013-05-09 21:51:00
你可以显示一些代码,也许你可以做你自己的自定义keydown事件 – MethodMan 2013-05-09 22:12:31