tags:

views:

90

answers:

2

I'm trying to set new value for asp.net control property when I check a checkbox.

In my case I have RegularExpressionValidator asp.net control and when I check the box I would have new value for the properties ErrorMessage and ValidationExpression.

I tried this code but did not work with me.

please help.

here is my code: ...............................................................................

<script src="js/jquery-1.4.1.js" type="text/javascript"></script>

<script type="text/javascript">

    function chkinput() {


        if ($('#<%=chkIntphoneHome.ClientID %>').is(':checked')) {

            $('#<%=REV_HomePhone.ClientID %>').attr('ErrorMessage', 'new msg');
            $('#<%=REV_HomePhone.ClientID %>').attr('ValidationExpression', 'new exp'); 
        }
    }

 </script>
<asp:CheckBox ID="chkIntMobileHome" runat="server" Style="position: absolute; top: 200px;
                    left: 535px;" Text="Internation Code" AutoPostBack="True"
                    />
<asp:TextBox ID="txtHomePhone" runat="server" Style="top: 147px; left: 543px; position: absolute;
                    height: 22px; width: 128px" ></asp:TextBox>
                <asp:RegularExpressionValidator ID="REV_HomePhone" runat="server" ErrorMessage="Please enter valid Home Phone"
                    Style="position: absolute; top: 177px; left: 476px; width: 226px;" 
                    Display="Dynamic" ControlToValidate="txtHomePhone"
                    ValidationExpression="\d{3}?\d{3}\d{4}"></asp:RegularExpressionValidator>
+1  A: 

You need to deal with that in the code behind, it wont work the way you are doing. jQuery will add those attributes after the page has been rendered, thus, to the output, having no effect on your .NET control.

Edit: suggestion, you can wrap your content with an updatePanel so you get the partial postback effect you might be wanting to have with jQuery, you can then hook the OnLoad event in the updatePanel and see if it is an ajaxRequest, and update your page accordingly.

F.Aquino
is this do the same with ajax control ...can not change any properties after page loaded unless using code behind??
Eyla
the validation control (when enabled for client-side checks) DOES have javascript code containing the rule, the problem is that you will only change the rule on the client and not the server (when the user submits, if you check for something like Page.IsValid it wont work), so you would have 2 different rules for the same control. That's still only in case you are able to find the proper javascript code and replace/inject it, I do not know how to do that though.
F.Aquino
A: 

I think you are changing the attribute of the textbox and not the regularexpressionvalidator control. Should it be something like this?

        if ($('#<%=chkIntphoneHome.ClientID %>').is(':checked')) {

        $('#<%=REV_HomePhone.ClientID %>').attr('ErrorMessage', 'new msg');
        $('#<%=REV_HomePhone.ClientID %>').attr('ValidationExpression', 'new exp'); 
    }
hallie
it was typing mistake I corrected but same thing nothing happend.
Eyla
Nope, that's not the problem. You can only set properties of server controls on the server.
Slavo