Solved

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

Posted on 2010-11-14
5
773 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

726 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