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プロパティを使用できますDropDownListwhere 句の 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 に答える