c# - 如何从下拉列表框中获取值

标签 c# asp.net winforms

我想要的是从下拉列表中的所选项目中获取值,并使用它在按钮单击事件中进行查询(选择表格)。

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DropDownList1.SelectedIndex == 1)
        {
            using (SqlConnection con = new SqlConnection(DBcon))
            {

               SqlCommand sqlCommand = new SqlCommand("Select * from tbl_WinApps_FileHeader");
               sqlCommand.Connection = con;

               SqlDataReader read = sqlCommand.ExecuteReader();

                GridView1.DataSource = read;
                GridView1.DataBind();


            }
        }
    }

  <asp:DropDownList ID="DropDownList1" runat="server" 
        onselectedindexchanged="DropDownList1_SelectedIndexChanged">
        <asp:ListItem>ER00 - File Header</asp:ListItem>
        <asp:ListItem>ER01 - Expense Report Trans</asp:ListItem>
        <asp:ListItem>ER02 - Expense Report Item Trans</asp:ListItem>
        <asp:ListItem>ER03 - Expense Report Item Tax Trans</asp:ListItem>
        <asp:ListItem>ER04 - Expense Report Item</asp:ListItem>
    </asp:DropDownList>

最佳答案

DropDownList1.SelectedValue.ToString();

或者

DropDownList1.Text;

或者

DropDownList1.SelectedValue.ToString();

关于c# - 如何从下拉列表框中获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15378602/

相关文章:

c# - 无法以编程方式连接到 Mysql

.net - 使用 "right".NET Framework 版本

ASP.NET:当我的页面执行存储过程时,如何显示加载图像?

C#,FormBorderStyle :None Restore Issue

C# 将列表加载到文本框 WinForms

c# - asp 标签内的 foreach 变量不存在

c# - 如何使用 Visual Web Developer 在 Internet 上托管网站

c# - 如何按类型访问集合中的控件?

c# - 如何确定组合框是否包含值成员?

c# - 在 HttpClient C# 中支持 TLS 1.2