我这是个文章编辑初始化的时候的程序,两个DropDownList表示文章的分类,,但在这DropDownList的值跟我要编辑文章的值不对应,应该怎么做呢,什么SelectedValue,FindValue的方法都用了怎么都不行,有做过的给详细指点一下 private void databind()
{
string sql;
DataSet ds=new DataSet();
SqlConnection conn2=new SqlConnection();
string strConn=System.Configuration.ConfigurationSettings.AppSettings["connstr"];
conn2.ConnectionString=strConn;
conn2.Open();
sql="select * from info_type where sitename='sdcio' order by num";
ds=conn.GetDs(sql);
DropDownList3.DataSource=ds;
DropDownList3.DataBind(); sql="select * from info_dict where sitename='sdcio' and tab_type="+DropDownList3.SelectedValue+" order by num";
ds=conn.GetDs(sql);
DropDownList1.DataSource=ds;
DropDownList1.DataBind();

arcid=int.Parse(Request["aid"].ToString());
sql="select * from cio_arc where id="+Request["aid"];
SqlCommand cmd=new SqlCommand(sql,conn2);
SqlDataReader re=cmd.ExecuteReader();
re.Read();
TextBox1.Text=re["topic"].ToString();
TextBox3.Text=re["author"].ToString();
TextBox4.Text=re["source"].ToString();
FreeTextBox2.Text=re["content"].ToString();
re.Close();
conn2.Close();
}

解决方案 »

  1.   

    设置DropDownList的DataTextField和DataValueField属性分别为你所要的列的名称。一般来说,ValueField都是设ID,方便使用SelectedValue来读取其ID。
      

  2.   

    楼主你试一试把 
                   DropDownList3.SelectedValue 改为
    DropDownList3.SelectedItem.Text 看看!
      

  3.   

    DropDownList1.DataSource = ds.Tables[0].DefaultView;
    DropDownList1.DataTextField = ds.Tables[0].Columns["NAME"].ToString(); //绑定DropDownList1中每项的Text
    DropDownList1.DataValueField = ds.Tables[0].Columns["NAME"].ToString();//绑定DropDownList1中每项的Value
    DropDownList1.DataBind();DLqs.Items.Insert(0,"--请选择--");
    DLqs.Items[0].Selected=true;
    //绑定后再在DropDownList1的0索引处插入"--请选择--",注意顺序
      

  4.   

    还是不行啊,在DropDownList3 和DropDownList1 都绑定之后,怎么自己指定他们的值,我用
    DropDownList3.Items.FindByValue("27").Selected=true;,老是说:未将对象引用设置到对象的实例,郁闷啊
      

  5.   

    首先要确定要选中的条目(通常是条目ID),然后利用以下代码可以实现:
    DropDownList1.SelectedIndex=DropDownList1.Items.IndexOf(DropDownList1.Items.FindByValue(_table.Rows[0]["CatalogID"].ToString()));
      

  6.   

    你的DropDownList3绑定之后始终在第一项上,DropDownList1也就是绑定和DropDownList3相对应的第一项上。建议在DropDownList3的SelectedIndexChanged事件中绑定DropDownList1的值。但是页面可能刷新一下。
      

  7.   

    DropDownList3.SelectedValue----------->DropDownList3.SelectedItem.Value
      

  8.   

    changkongyimen的方法可能行,但是DropDownList1.Items.FindByValue的返回怎么显示未定义的值啊,没起什么作用啊
      

  9.   

    DropDownList1.Items.FindByValue能返回值,但3就不行了,不知为什么
      

  10.   

    首先要确定要选中的条目(通常是条目ID),然后利用以下代码可以实现:
    DropDownList1.SelectedIndex=DropDownList1.Items.IndexOf(DropDownList1.Items.FindByValue(_table.Rows[0]["CatalogID"].ToString()));以上语是根据条目的value来选中某一项的.如果这个value在DropDownList不存在,就会产生你说的那种情况,即返回未定义的值.