Eyla
Eyla

Reputation: 5861

Setting ASP.NET control property using jquery script

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>

Upvotes: 1

Views: 6759

Answers (2)

hallie
hallie

Reputation: 2845

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'); 
    }

Upvotes: 0

Francisco Aquino
Francisco Aquino

Reputation: 9117

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.

Upvotes: 1

Related Questions