2

I have this onchange event in my JS that according to a select list, changes the maxlength for the textboxes under it. Now i want to be able to do this server side just incase if a user has JS turned off.

I have some code behind code but it only works after the selection is made and then the page is refreshed, i want to be able to work without the refresh factor or some sort of auto refresh as soon as the user selects an option from the dropdown.

This is my Code Behind:-

protected void ListPayment_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ListPayment.SelectedIndex == 4)
        {

            TextBoxCCN.MaxLength = 15;
            TextCSS.MaxLength = 4;
        }

        else if (ListPayment.SelectedIndex != 4)
        {

            TextBoxCCN.MaxLength = 16;
            TextCSS.MaxLength = 3;
        }
    }

This is my HTML

<asp:DropDownList ID="ListPayment" runat="server" 
  onchange="ValidateCCN();" 
  OnSelectedIndexChanged="ListPayment_SelectedIndexChanged">
    <asp:ListItem Value="0">Select...</asp:ListItem>
    <asp:ListItem Value="Visa">Visa</asp:ListItem>
    <asp:ListItem Value="MasterCard">MasterCard</asp:ListItem>
    <asp:ListItem Value="Discover">Discover</asp:ListItem>
    <asp:ListItem Value="American Express">American Express</asp:ListItem>
    </asp:DropDownList>
0

1 Answer 1

2

This is what the AutoPostBack property is for:

<asp:DropDownList AutoPostBack="true"...

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listcontrol.autopostback(v=vs.110).aspx

EDIT:

Full revised code:

<asp:DropDownList ID="ListPayment" runat="server" AutoPostBack="true"
  onchange="ValidateCCN();" 
  OnSelectedIndexChanged="ListPayment_SelectedIndexChanged">
    <asp:ListItem Value="0">Select...</asp:ListItem>
                    <asp:ListItem Value="Visa">Visa</asp:ListItem>
                    <asp:ListItem Value="MasterCard">MasterCard</asp:ListItem>
                    <asp:ListItem Value="Discover">Discover</asp:ListItem>
                    <asp:ListItem Value="American Express">American Express</asp:ListItem>
    </asp:DropDownList>
Sign up to request clarification or add additional context in comments.

8 Comments

where do i add that? above the select list?
I've added it to your DropDownList tag in my answer. See my edit for full code.
yep okay, so do i have to move all my code behind code into the page load function? because it isnt working as of now
@rookie26 no, setting AutoPostBack="true" should do the trick. When you change the value in the select list is it causing the page to postback? If not I would look what ValidateCCN() is doing client side as this may be preventing the postback.
Why do you think you'd have to do that as well as the suggestion I've just made?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.