开发者

Required Field Validator incorrectly failing validation on disabled textbox

A required field validator seems to always to fire when 开发者_如何学JAVAthe associated textbox is disabled (whether the textbox contains text or not).

When the textbox is enabled the validator behaves correctly.

Can anybody tell me why?

I've tried disabling the required field validator with ValidatorEnable but that seems to make no difference.

Here's the relevant HTML from the page (cut down):

    <tr id="trBrokerNetID" runat="server">
    <td>
        <cc1:mitextbox id="txtBrokerNetID" runat="server" cssclass="bodytext" width="220px" maxlength="20" onBlur="JavaScript:CheckBrokerBranch(false);"></cc1:mitextbox>
        <asp:requiredfieldvalidator id="rfvBrokerNetID" runat="server" width="1px" errormessage="BrokerNetID - Please supply a value" controltovalidate="txtBrokerNetID">*</asp:requiredfieldvalidator>
    </td>
</tr>

Any ideas gratefully received.


Now what I didn't know was that when a control is disabled on the clientside it doesn't get included in the postback.

Which is why the serverside validation was firing. As far as it was concerned the control was empty.

The soolution is to use the readOnly property rather than the disabled property.

Now to figure out how to style the control to make it have the same look as if it was disabled.


ASP.NET's validators work in mysterious ways :D

First of all it is dangerous to use the id of an ASP.NET control to access it in jQuery. If you place the control in a repeater or wrap the page in a master page, then the id of the html element will be something different than the id you specified. Use class to access the element instead.

If ASP.NET validators want the field to be enabled then you must try another approach. My suggestion would be this:

1. Add a class to the textbox, that makes it looks disabled:

$("#txtBrokerNetID").addClass("thisClassMakesItLookDisabled");

2. Add an event that checks on focus to the textbox and blurs it if there is focus:

$("#txtBrokerNetID").focus(function() {
  $(this).blur();
});

Now the field behaves as if it is disabled and the validator works.


One option you can choose, is to set a ValidationGroup than the one the form uses, an then, when validating the form call Page_ClientValidate('text_validation_group') if needed. That way client validation won't get in the way.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜