Creating a Numeric Up/Down Control with a Web Service Backend (VB)
Instead of letting a user type a value into a check box, a numeric up/down control (that exists on Windows and other operating systems) could prove as more comfortable. By default, the NumericUpDown control always increases or decreases a value by 1, but a web service proves more flexibility.
Overview
Instead of letting a user type a value into a check box, a numeric up/down control (that exists on Windows and other operating systems) could prove as more comfortable. By default, the NumericUpDown
control always increases or decreases a value by 1, but a web service proves more flexibility.
Steps
The ASP.NET AJAX Control Toolkit contains the NumericUpDown
extender which automatically adds two buttons to a text box: One for increasing its value, one for decreasing it. However the control also supports a web service call (or page method call). Whenever the up or down button is clicked, the JavaScript code connects to the web server and executes a method there. The method signature is the following one:
[!code-vbMain]
1: Function MethodName(ByVal current As Integer, ByVal tag As String) As Integer
The current
argument is the current value in the text box; the tag
attribute is additional context data that can be set as a property of the NumericUpDown
extender (but is not required).
For this sample, the numeric up/down control shall only allow values that are powers of two: 1, 2, 4, 8, 16, 32, 64, and so on. Therefore, the method executed when the user wants to increase the value must double the old value; the other method must divide value by two. So here is the complete web service:
[!code-aspxMain]
1: <%@ WebService Language="VB" Class="NumericUpDown1" %>
2: Imports System.Web
3: Imports System.Web.Services
4: Imports System.Web.Services.Protocols
5: <System.Web.Script.Services.ScriptService()> _
6: Public Class NumericUpDown1
7: Inherits System.Web.Services.WebService
8: <WebMethod()> _
9: Function Up(ByVal current As Integer, ByVal tag As String) As Integer
10: If current <= 536870912 Then
11: Return current * 2
12: Else
13: Return current
14: End If
15: End Function
16: <WebMethod()> _
17: Function Down(ByVal current As Integer, ByVal tag As String) As Integer
18: If current >= 2 Then
19: Return CInt(current / 2)
20: Else
21: Return current
22: End If
23: End Function
24: End Class
Finally, create a new ASP.NET page. As usual, you need a ScriptManager
control, a TextBox
control and a NumericUpDownExtender
control. For the latter, you have to provide the web service information:
ServiceDownMethod
name of the down web method or page methodServiceDownPath
path to the web service with the down service method; omit if you are using a page methodServiceUpMethod
name of the up web method or page methodServiceUpPath
path to the web service with the up service method; omit if you are using a page method
Here is the complete markup for the page:
[!code-aspxMain]
1: <%@ Page Language="VB" %>
2: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3: <html xmlns="http://www.w3.org/1999/xhtml">
4: <head id="Head1" runat="server">
5: <title>Control Toolkit</title>
6: </head>
7: <body>
8: <form id="form1" runat="server">
9: <asp:ScriptManager ID="asm" runat="server" />
10: <div>
11: How many MB do you want? <asp:TextBox ID="TextBox1" Text="32" runat="server" />
12: <ajaxToolkit:NumericUpDownExtender ID="nud" runat="server"
13: TargetControlID="TextBox1" Width="100"
14: ServiceUpPath="NumericUpDown1.vb.asmx" ServiceDownPath="NumericUpDown1.vb.asmx"
15: ServiceUpMethod="Up" ServiceDownMethod="Down" />
16: </div>
17: </form>
18: </body>
19: </html>
If you run the page, notice how the value in the text box always doubles when you click on the upper button, and is halved when you click on the lower button.
Only numbers that are a power of 2 appear (Click to view full-size image)
|