【问题标题】:Is there a numeric UpDown control for ASP.NET?ASP.NET 是否有数字 UpDown 控件?
【发布时间】:2013-05-08 10:41:06
【问题描述】:

有没有一种方法可以在不使用 JavaScript 的情况下在 ASP.NET 中使用数字 updown?

如果没有,还有其他选择吗?

【问题讨论】:

  • 替代什么问题? “不使用 Javascript”是什么意思?纯HTML?没有:tjvantoll.com/2012/07/15/…
  • @CasparKleijne 在不使用 Javascript 的情况下获取此类控件的替代方法
  • 我的网络表单中有一个名为 Quantity 的字段。文本框需要大量验证器来确保它是数字的,所以我认为数字 updown 对象对于界面和代码来说更加用户友好。 updown 对象的任何替代方法?除了文本框?
  • @LukeHennerly,ASP.NET 在很大程度上依赖于 javascript。

标签: html asp.net


【解决方案1】:

我试图做同样的事情,结果 asp 文本框有一个选项。对我有用的是:

<asp:TextBox TextMode="Number" runat="server" min="0" max="20" step="1"/>

这给了我一个文本框,当鼠标悬停在它上面或获得焦点时,它会显示上下控件,并且只允许从最小值到最大值的数字。 它的工作原理与

相同
<input type="number" min="0" max="20" step="1" />

【讨论】:

【解决方案2】:

请查看 Ajax 控制工具包

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" />

还可以考虑使用PM&gt; Install-Package AjaxControlToolkit 来添加带有NuGet 的引用

【讨论】:

  • -1 OP 明确需要一个不使用 javascript 的解决方案。此解决方案需要 javascript。
  • @CasparKleijne 上面的代码在什么时候使用了javascript?它是纯 HTML,可能是他可用的最佳/唯一解决方案。底层代码可能使用 javascript 但解决方案没有,我想说 OP 不想使用 javascript 的主要原因是因为他们不理解它,而不是因为他们不能使用它。
【解决方案3】:

我认为下面的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 中的 numericUpDown1StylenumericUpDown1CssClass 是什么

推荐提示:

如果您的网站包含大量用于您的 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!

【讨论】:

    【解决方案4】:

    如果您停留在 .NET 4.0 上并且想要使用本机 HTML5 输入类型“数字”(而不是 Ajax 控件工具包中的 NumericUpDown),您可以使用 ASP TextBox 控件与额外的“类型”标签的组合:

    <asp:TextBox runat="server" ID="txtMyTextBox" type="number" min="0" max="10" step="1"/>
    

    如果你想阻止任何文本输入,你甚至可以从 Ajax Control Toolkit 添加一个 FilteredTextBoxExtender:

    <ajaxToolkit:FilteredTextBoxExtender runat="server" TargetControlID="txtMyTextBox" FilterType="Numbers" />
    

    【讨论】:

      猜你喜欢
      • 2012-08-31
      • 2010-10-24
      • 2014-11-03
      • 2020-02-25
      • 1970-01-01
      • 2010-09-08
      • 2011-01-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多