Solved

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

Posted on 2010-11-14
5
774 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

729 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