日期:2014-05-18  浏览次数:20467 次

关于dropdownist的问题
在gridview里显示学生信息,在gridview里添加了一个超链接按钮 "修改 ",点击修改就会跳转到修改页面进行修改,在修改页面有dropdownlist控件,已经在修改页面的page_load事件里连接上数据库里面的班级表了,当在gridview点击修改链接时就会在dropdownlist控里面显示相应的班级,其他项会在文本框控件中显示,请教如何在dropdownlist控件里面显示相应班级;

这是在修改页面连接班级的代码  
  Dim   sql   As   String
                Dim   conn   As   New   SqlConnection
                conn.ConnectionString   =   "data   source=192.168.31.199;initial   catalog=kaoqin   manager;user   id=james5;password=james5; "
                sql   =   "select   classno,classname   from   class "
                Dim   cmd   As   New   SqlCommand(sql,   conn)
                conn.Open()
                Dim   dr   As   SqlDataReader
                dr   =   cmd.ExecuteReader()
                banji.AppendDataBoundItems   =   True
                banji.Items.Add(New   ListItem( "--请选择-- ",   "-1 "))
                banji.DataSource   =   dr
                banji.DataValueField   =   "classno "
                banji.DataTextField   =   "classname "
                banji.DataBind()

------解决方案--------------------
banji.Items.FindByText( "你的文本 ").Selected = True
------解决方案--------------------
楼主是vb.net,偶给你一个c#版本的例子,你参考一下:

protected System.Data.SqlClient.SqlConnection mySqlConnection;
protected System.Data.SqlClient.SqlCommand mySqlCommand;
protected System.Data.SqlClient.SqlDataAdapter mySqlDataAdapter;
protected System.Data.SqlClient.SqlDataReader mySqlDataReader;
protected System.Data.DataSet myDataSet;
protected System.Data.DataView myDataView;

//数据绑定
protected void BindData()
{
this.mySqlConnection = URCode.GetConn();
this.mySqlDataAdapter = new SqlDataAdapter();
string selectSqlStr = "select * from pub_dic_disease ";
this.mySqlDataAdapter.SelectCommand = new SqlCommand(selectSqlStr, mySqlConnection);
this.myDataSet = new DataSet();
this.mySqlDataAdapter.Fill(myDataSet);
this.GridView1.DataSource = myDataSet;
this.GridView1.DataBind();
this.mySqlConnection.Close();
}

//选择记录
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
this.btnMessage.Visible = false;
string code = " ";
this.lblCode.Text = GridView1.SelectedValue.ToString();
code = lblCode.Text.ToString();
this.mySqlConnection = URCode.GetConn();
this.mySqlCommand = mySqlConnection.CreateCommand();
this.mySqlConnection.Open();
this.mySqlCommand.CommandText = "select * from pub