"
 
 
 
ASP.NET (snapshot 2017) Microsoft documentation and samples

Creating a Numeric Up/Down Control with a Web Service Backend (C#)

by Christian Wenz

Download Code or Download PDF

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-csharpMain]

   1:  public int MethodName(int current, string tag) {}

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="C#" Class="NumericUpDown1" %>
   2:  using System;
   3:  using System.Web;
   4:  using System.Web.Services;
   5:  using System.Web.Services.Protocols;
   6:  [System.Web.Script.Services.ScriptService]
   7:  public class NumericUpDown1 : System.Web.Services.WebService
   8:  {
   9:   [WebMethod]
  10:   public int Up(int current, string tag)
  11:   {
  12:   if (current <= 536870912)
  13:   {
  14:   return current * 2;
  15:   }
  16:   else
  17:   {
  18:   return current;
  19:   }
  20:   }
  21:   [WebMethod]
  22:   public int Down(int current, string tag)
  23:   {
  24:   if (current >= 2)
  25:   {
  26:   return (int)(current / 2);
  27:   }
  28:   else
  29:   {
  30:   return current;
  31:   };
  32:   }
  33:  }

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:

Here is the complete markup for the page:

[!code-aspxMain]

   1:  <%@ Page Language="C#" %>
   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.cs.asmx" 
  15:   ServiceDownPath="NumericUpDown1.cs.asmx"
  16:   ServiceUpMethod="Up" ServiceDownMethod="Down" />
  17:   </div>
  18:   </form>
  19:  </body>
  20:  </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

Only numbers that are a power of 2 appear (Click to view full-size image)

Next





Comments ( )
<00>  <01>  <02>  <03>  <04>  <05>  <06>  <07>  <08>  <09>  <10>  <11>  <12>  <13>  <14>  <15>  <16>  <17>  <18>  <19>  <20>  <21>  <22>  <23
Link to this page: //www.vb-net.com/AspNet-DocAndSamples-2017/aspnet/web-forms/overview/ajax-control-toolkit/numericupdown/creating-a-numeric-up-down-control-with-a-web-service-backend-cs.htm
<SITEMAP>  <MVC>  <ASP>  <NET>  <DATA>  <KIOSK>  <FLEX>  <SQL>  <NOTES>  <LINUX>  <MONO>  <FREEWARE>  <DOCS>  <ENG>  <CHAT ME>  <ABOUT ME>  < THANKS ME>