Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You are doing the parse on the wrong thing. <code>arg.ToString()</code> will give you the string of <code>"System.Web.UI.WebControls.ServerValidateEventArgs"</code>, when you actually want to the <code>arg.Value</code> (which is already a string)</p> <p>So, instead of...</p> <pre><code>bool isNum = int.TryParse(arg.ToString(), out num); </code></pre> <p>Change it to (note the <code>.Value</code>)...</p> <pre><code>bool isNum = int.TryParse(arg.Value, out num); </code></pre> <p>Although it is a lot more complex than it actually needs to be. The whole thing could be rewritten a lot more comprehensively like this...</p> <pre><code>protected void intValidate_Validate(object sender, ServerValidateEventArgs arg) { int num; arg.IsValid = int.TryParse(are.Value, out num); } </code></pre> <p>This is because <a href="http://msdn.microsoft.com/en-us/library/f02979c7.aspx" rel="nofollow">TryParse</a> will return a true if the conversion was successful, and false otherwise.</p> <p>And as a final thought, all this could be achieved using the <code>&lt;asp:RequiredFieldValidator ...&gt;</code>, <code>&lt;asp:CompareValidator Operator="DataTypeCheck" Type="Integer" ...&gt;</code> and <code>&lt;asp:RangeValidator ...&gt;</code> validator controls.</p> <hr> <p><strong>UPDATE</strong></p> <p>As the OP points out, he doesn't want the error when when the textbox is empty, so instead try this...</p> <pre><code>protected void intValidate_Validate(object sender, ServerValidateEventArgs arg) { if (args.Value.Length &gt; 0) { int num; arg.IsValid = int.TryParse(are.Value, out num); } else { arg.IsValid = true; } } </code></pre> <p>And has already been pointed out by @HansKesting... this will only ever be called if all client-side validation is passed. As soon as client-side validation fails, the submission of the form back to the server is also cancelled.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload