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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15378602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 16:33:53  来源:igfitidea点击:

How to get value from drop down list box

c#asp.netwinforms

提问by user1954418

What I want is to get the value from the selected item in the drop down list, and use it to query (select table) in a button click event.

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

    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>

回答by Adil

You can use SelectedValueproperty of DropDownList. To see how to add parameter to the select query in where clause see this post.

您可以使用SelectedValue属性DropDownList。要了解如何向 where 子句中的选择查询添加参数,请参阅此帖子

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    string sel = DropDownList1.SelectedValue.ToString();
  //  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();
        }
    //}
}

回答by Soner G?nül

You should use SelectedValueproperty.

你应该使用SelectedValue财产。

Gets the value of the selected item in the list control, or selects the item in the list control that contains the specified value.

获取列表控件中选定项的值,或选择列表控件中包含指定值的项。

DropDownList1.SelectedValue.ToString();

Like;

喜欢;

string val = DropDownList1.SelectedValue.ToString();
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();
}

If you want to use it as a parameter in your SqlCommand, you can use it like;

如果你想在你的 中使用它作为参数SqlCommand,你可以像这样使用它;

SqlCommand sqlCommand = new SqlCommand("Select * from tbl_WinApps_FileHeader WHERE val = @val");
sqlCommand.Parameters.AddWithVAlue("@val", val);
sqlCommand.Connection = con;
SqlDataReader read = sqlCommand.ExecuteReader();

回答by Freelancer

DropDownList1.SelectedValue.ToString();

Or

或者

DropDownList1.Text;

Or

或者

DropDownList1.SelectedValue.ToString();