【问题标题】:How to get value from drop down list box如何从下拉列表框中获取值
【发布时间】:2013-02-28 23:47:58
【问题描述】:

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

    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>

【问题讨论】:

    标签: c# asp.net winforms


    【解决方案1】:

    您可以使用DropDownListSelectedValue 属性。要查看如何在 where 子句中向选择查询添加参数,请参阅此 post

    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();
            }
        //}
    }
    

    【讨论】:

    • 我可以把那条线放在哪里?
    • 同时删除 if(SelectedIndex == 1) 条件。如果您想在 SQL 查询中使用 SelectedValue,那么该条件没有任何意义。
    【解决方案2】:

    您应该使用SelectedValue 属性。

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

    DropDownList1.SelectedValue.ToString();
    

    喜欢;

    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();
    }
    

    如果你想在你的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();
    

    【讨论】:

      【解决方案3】:
      DropDownList1.SelectedValue.ToString();
      

      或者

      DropDownList1.Text;
      

      或者

      DropDownList1.SelectedValue.ToString();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-08-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多