服务器端Web表单中的listbox keypress事件

时间:2012-12-27 11:55:38

标签: c# asp.net

我有一个列表框(GroupListBox),我想在C#网络表单中捕获此列表框中的回车键按事件。我尝试了很多方法,但没有人为我工作。

1 个答案:

答案 0 :(得分:1)

如果你想在服务器端这样做,你应该使用__doPostBack来获得回发。

您的列表框 -

<asp:ListBox ID="ListBox1" runat="server" onkeydown="enterKeyPressed(event);">
            <asp:ListItem>one</asp:ListItem>
            <asp:ListItem>two</asp:ListItem>
            <asp:ListItem>Three</asp:ListItem>
 </asp:ListBox>

Javascript功能 -

<script language="javascript" type="text/javascript">
    function enterKeyPressed(e) {
         if (window.event) {
            e = window.event;
        }
        if (e.keyCode == 13) {
            __doPostBack('ShowMessage', '');
        }
    }
</script>

在页面的页面加载事件 -

protected void Page_Load(object sender, EventArgs e)
    {
        ClientScript.GetPostBackEventReference(this, string.Empty);
        if (Request.Params["__EVENTTARGET"] != null)
            if (Request.Params["__EVENTTARGET"] == "ShowMessage")
            {
                Label1.Text = "Enter is pressed";
            }
 }
相关问题