微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

原文地址:https://www.jb51.cc/aspnet/252924.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐