Question

I have an Asp.Net repeater, which contains a textbox and a checkbox. I need to add client-side validation that verifies that when the checkbox is checked, the textbox can only accept a value of zero or blank.

I would like to use one or more of Asp.Net's validator controls to accomplish this, to provide a consistent display for client side errors (server-side errors are handled by another subsystem).

The Asp:CompareValidator doesn't seem to be flexible enough to perform this kind of complex comparison, so I'm left looking at the Asp:CustomValidator.

The problem I'm running into is that there doesn't seem to be any way to pass custom information into the validation function. This is an issue because the ClientIds of the checkbox and the textbox are unknown to me at runtime (as they're part of a Repeater).

So... My options seem to be:

  1. Pass the textbox and checkbox to the CustomValidator somehow (doesn't seem to be possible).
  2. Find the TextBox through JavaScript based on the arguments passed in by the CustomValidator. Is this even possible, what with the ClientId being ambiguous?
  3. Forget validation entirely, and emit custom JavaScript (allowing me to pass both ClientIds to a custom function).

Any ideas on what might be a better way of implementing this?

Was it helpful?

Solution

I think the best way would be to inherit BaseValidator in a new class, and pass those IDs to your control as attributes. You should be able to resolve the IDs within your validator, without knowing the full client side ID that is generated at runtime. You should get the data validating on the server first, and on the client second.

OTHER TIPS

Can you not put the CustomValidator inside the repeater? If not, you can create it dynamically when the repeater is bound and user FindControl()

protected MyDataBound(object sender, RepeaterItemEventArgs e) {
  (CheckBox)cb = (CheckBox)e.Item.FindControl("myCheckboxName");
  (TextBox)tb = (TextBox)e.Item.FindControl("myTextBox");
}

...or something like that. I did the code off the top of my head.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top