gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 10:40:32 31 4
gpt4 key购买 nike

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

最佳答案

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

关于c# - 如何在 ASP.NET C# 中触发 OnChange 类型的事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25504876/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com