asp.net下载列表在运行时进行数据绑定

时间:2016-09-13 06:48:14

标签: asp.net data-binding

 con = new SqlConnection(s);
        con.Open();
        if (RadioButtonList1.SelectedIndex == 0)
        {
            cmd = new SqlCommand("select [Item] from Veg_Items", con);
            da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds, "[Item]");
            DropDownList1.DataSource = ds.Tables[0];
            DropDownList1.DataBind();
        }
        else if (RadioButtonList1.SelectedIndex == 1)
        {
            cmd = new SqlCommand("select [Item] from NonVeg_Items", con);
            da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds, "[Item]");
            DropDownList1.DataSource = ds.Tables[0];
            DropDownList1.DataBind();

        }
            con.Close();
    }

我的表中有项目,一旦我在Dropdownlist中选择任何值,我需要在RadioButtonList中显示我的项目。 我还可以看到ds.Tables[0]行中的项目,但我无法将它们绑定到Dropdownlist

1 个答案:

答案 0 :(得分:0)

  con.Open();
        if (RadioButtonList1.SelectedIndex == 0)
        {
            cmd = new SqlCommand("select [Item] from [Veg_Items]", con);
            SqlDataReader dr = cmd.ExecuteReader();
            List<Object> lt = new List<Object>();
            if (dr.HasRows)
            {
                while (dr.Read())
                {

                    lt.Add(dr["Item"].ToString());
                }
            }
            DropDownList1.DataSource = lt;
            DropDownList1.DataBind();
        }
        else if (RadioButtonList1.SelectedIndex == 1)
        {
            cmd = new SqlCommand("select [Item] from [NonVeg_Items]", con);
            SqlDataReader dr = cmd.ExecuteReader();
            List<Object> lt = new List<Object>();
            if (dr.HasRows)
            {
                while (dr.Read())
                {

                    lt.Add(dr["Item"].ToString());
                }
            }
            DropDownList1.DataSource = lt;
            DropDownList1.DataBind();
  }
  con.Close();