开发者

What is the recommended method to Validate an ASP.net Control with an existing method?

I am wondering what the prefered method of validating user input in asp.net using an existing method call is. I have implemented this a couple of ways now and while they all work I get the sense that there might be a better or "optimal" method?

I have an asp.net textbox

<asp:TextBox ID="myTextBox" runat="server" />

I also have a couple existing methods available to me on the objec that the form will eventually populate and save

public static bool IsNameValid()
public bool IsValid()

I'm wondering how people would wire up those items to a validation control (I'm ass开发者_如何学运维uming customValidator?). I'd like to avoid rewriting the validation in JavaScript (to avoid duplication of code).


Use a CustomValidator, and set the EnableClientScript property to false, forcing it to go to the server for validation. Then in the custom validator's ServerValidate method, set the args.IsValid property to the result of your methods above.

The advantage to using the validator is that when you go to your "submission" method for the form, you can wrap the final processing logic around a If Page.IsValid() block, which will make sure all validators come back as true before processing.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜