Fork me on GitHub

Thursday, February 22, 2007

Use custom validators instead of Attributes.Add() when using PostBackUrl

posted by Lars Hundebøl # 4:38 p.m.
I recently had a problem regarding using a button's PostBackUrl and client script validation in ASP.NET. I had a form which had to Postback to another page on another website (a website used to host a secure page for credit cart transations), but before proceeding to postback to the remote page, the user must check off that they have read the disclaimer. So I thought to myself to validate that the user has checked off, that they have read the disclaimer, I would add a simple client script, and then add this client script to the buttons onclick attribute. btnBuy.Attributes.Add("onclick", "javascript:validateCheckOff();"); However when using the PostBackUrl on a button, this automatically generate some client script code to create a postback on the onclick attribute, which in turn means that when using the Attributes.Add() method, this will overwrite the postbackurl rendered client script. In order to fix the issue above, one should instead use a custom validator control. I found this article explaining how to create a custom validator control for a checkbox control.

Leave a comment

Your name

Your e-mail (will not be displayed)†

†Your e-mail will never be displayed and only used on the site to display your gravatar (if you got one), and as a means to potentially get back to you, if I have any feedback or questions regarding your comment.

all posts