Is there an elegant way to compare checkbox and text box using ASP.NET validators?

I have an Asp.Net repeater that contains a text box and a checkbox. I need to add a client side check that checks that when the checkbox is checked, the text box can only accept zero or space.

I would like to use one or more Asp.Net controls to accomplish this in order to ensure consistent display of errors on the client side (errors on the server side are handled by another subsystem).

Asp: CompareValidator doesn't seem flexible enough to do such a complicated comparison, so I stay on the Asp: CustomValidator page.

The problem I am facing is that there seems to be no way to pass user information to a validation function. This is a problem because the ClientIds of the checkbox and text box are unknown to me at runtime (since they are part of the relay).

So ... My options seem like this:

  • Pass the text box and checkbox into CustomValidator in some way (it seems not possible).
  • Find a TextBox via JavaScript based on the arguments passed using CustomValidator. Is it possible that with ClientId is ambiguous?
  • Forget about full validation and release your own JavaScript (allowing me to pass both ClientIds to a user-defined function).

Any ideas on what could be the best way to implement this?

+4
source share
2 answers

I think the best way would be to inherit BaseValidator in a new class and pass these identifiers to your control as attributes. You should be able to resolve identifiers in your validator without knowing the full client-side identifier that is created at runtime. First you should get the data checking on the server, and on the second - the client.

+2
source

Can you put CustomValidator inside the repeater? If not, you can create it dynamically when the relay is connected and the 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 made the code from my head.

0
source

Source: https://habr.com/ru/post/1276833/


All Articles