This is a very simple task ... You can easily ensure that the user enters a number field. Visual Studio has built support for this (and you can also achieve this through coding). Here's what you need to do: - Switch to the design view from the layout view. Now click on the projector and press Ctrl + Alt + X. From the toolbar that opens, click Verify and drag the comparison checker next to your text box. Go back to the comparison validator and select properties, now find ErrorMessage and write "Alert: Type only Number". In the control to check, select your control. In the Statement, select DataTypeCheck and in the Type field select Integer. That's all. Also with the help of encoding you can get it as: -
protected void Button1_Click(object sender, EventArgs e) { int i; if (!int.TryParse(textBox.Text, out i)) { Label.Text ="This is a number only field"; return; }
source share