开发者

CustomValidator Javascript function thinks that the radio button is not checked

I have a user control which contains a series of radio buttons and some text fields on it. If a specific radio button is checked I want to validate the contents of two text fields.

My control markup contains this:

<asp:TextBox ID="FromDate" runat="server" Columns="8"></asp:TextBox>
<asp:TextBox ID="ToDate" runat="server" Columns="8"></asp:TextBox>
<asp:RadioButton runat="Server" ID="RadioBetween" GroupName="Date" CssClass="date_group_options_control_radio"/>
<asp:CustomValidator ID="DateValidator" runat="server" Display="Dynamic" ClientValidationFunction="ValidateDateFields_Client" OnServerValidate="ValidateDateFields"></asp:CustomValidator>

<script type="text/javascript">
function ValidateDateFields_Client(source, args)
{
    debugger;
    var bR开发者_Go百科adioBetweenSelected = false;

    var oRadio = document.getElementById('<%=RadioBetween.ClientID%>');
    if (oRadio != null && (oRadio.checked == true || oRadio["checked"] == true))
    {
        bRadioBetweenSelected = true;
    }

    if (bRadioBetweenSelected)
    {
        var oFromDate = document.getElementById('<%=FromDate.ClientID%>');
        var oToDate = document.getElementById('<%=ToDate.ClientID%>');

        if (oFromDate != null && oToDate != null)
        {
            var sFromDate = oFromDate.value;
            var sToDate = oToDate.value;

            source.innerHTML = ValidateFromToDate(sFromDate, sToDate, args);
        }
        else
        {
            args.IsValid = true;
        }
    }
    else
    {
        args.IsValid = true;
    }
}
</script>

ValidateFromToDate just checks the values and makes sure that they are sane.

It never goes into the check though because I can't seem to tell whether RadioBetween is checked or not. The server side code, however, works fine (which does exactly the same thing).

If I manually set Checked to be 'true' in the control it works as expected.

How can I validate this client side to save a trip to the server? What am I doing wrong? This should be fairly trivial code :-)


It is not clear why you use the RadioButton. Usually there are more than one RadioButtons with same GroupName. Maybe you just need a CheckBox?


You can localize problem with the help of FireBug or other JavaScript debugging tool. Put breakpoint(s) into your function and see what values are inserted into variables on each execution step.


Javascript Get or Set Checked Radio Value


Turns out that this is because I have two versions of the same control in one page. The hidden one (deep in the depths of a modal dialog) is conflicting with the main one. The client validation function is being called twice on the controls in the hidden control.

Why this is remains to be seen but when commenting out the second instance of the control it all works fine.

Every day ASP.NET confuses me :-)

The reason is discussed here: User Control with Client + Server Side CustomValidation; Wrong Client side validator is picked

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜