2

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

    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>
4

3 回答 3

3
DropDownList1.SelectedValue.ToString();

或者

DropDownList1.Text;

或者

DropDownList1.SelectedValue.ToString();
于 2013-03-13T06:43:23.130 回答
2

您可以使用SelectedValue的属性DropDownList。要查看如何在 where 子句中向 select 查询添加参数,请参阅这篇文章

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();
        }
    //}
}
于 2013-03-13T06:41:49.277 回答
0

你应该使用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();
于 2013-03-13T06:42:35.587 回答