Conditional RequiredFieldValidator inside of a repeater

孤街醉人 提交于 2019-12-25 05:13:13

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!