Solved

want to create a custom textbox control which takes only integer value

Posted on 2010-11-14
5
771 Views
Last Modified: 2012-05-10
i want to create a custom textbox control which only takes integer as input. please show step by step how to create such type of custom control.
0
Comment
Question by:dynamicweb09
  • 2
  • 2
5 Comments
 
LVL 6

Expert Comment

by:iandian
ID: 34132409
It might be easier to use a textbox together with a validator control.
Not sure whether you can actually use this, or really need the customized control, but it can't hurt to inform you.
<asp:TextBox ID="textThatShouldBeNumeric" runat="server" Width="250px" />
<asp:RegularExpressionValidator ID="validator" runat="server" ControlToValidate="textThatShouldBeNumeric" ValidationExpression="-?\d*" ErrorMessage="This should be a whole number" Display="Dynamic" />

Open in new window

0
 
LVL 1

Author Comment

by:dynamicweb09
ID: 34133841
i want actually custom control so that it might be used in different application.
0
 
LVL 7

Expert Comment

by:kovilpattiBalu
ID: 34135109
hi,
this will be the format of the class..

1.      Create a new class inherit from "TextBox".

      public class DateTextBox : TextBox
      {      

      }

2.      In "OnInit" method..
      2.1.      set the height, width and all

            this.MaxLength = 10;
            this.Width =100;
            this.Height=20;

      2.1.      Decalre intValidator = new RegularExpressionValidator();
            
      2.2.      Give the validation here..

            dateValidator.ID = "dateValidator_"+this.ClientID;
            dateValidator.EnableViewState = false;
            dateValidator.ControlToValidate = this.ID;
            dateValidator.ValidationExpression = "^\\s*(\\d{1,2})([-./])(\\d{1,2})\\2" +
                  "((\\d{4})|(\\d{2}))\\s*$";
            dateValidator.Display = ValidatorDisplay.Static;      

      2.3.      Add the control
            
            Controls.Add(ranValidator);
            Controls.Add(dateValidator);
            
      code will be like this.. change the validation expression as per your requirements..

      public class DateTextBox : TextBox
      {            
            public string InvalidMessage;
            private RegularExpressionValidator dateValidator;
            private RangeValidator ranValidator;

            public string ClientScript="true";

            protected override void OnInit(EventArgs e)
            {
                  dateValidator = new RegularExpressionValidator();
                  ranValidator= new RangeValidator();
                  
                  this.MaxLength = 10;
                  this.Width =100;
                  this.Height=20;
                  
                  

                  dateValidator.ID = "dateValidator_"+this.ClientID;
                  dateValidator.EnableViewState = false;
                  dateValidator.ControlToValidate = this.ID;
                  dateValidator.ValidationExpression = "^\\s*(\\d{1,2})([-./])(\\d{1,2})\\2" +
                        "((\\d{4})|(\\d{2}))\\s*$";
                  dateValidator.Display = ValidatorDisplay.Static;
                  
                  Controls.Add(ranValidator);
                  Controls.Add(dateValidator);
            }
            
            // If needed add this..
            protected override void OnPreRender(EventArgs e)
            {
                  // Jscript to Add "/" after month and date entered and to validate numeric values
                  Attributes.Add("onkeypress","mydate(this);");
                  
                  Page.RegisterClientScriptBlock("mydate", script);                  
                  base.OnPreRender(e);
            }
            
            protected override void Render(HtmlTextWriter w)
            {
                  base.Render(w);                  
                  ranValidator.RenderControl(w);
                  dateValidator.RenderControl(w);                  
            }
      }
0
 
LVL 1

Author Comment

by:dynamicweb09
ID: 34138653
how will we the control to our .aspx page.I have added the control to the toolbox and drag and drop the control into our page .but showing error "control to validate cant not be blank".
0
 
LVL 7

Accepted Solution

by:
kovilpattiBalu earned 250 total points
ID: 34142484
hi,

Step 1:
=======

Register the class in the .aspx page

ie: <%@ Register TagPrefix="dval" Namespace="WebControls" Assembly="sampleProject" %>

1.      Namespace="WebControls"       -> is the folder name which your class exists
2.      Assembly="sampleProject"      -> is the Project Name

Step 2:
======

Than create a tag in .aspx page..
ie:

<td>
    <dval:DateVal runat="server" id="dval"></dval:DateVal>
</td>
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question