不明白!
在工程的App_Code下面有一个DataCon.cs公共类文件,
其中有函数:
public SqlConnection getcon()
{
string strCon = "Data Source=(local);DataBase=db_Examination;User ID=sa;PWD= ";
SqlConnection sqlCon = new SqlConnection(strCon);
return sqlCon;
}
public bool ecDropDownList(DropDownList DDL,string sqlstr3,string DTF,string DVF)
{
SqlConnection con = this.getcon();
con.Open();
SqlDataAdapter mydataadapter = new SqlDataAdapter(sqlstr3, con);
DataSet mydataset = new DataSet();
mydataadapter.Fill(mydataset);
DDL.DataSource = mydataset;
DDL.DataTextField = DTF;
DDL.DataValueField = DVF;
try
{
DDL.DataBind();
return true;
}
catch
{
return false;
}
finally
{
con.Close();
}
}
我在页面上放一个DropDownList和一个Button控件,后台代码为:
Datacon dataconn = new Datacon();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
dataconn.ecDropDownList(DropDownList1, "select * from tb_Profession ", "Name ", "ID ");
}
protected void Button1_Click(object sender, EventArgs e)
{