如何在ASP.NET C中触发OnChange类型的事件#

时间:2014-08-26 11:35:07

标签: c# asp.net code-behind

我在JS中有这个onchange事件,根据选择列表,更改其下的文本框的maxlength。现在我希望能够在用户关闭JS的情况下执行此服务器端。

我有一些代码背后的代码,但它只在选择完成后才能工作,然后刷新页面,我希望能够在没有刷新因子的情况下工作或者在用户选择选项时立即进行某种自动刷新从下拉列表。

这是我的代码背后: -

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

这是我的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>

1 个答案:

答案 0 :(得分:2)

这是AutoPostBack属性的用途:

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

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

编辑:

完整修订代码:

<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>