How to call a private void keypress event handler from client side using C# or ASP.net to allow only numbers into textbox -


i've never learned either asp.net or c# , yet i'm trying create textbox using asp.net allows numbers entered. i'm using following code define textbox in default.aspx:

<asp:textbox runat="server" id="studentno"         autopostback="true"              ontextchanged="studentno_keypress"         style="margin-left:20px; width:70px;"/> 

the ontextchanged isn't calling server side page (default.aspx.cs) , here code i'm using that:

using system; using system.collections; ... using system.data; using system.windows.forms  private void studentno_keypress(object sender, keypresseventargs e) {    if (!char.iscontrol(e.keychar)       && !char.isdigit(e.keychar)       && e.keychar != '.')    {       e.handled = true;    } } 

i've tried give relevant bits correct indentation fear isn't going work. i'm sorry if basic question, said, i've never coded in either of these languages. i'm not allowed use javascript or ajax, c# , asp.net.

any suggestions or alternative solutions fantastic. i've tried using rangevalidator, regularexpressionvalidator, requiredfieldvalidator , customvalidator, none of use whatsoever.

thanks in advance, hugely appreciated. if require more information, please let me know.

you headed down correct path asp.net validation server controls.

use regularexpressionvalidator way:

<asp:textbox id="txtnumber" runat="server" />  <asp:regularexpressionvalidator id="regexvalidator" runat="server"     controltovalidate="txtnumber" errormessage="you must enter number"     validationexpression="^\d+$" /> 

and if want require number entered, add 1 too:

<asp:requiredfieldvalidator id="reqvalidator" runat="server"      controltovalidate="txtnumber" errormessage="you must enter number" /> 

note "controltovalidate" attribute must set control validating. example value "studentno"


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -