使用 Web 服务后端创建数字增大/减小控件 (VB)

作者 :Christian Wenz

下载 PDF

Windows 和其他操作系统) 上存在的数字向上/向下控件 (可以证明更舒适,而不是让用户在检查框中键入值。 默认情况下,NumericUpDown 控件始终将值增加或减少 1,但 Web 服务具有更大的灵活性。

概述

Windows 和其他操作系统) 上存在的数字向上/向下控件 (可以证明更舒适,而不是让用户在检查框中键入值。 默认情况下, NumericUpDown 控件始终将值增加或减少 1,但 Web 服务具有更大的灵活性。

步骤

ASP.NET AJAX 控件工具包包含 NumericUpDown 扩展器,该扩展器会自动向文本框添加两个按钮:一个用于增加其值,一个用于减小它的值。 但是, 控件还支持 web 服务调用 (或页面方法调用) 。 只要单击向上或向下按钮,JavaScript 代码就会连接到 Web 服务器,并在其中执行方法。 方法签名如下:

Function MethodName(ByVal current As Integer, ByVal tag As String) As Integer

参数 current 是文本框中的当前值; tag 属性是其他上下文数据,可以设置为扩展程序 (的属性 NumericUpDown ,但) 不需要。

对于此示例,数值向上/向下控制应只允许两个幂的值:1、2、4、8、16、32、64 等。 因此,当用户想要增加值时执行的方法必须将旧值翻倍;另一个方法必须将值除以 2。 下面是完整的 Web 服务:

<%@ WebService Language="VB" Class="NumericUpDown1" %>
Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
<System.Web.Script.Services.ScriptService()> _
Public Class NumericUpDown1
 Inherits System.Web.Services.WebService
 <WebMethod()> _
 Function Up(ByVal current As Integer, ByVal tag As String) As Integer
 If current <= 536870912 Then
 Return current * 2
 Else
 Return current
 End If
 End Function
 <WebMethod()> _
 Function Down(ByVal current As Integer, ByVal tag As String) As Integer
 If current >= 2 Then
 Return CInt(current / 2)
 Else
 Return current
 End If
 End Function
End Class

最后,创建新的 ASP.NET 页。 像往常一样,你需要控件 ScriptManagerTextBox 控件和 NumericUpDownExtender 控件。 对于后者,必须提供 Web 服务信息:

  • ServiceDownMethod down Web 方法或页面方法的名称
  • ServiceDownPath 使用 down 服务方法指向 Web 服务的路径;如果使用页面方法,则省略
  • ServiceUpMethod up Web 方法或页面方法的名称
  • ServiceUpPath 使用 up 服务方法指向 Web 服务的路径;如果使用页面方法,则省略

下面是页面的完整标记:

<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
 <title>Control Toolkit</title>
</head>
<body>
 <form id="form1" runat="server">
 <asp:ScriptManager ID="asm" runat="server" />
 <div>
 How many MB do you want? <asp:TextBox ID="TextBox1" Text="32" runat="server" />
 <ajaxToolkit:NumericUpDownExtender ID="nud" runat="server"
 TargetControlID="TextBox1" Width="100"
 ServiceUpPath="NumericUpDown1.vb.asmx" ServiceDownPath="NumericUpDown1.vb.asmx"
 ServiceUpMethod="Up" ServiceDownMethod="Down" />
 </div>
 </form>
</body>
</html>

如果运行该页,请注意单击上部按钮时文本框中的值始终翻倍,单击下部按钮时,该值将减半。

仅显示幂为 2 的数字

仅显示 2 幂的数字 (单击以查看全尺寸图像)