Html 是否有用于 ASP.NET 的数字 UpDown 控件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16538286/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a numeric UpDown control for ASP.NET?
提问by HelpASisterOut
Is there a way I can use a numeric updown in ASP.NET without using JavaScript?
有没有一种方法可以在不使用 JavaScript 的情况下在 ASP.NET 中使用数字倒置?
And if not, is there an alternative?
如果没有,还有其他选择吗?
回答by Armando
I was trying to do the same thing, and it turns out the asp textbox has an option for it. what worked for me was this:
我试图做同样的事情,结果是 asp 文本框有一个选项。对我有用的是:
<asp:TextBox TextMode="Number" runat="server" min="0" max="20" step="1"/>
this gave me a textbox which, when mouse hovers over it or is given focus, shows the up-down controls, and only allows numbers from min to max. it works the same as
这给了我一个文本框,当鼠标悬停在它上面或获得焦点时,显示上下控件,并且只允许从最小到最大的数字。它的工作原理与
<input type="number" min="0" max="20" step="1" />
回答by LukeHennerley
Please look into the Ajax Control Toolkit
请查看 Ajax 控件工具包
http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/NumericUpDown/NumericUpDown.aspx
http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/NumericUpDown/NumericUpDown.aspx
<ajaxToolkit:NumericUpDownExtender ID="NUD1" runat="server"
TargetControlID="TextBox1"
Width="100"
RefValues="January;February;March;April"
TargetButtonDownID="Button1"
TargetButtonUpID="Button2"
ServiceDownPath="WebService1.asmx"
ServiceDownMethod="PrevValue"
ServiceUpPath="WebService1.asmx"
ServiceUpMethod="NextValue"
Tag="1" />
Also consider adding the reference with NuGetby using PM> Install-Package AjaxControlToolkit
还可以考虑将与参考的NuGet使用PM> Install-Package AjaxControlToolkit
回答by LukeHennerley
I think the following html can answer your question:
我认为以下 html 可以回答您的问题:
<head runat="server">
<title>Numeric Up Down</title>
<script type="text/jscript">
function Load() {
/*numericUpDown1.value = or*/ document.getElementById("numericUpDown1").value = parseFloat(document.getElementById("<%=NumericUpDown1.ClientID%>").value);
}
function Change() {
document.getElementById("<%=NumericUpDown1.ClientID%>").value = document.getElementById("numericUpDown1").value; //or numericUpDown1.value
}
</script>
</head>
<body onload="Load()">
<form id="form1" runat="server">
<div>
<input type="number" id="numericUpDown1" onchange="Change()" />
<asp:HiddenField ID="NumericUpDown1" runat="server" />
</div>
</form>
</body>
And then on the asp server side code in C# or Visual Basic, you can treat that HiddenField as NumericUpDown, but notethat his value is string, and notdecimal, like System.Windows.Forms.NumericUpDown control, or float, or double, or int, so you will have to Parse it to one of these types for what you need the most.
然后在 C# 或 Visual Basic 中的 asp 服务器端代码中,您可以将该 HiddenField 视为 NumericUpDown,但请注意,他的值是string,而不是十进制,如 System.Windows.Forms.NumericUpDown 控件,或者 float 或 double,或 int,因此您必须将其解析为您最需要的这些类型之一。
If you want to stylethe numeric up down, then in javascript it is simple. Just set document.getElementById("numericUpDown1").style, but if you want to do it through the asp server side code in C# or Visual Basic, then the html mustbe different:
如果您想向下设置数字样式,那么在 javascript 中它很简单。只需设置document.getElementById("numericUpDown1").style,但是如果你想通过C#或Visual Basic中的asp服务器端代码来做,那么html必须不同:
<head runat="server">
<title>Numeric Up Down</title>
<script type="text/jscript">
function Change() {
document.getElementById("<%=NumericUpDown1.ClientID%>").value = document.getElementById("numericUpDown1").value; //or numericUpDown1.value
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<% this.Response.Write(string.Format("<input type='number' id='numericUpDown1' value='{0}' onchange='Change()' style='{1}' />", this.NumericUpDown1.Value, this.numericUpDown1Style)); %>
<asp:HiddenField ID="NumericUpDown1" runat="server" />
</div>
</form>
</body>
numericUpDown1Styleis a protected field whose type is stringdefined in the asp server side code in C# or Visual Basic.
numericUpDown1Style是一个受保护的字段,其类型是在 C# 或 Visual Basic 中的 asp 服务器端代码中定义的字符串。
If you want to give it a classand not to style it, then the html must be:
如果你想给它一个类而不是它的样式,那么 html 必须是:
<head runat="server">
<title>Numeric Up Down</title>
<script type="text/jscript">
function Change() {
document.getElementById("<%=NumericUpDown1.ClientID%>").value = document.getElementById("numericUpDown1").value; //or numericUpDown1.value
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<% this.Response.Write(string.Format("<input type='number' id='numericUpDown1' value='{0}' onchange='Change()' class='{1}' />", this.NumericUpDown1.Value, this.numericUpDown1CssClass)); %>
<asp:HiddenField ID="NumericUpDown1" runat="server" />
</div>
</form>
</body>
numericUpDown1CssClassis a protected field whose type is stringdefined in the asp server side code in C# or Visual Basic.
numericUpDown1CssClass是一个受保护的字段,其类型是在 C# 或 Visual Basic 中的 asp 服务器端代码中定义的字符串。
If you want to style it and give it a class, then html is like html #2 or html #3, but the only change is in the following line:
如果你想给它设置样式并给它一个类,那么 html 就像 html #2 或 html #3,但唯一的变化是在以下行中:
<% this.Response.Write(string.Format("<input type='number' id='numericUpDown1' value='{0}' onchange='Change()' style='{1}' class='{2}' />", this.NumericUpDown1.Value, this.numericUpDown1Style, this.numericUpDown1CssClass)); %>
I think you know what is numericUpDown1Styleand numericUpDown1CssClassfrom #2 and #3
我想你知道#2 和 #3 中的numericUpDown1Style和numericUpDown1CssClass是什么
RECOMMENDED TIP:
推荐提示:
If your website contains a lot of numeric up down controls that are used in your asp server side code, and this is disadvantageous to create all of them this way, then you can add new "Web User Control" item to your website and name it "NumericUpDown". Then in its source html you can copy the html #1 or html #2 or html #3 or html #4 that I posted above (depends on if you want to style the numeric up down or not, or give it a class or not, or both or not) with some removes and changes, because it is not "WebForm", but "Web User Control" and in the asp server side code make the following properties (They are in C#, but if you use Visual Basic, I don't think it will be a problem for you to translate the code):
如果您的网站包含许多在您的 asp 服务器端代码中使用的数字向上向下控件,并且以这种方式创建所有这些控件是不利的,那么您可以向您的网站添加新的“Web 用户控件”项并为其命名“数字向上”。然后在其源 html 中,您可以复制我在上面发布的 html #1 或 html #2 或 html #3 或 html #4(取决于您是否想将数字向上设置样式,或者是否给它一个类或两者都没有)进行一些删除和更改,因为它不是“WebForm”,而是“Web 用户控件”,并且在 asp 服务器端代码中创建以下属性(它们在 C# 中,但如果您使用 Visual Basic,我认为你翻译代码不会有问题):
public decimal Value
{
get
{
return decimal.Parse(this.HiddenField.Value);
}
set
{
this.HiddenField.Value = value.ToString();
}
}
//Like the System.Windows.Forms.NumericUpDown.Value, but if you dislike 'decimal', and you want other type, then you can change the return type and the type Parse.
//Note that the ID of the HiddenField is simply "HiddenField", and not "NumericUpDown1", so make sure in the Source html to rename "NumericUpDown1" to "HiddenField", but probably you would like a different ID, so if you gave it different ID, then ensure that in the code you refer this HiddenField with the ID you chose, and not "HiddenField" or "NumericUpDown1".
//The following properties are for only if you want to style your Numeric Up Down:
protected string style;
public string Style
{
get
{
return this.style;
}
set
{
this.style = value;
}
}
//If you chose, copied, pasted and changed html #2 or html #4, then don't forget to replace this.numericUpDown1Style to this.Style in the source html of the Web User Control.
//Optional
public Unit Width
{
get
{
int startIndex = this.style.IndexOf("width") + 6;
if (index != -1)
{
int endIndex = this.style.IndexOf(';', startIndex);
return Unit.Parse(this.style.Substring(startIndex, endIndex - startIndex));
}
return Unit.Empty;
}
set
{
if (this.style.Contains("width"))
{
this.style = this.style.Replace("width:" + this.Width.ToString() + ';', "width:" + value.ToString() + ';');
}
else
{
this.style += "width:" + value.ToString() + ';';
}
}
}
//The same you can do with the Height property.
//You can replace all the style code with the CssClass code instead, or just add it:
protected string cssClass;
public string CssClass
{
get
{
return this.cssClass;
}
set
{
this.cssClass = value;
}
}
//If you chose, copied, pasted and changed html #3 or html #4, then don't forget to replace this.numericUpDown1CssClass to this.CssClass in the source html of the Web User Control.
If you style the NumericUpDown, so know also that in every ASP.NET control, you can type after their ID .Style["style"] = "value".
如果您设置 NumericUpDown 的样式,那么还要知道在每个 ASP.NET 控件中,您可以在它们的 ID 之后键入 .Style["style"] = "value"。
If you want to be able to do this with NumericUpDown too, then change the type of the protected field stylefrom stringto MyStyle
如果您也希望能够使用 NumericUpDown 执行此操作,请将受保护字段样式的类型从字符串更改为MyStyle
There is the definition of MyStyle:
有MyStyle的定义:
public class MyStyle
{
internal string style;
public string this[string style]
{
get
{
int startIndex = this.style.IndexOf(style) + style.Length + 1;
int endIndex = this.style.IndexOf(';', startIndex);
return this.style.Substring(startIndex, endIndex - startIndex)
}
set
{
this.style = this.style.Replace(style + ':' + this[style] + ';', style + ':' + value + ';')
}
}
}
Add this class to the Code of the Web User Control, and change the Style property:
将此类添加到 Web 用户控件的代码中,并更改 Style 属性:
public string Styles
{
get
{
return this.style.style;
}
set
{
this.style.style = value;
}
}
and then add the property:
然后添加属性:
public MyStyle Style
{
get
{
return this.style;
}
}
and change the line from:
并将行更改为:
protected string style;
to:
到:
protected readonly MyStyle style = new MyStyle();
Don't forget in the source html of Web User Control, to replace this.Style to this.Styles.
不要忘记在 Web User Control 的源代码中,将 this.Style 替换为 this.Styles。
NOTE: I didn't had patience to test the code by myself, so it might not work, so you'll have to fix it by yourself. At least you understood my idea.
注意:我没有耐心自己测试代码,所以它可能不起作用,所以你必须自己修复它。至少你明白我的想法。
After the fixes, you can edit my answer and replace the wrong code with your fixed code.
修复后,您可以编辑我的答案并用您的固定代码替换错误的代码。
I will appreciate it very much!
我会很感激的!
This Web User Control is the ASP NumericUpDown you wanted!
这个 Web 用户控件就是您想要的 ASP NumericUpDown!
回答by breez
If you are stuck on .NET 4.0 and you want to use the native HTML5 input type "number" (rather than the NumericUpDown from Ajax Control Toolkit) you can use a combination of the ASP TextBox control with extra "type" tag:
如果您坚持使用 .NET 4.0 并且想要使用原生 HTML5 输入类型“数字”(而不是来自 Ajax 控件工具包的 NumericUpDown),您可以使用 ASP TextBox 控件与额外的“类型”标签的组合:
<asp:TextBox runat="server" ID="txtMyTextBox" type="number" min="0" max="10" step="1"/>
If you want to prevent any text input, you could even add a FilteredTextBoxExtender from Ajax Control Toolkit:
如果你想阻止任何文本输入,你甚至可以从 Ajax Control Toolkit 添加一个 FilteredTextBoxExtender:
<ajaxToolkit:FilteredTextBoxExtender runat="server" TargetControlID="txtMyTextBox" FilterType="Numbers" />