简体   繁体   English

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

[英]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.

相关问题 如何使重定向后的下拉列表选项突出显示? - How do I keep the Drop Down List option to be highlighted after a Redirect? 使用“SAPFEWSELib”的 C# SAP 自动化。 如何按下下拉列表中的按钮? - C# SAP automation using “SAPFEWSELib”. How do I press a button from a drop down list? 如何控制下拉列表中显示的屏幕保护程序名称? - How do I control the screensaver name shown in the drop down list? 如何将下拉列表选择发送到控制器 - How do I send drop down list selection to my controller 如何在页面加载时强制下拉列表选择? - How do I force drop down list selections on page load? 如何根据另一个下拉列表 ASP.NET 的选择填充下拉列表 - How do I fill a drop down-list based on selection by another drop-down list ASP.NET 如何从对象列表中获取字符串值并将其添加到下拉列表中? - How do I take string values from a list of objects and add them to a drop down list? 如何更改下拉列表中按钮的文本 - How to change the text of a button inside a drop down list 如何从下拉列表中获取所选项目并将其提交到我的详细信息视图? - How do I get the selected item from drop down list and submit it to my Details view? 如何让DataGridView组合框一键显示下拉列表? - How do I get DataGridView comboboxes to display their drop down list in one click?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM