62,047
社区成员
发帖
与我相关
我的任务
分享
protected void Page_Load(object sender, EventArgs e)
{
SelectBind("ABC", "A");
SelectBind("ABD", "B");
}
public void SelectBind(string FieldsName,string TableName)
{
SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
con.Open();
string StrSql = "select "+FieldsName+" from "+TableName+"";
SqlCommand cmd = new SqlCommand(StrSql, con);
SqlDataReader sdr = cmd.ExecuteReader();
this.FindControl(FieldsName as DropDownList).DataSource = sdr;
this.FindControl(FieldsName as DropDownList).DataTextField = FieldsName;
this.FindControl(FieldsName as DropDownList).DataValueField = FieldsName;
this.FindControl(FieldsName as DropDownList).DataBind();
sdr.Dispose();
}
DropDownList DropDwonControl = this.FindControl(FieldsName) as DropDownList;
DropDwonControl.DataSource = sdr;