关于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_dic_disease where code = ' " + code + " ' ";
this.mySqlDataReader = mySqlCommand.ExecuteReader();
this.mySqlDataReader.Read();
string name = mySqlDataReader[ "name "].ToString();
string disease_class = mySqlDataReader[ "class "].ToString();
string icd10 = mySqlDataReader[ "icd10 "].ToString();
string note = mySqlDataReader[ "note "].ToString();
this.mySqlConnection.Close();
this.lblCode.Text = code;
this.txtNameShow.Text = name;
for (int cntDisease_Class = 0; cntDisease_Class < dpdDC.Items.Count; cntDisease_Class++)
{
if (dpdDC.Items[cntDisease_Class].Value.Trim() == disease_class)
{
dpdDC.SelectedIndex = cntDisease_Class;
}
}
for (int cntICD10 = 0; cntICD10 < dpdICD.Items.Count; cntICD10++)
{
if (dpdICD.Items[cntICD10].Value.Trim() == icd10)
{
dpdICD.SelectedIndex = cntICD10;
}
}
this.txtNote.Text = note;
btnEdit.Enabled = true;
btnDel.Enabled = true;
btnAdd.Enabled = false;
}
p.s. dpdDC就是dropdowslist,遍历dropdownlist里面所有元素,之后显示编辑修改。
[解决办法]
搞错了
banji.Items.FindByText(dr.Item( "classname ").ToString).Selected = True