?
Solved

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

Posted on 2010-11-14
5
Medium Priority
?
775 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 1000 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

Industry Leaders: 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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month11 days, 11 hours left to enroll

752 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