Solved

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

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now