GIDForums  

Go Back   GIDForums > Computer Programming Forums > .NET Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 06-Jun-2008, 04:38
Annon777 Annon777 is offline
New Member
 
Join Date: Apr 2008
Posts: 4
Annon777 is on a distinguished road

C#: i need the Textbox to allow integer only


guys,

I'm building an ASP .net webpage using Microsoft Visual C#. I have a textbox, a label and a command button. I need to make an error checking code so that the textbox only allow integers to be entered into it. But it should be checked when the command button is clicked which then the error should be displayed using the label..

Is there any specific commands/keywords i need to use?
  #2  
Old 09-Jun-2008, 01:01
netnut netnut is offline
Member
 
Join Date: Dec 2005
Location: India
Posts: 174
netnut will become famous soon enough

Re: C#: i need the Textbox to allow integer only


I don't know of any in built mechanism for it but you may try by casting the text in the text box to an int. And then catch the exception if and when it occurs. Putting the code in the event handler for the button would solve the purpose.

Netnut.
  #3  
Old 16-Jun-2008, 10:11
erniegerdie erniegerdie is offline
Awaiting Email Confirmation
 
Join Date: Feb 2004
Location: England
Posts: 93
erniegerdie has a spectacular aura abouterniegerdie has a spectacular aura about

Re: C#: i need the Textbox to allow integer only


Aaron you should look at using a RegularExpressionValidator. The validator is placed on the ASPX page, you indicate which control you are validating with the ControlToValidate property

Something like this:
Code:
<asp:RegularExpressionValidator id="RegularExpressionValidator1" ControlToValidate="TextBox1" ValidationExpression="^\d+$" Display="Static" ErrorMessage="value must be an integer" runat="server"/>

On your button you would set the CausesValidation property to true.

Code:
<asp:Button ID="SomeButton" Text="Submit" CausesValidation="True" runat="server"/>

I wouldn't suggest handling exceptions for this. Exceptions are expensive, only use exception handling where it is necessary.
 
 

Recent GIDBlogVista ?Widgets? on Windows XP by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Integer overflow check? s.hanson C++ Forum 2 24-Sep-2007 22:48
Textbox in Form xtinct MS Visual C++ / MFC Forum 5 03-Jan-2007 04:17
Fortran problem... Justin Fox Miscellaneous Programming Forum 6 24-Oct-2006 15:30
Operator Overloading the_crazyman C++ Forum 4 02-Jun-2005 07:43
Help with integer inputs jrl134 C++ Forum 1 20-Feb-2005 00:36

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 14:30.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.