我有一个文本框,可以更改 OnTextChanged 事件中下拉列表的内容。当文本框失去焦点时,此事件似乎会触发。如何在按键或按键事件上实现此操作?
这是我的代码的示例
<asp:TextBox ID="Code" runat="server" AutoPostBack="true" OnTextChanged="Code_TextChanged">
<asp:UpdatePanel ID="Update" runat="server">
<ContentTemplate>
<asp:DropDownList runat="server" ID="DateList" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Code" />
</Triggers>
</asp:UpdatePanel>
因此,在后面的代码中,我在页面加载时绑定了下拉列表。 Code_TextChanged 事件只是重新绑定下拉列表。我希望这种情况发生在每次按键时,而不是当文本框失去焦点时。
我最近继承了这段代码,这对我来说不是理想的方法,但时间限制阻止我在网络服务方法中重写它。
我尝试使用 jQuery 绑定“keyup”事件以匹配文本框的“change”事件,但这仅适用于按下的第一个键。
这将解决您的问题。逻辑与凯尔建议的解决方案相同。
看看这个。
<head runat="server">
<title></title>
<script type="text/javascript">
function RefreshUpdatePanel() {
__doPostBack('<%= Code.ClientID %>', '');
};
</script>
<asp:TextBox ID="Code" runat="server" onkeyup="RefreshUpdatePanel();" AutoPostBack="true" OnTextChanged="Code_TextChanged"></asp:TextBox>
<asp:UpdatePanel ID="Update" runat="server">
<ContentTemplate>
<asp:DropDownList runat="server" ID="DateList" />
<asp:TextBox runat="server" ID="CurrentTime" ></asp:TextBox>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Code" />
</Triggers>
</asp:UpdatePanel>
后面的代码是这样的......
protected void Code_TextChanged(object sender, EventArgs e)
{
//Adding current time (minutes and seconds) into dropdownlist
DateList.Items.Insert(0, new ListItem(DateTime.Now.ToString("mm:ss")));
//Setting current time (minutes and seconds) into textbox
CurrentTime.Text = DateTime.Now.ToString("mm:ss");
}
我添加了额外的文本框来查看操作的变化,请删除文本框。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)