asp.net – 如何在KeyUp上进行文本框回发?

我有一个TextBox可以更改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>

所以在codebehind中,我绑定页面加载的下拉列表。 Code_TextChanged事件只是重新绑定下拉列表。我希望这可以在每个按键上发生,而不是当文本框失去焦点时。

我最近继承了这段代码,这不是为我这样做的理想方法,但时间限制阻止我在Web服务方法中重写。

我已经尝试使用jQuery绑定“keyup”事件来匹配文本框的“更改”事件,但这只适用于第一个按键。

解决方法

这将解决您的问题。逻辑与凯尔提出的解决方案相同。

看看这个。

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

添加了额外的文本框来查看更改的操作,删除文本框。

相关文章

这篇文章主要讲解了“WPF如何实现带筛选功能的DataGrid”,文...
本篇内容介绍了“基于WPF如何实现3D画廊动画效果”的有关知识...
Some samples are below for ASP.Net web form controls:(fr...
问题描述: 对于未定义为 System.String 的列,唯一有效的值...
最近用到了CalendarExtender,结果不知道为什么发生了错位,...
ASP.NET 2.0 page lifecyle ASP.NET 2.0 event sequence cha...