问题
I have a repeater that holds a Radio button list, a textbox and a requiredfieldvalidator control. The Radiobuttonlist holds the following:
Meets (Value M)
Above (Value A)
Below (Value B)
N/A (Value(N/A)
The textbox is set to allow the user to input some justification for their answer, and the requiredfieldvalidator makes sure that the textbox is not empty, simple enough. However the scope has changed a bit and now the client wants the requiredfieldvalidator to fire ONLY if the answer is not "M".
I have tried setting a Load method for the validator, however when the validator loads it has no idea what the answer is in the RBL because it is fired before the user answers. I do not think that I can do a postback, because the repeater is handled within a Modal popup and if I do a postback I am not sure what will happen. How can I set my validator to false only if the user selects something other than "Meets"?
回答1:
try this:
java script:
<script type="text/javascript">
function UpdateValidator(radioID, validatorid) {
//enabling the validator only if the checkbox is checked
var enableValidator = $("#" + radioID).is(":checked");
ValidatorEnable(validatorid, enableValidator);
}
</script>
cs code:
protected void myRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
RadioButton rd = e.Item.FindControl("myRb") as RadioButton;
RequiredFieldValidator rfv = e.Item.FindControl("myRfv") as RequiredFieldValidator;
// you can just pass "this" instead of "myDiv.ClientID" and get the ID from the DOM element
rd.Attributes.Add("onclick", "UpdateValidator('" + rd.ClientID + "','" + rfv.ClientID + "');");
}
}
来源:https://stackoverflow.com/questions/10432090/conditional-requiredfieldvalidator-inside-of-a-repeater