如何使用下拉列表重定向,而不是按钮?

MrM*_*MrM 2 .net c# asp.net

我期待只使用ddl来运行我的查询,而不是ddl和Button_Click函数.我还没找到该怎么做.我该怎么做呢?

Rex*_*x M 6

在你的(p/c)x:

<asp:DropDownList runat="server" 
                  id="ddl"
                  OnSelectedIndexChanged="SelectionChanged"
                  AutoPostBack="true">
    <asp:ListItem Text="Page 1" Value="/page1.aspx" />
    <asp:ListItem Text="Page 2" Value="/page2.aspx" />
</asp:DropDownList>
Run Code Online (Sandbox Code Playgroud)

"AutoPostBack"属性告诉ASP.NET连接一个客户端(javascript)命令,该命令在下拉列表更改后立即提交表单,而不是等待按钮单击.

在你的代码隐藏中,我们在"OnSelectedIndexChanged"属性中引用的事件处理程序将被触发:

protected void SelectionChanged(object sender, EventArgs e)
{
    Response.Redirect(((DropDownList)sender).SelectedValue);
}
Run Code Online (Sandbox Code Playgroud)