[英]How do I redirect with a Drop Down List, and not a Button?
I am looking into using only a ddl to run my query, not a ddl and a Button_Click function. 我期待只使用ddl来运行我的查询,而不是ddl和Button_Click函数。 I am yet to find what to do. 我还没找到该怎么做。 How do I do this? 我该怎么做呢?
In your as(p/c)x: 在你的(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>
The "AutoPostBack" property tells ASP.NET to wire up a client-side (javascript) command that submits the form as soon as the drop down list changes, instead of waiting for a button click. “AutoPostBack”属性告诉ASP.NET连接一个客户端(javascript)命令,该命令在下拉列表更改后立即提交表单,而不是等待按钮单击。
And in your codebehind, the event handler we referenced in the "OnSelectedIndexChanged" property will get fired: 在你的代码隐藏中,我们在“OnSelectedIndexChanged”属性中引用的事件处理程序将被触发:
protected void SelectionChanged(object sender, EventArgs e)
{
Response.Redirect(((DropDownList)sender).SelectedValue);
}
Set the AutoPostBack property to true, then hook into the OnSelectedIndexChanged event 将AutoPostBack属性设置为true,然后挂钩到OnSelectedIndexChanged事件
<asp:DropDownList
id="dropDownList1"
runat="server"
AutoPostBack="true"
OnSelectedIndexChanged="dropDownList1_SelectedIndexChanged" />
Server Side 服务器端
void dropDownList1_SelectedIndexChanged
(Object sender, EventArgs e) {
//run your query
}
Ensure your Drop down list has it's "AutoPostback" property set to true. 确保您的下拉列表将其“AutoPostback”属性设置为true。 This will cause the page to post back when the user selects an item from within the drop-down list. 当用户从下拉列表中选择项目时,这将导致页面回发。 You can respond to this in your code-behind in whichever event you desire, Page_Load, or the DDL's own OnSelectedIndexChanged 无论您想要什么样的事件,Page_Load或DDL自己的OnSelectedIndexChanged,您都可以在代码隐藏中对此做出响应
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.