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

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

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....