【问题标题】:How to trigger a OnChange type of event in ASP.NET C#如何在 ASP.NET C# 中触发 OnChange 类型的事件
【发布时间】:2014-08-26 19:37:16
【问题描述】:

我的 JS 中有这个 onchange 事件,它根据选择列表更改它下面的文本框的最大长度。现在我希望能够在服务器端执行此操作,以防万一用户关闭了 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>

【问题讨论】:

    标签: c# asp.net code-behind


    【解决方案1】:

    这就是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>
    

    【讨论】:

    • 我在哪里添加?在选择列表上方?
    • 我已在我的回答中将其添加到您的DropDownList 标记中。有关完整代码,请参阅我的编辑。
    • 是的,我必须将代码后面的所有代码移动到页面加载函数中吗?因为它现在不工作
    • @rookie26 不,设置AutoPostBack="true" 应该可以解决问题。当您更改选择列表中的值时,是否会导致页面回发?如果不是,我会看看ValidateCCN() 在客户端做什么,因为这可能会阻止回发。
    • 你为什么认为你必须这样做以及我刚刚提出的建议?
    猜你喜欢
    • 2016-01-27
    • 2011-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多