我认为下面的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>
然后在 C# 或 Visual Basic 中的 asp 服务器端代码中,您可以将 HiddenField 视为 NumericUpDown,但 注意 他的值是 string,并且 不是十进制,例如 System.Windows.Forms.NumericUpDown 控件、浮点型、双精度型或整数型,因此您必须将其解析为这些类型之一以满足您的最需要。
如果你想style数字向上向下,那么在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>
numericUpDown1Style 是一个受保护的字段,其类型是在 C# 或 Visual Basic 的 asp 服务器端代码中定义的 string。
如果你想给它一个 class 而不是给它设置样式,那么 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>
numericUpDown1CssClass 是一个受保护的字段,其类型是在 C# 或 Visual Basic 的 asp 服务器端代码中定义的 string。
如果你想给它设置样式并给它一个类,那么 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)); %>
我想你知道 #2 和 #3 中的 numericUpDown1Style 和 numericUpDown1CssClass 是什么
推荐提示:
如果您的网站包含大量用于您的 asp 服务器端代码的数字上下控件,并且以这种方式创建所有这些控件是不利的,那么您可以将新的“Web 用户控件”项添加到您的网站并将其命名为“NumericUpDown”。然后在其源 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.
如果您为 NumericUpDown 设置样式,那么还要知道在每个 ASP.NET 控件中,您可以在其 ID 之后键入 .Style["style"] = "value"。
如果您也希望能够使用 NumericUpDown 执行此操作,请将受保护字段 style 的类型从 string 更改为 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 + ';')
}
}
}
将此类添加到 Web 用户控件的代码中,并更改 Style 属性:
public string Styles
{
get
{
return this.style.style;
}
set
{
this.style.style = value;
}
}
然后添加属性:
public MyStyle Style
{
get
{
return this.style;
}
}
并从以下位置更改行:
protected string style;
到:
protected readonly MyStyle style = new MyStyle();
不要忘记在 Web User Control 的源代码 html 中,将 this.Style 替换为 this.Styles。
注意:我没有耐心自己测试代码,所以它可能不起作用,所以你必须自己修复它。
至少你明白我的想法。
修复后,您可以编辑我的答案并将错误的代码替换为您的固定代码。
我会非常感激的!
这个 Web 用户控件就是你想要的 ASP NumericUpDown!