要根据下拉列表选择项目显示控件?

时间:2012-10-11 18:53:48

标签: c# asp.net

对于Dropdownlist项目,我编写了以下代码:

 protected void dropdowndatasrc()
{
    con = new OleDbConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
    string command = "select eventname from Events";
    cmd = new OleDbCommand(command);
    dataadapter = new OleDbDataAdapter(command, con);
    DataSet dataset = new DataSet();
    con.Open();
    DataTable dt = new DataTable("PayEvent");
    dataadapter.Fill(dt);
    DropDownList4.DataSource = dt;
    DropDownList4.DataTextField = dt.Columns[0].ToString();
  //  DropDownList4.DataValueField = dt.Columns[0].ToString();
   DropDownList4.DataBind();
}

我必须根据下拉列表的值显示控件(在访问数据库中有一个名为payevent的列,如果数据库中的选定项具有'yes',则它具有Yes / No数据类型,如果不是则表示显示控件不得显示任何控件)

我试过这段代码但没有用过

protected void DropDownList4_SelectedIndexChanged(object sender, EventArgs e)
{

    TextBox11.Text = DropDownList4.SelectedItem.Text;
    con = new OleDbConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
    string query = "select payevent from Events where  eventname=@dropdownlist";
    con.Open();
    //string query="select payevent from Events";
    cmd = new OleDbCommand(query, con);
    cmd.Parameters.Add("@dropdownlist", selectedtext);
    OleDbDataReader reader = cmd.ExecuteReader();

    while (reader.Read())
    {
        string value = reader.GetValue((0)).ToString();
        TextBox11.Text = value.ToString();
        if (value == "True" || value == "true")
        { pnl.Visible = false; }
        else if (value == "False" || value == "false")
        { pnl.Visible = true; }



    }
}

表结构如下:

**eventname             payevent**
Work Shop       Yes
emsisoft workshop   Yes
ECE             No
CSE             No

需要帮助!!!

1 个答案:

答案 0 :(得分:0)

通常的问题是,在回发后再次在下拉列表中创建数据绑定并且值丢失。因此,在页面加载时,请不要将帖子上的dropdowndatasrc称为:

if(!IsPostBack)
  dropdowndatasrc();

同样的问题:Dropdown list not selecting value asp.net