先描述我要的效果:可以通过dropdownlist的属性指定需要绑定的表和字段,自动绑定需要的值
应该是要重写dropdownlist,怎么重写啊,谢谢!
举个例子:
比如有两张表A,B
A表中有a,b两个字段
B表中有c,d两个字段
我只需要指定dropdownlist的属性就可以绑定A表的a字段活B表的b字段。
不知道描述的是否清楚?
应该是要重写dropdownlist,怎么重写啊,谢谢!
举个例子:
比如有两张表A,B
A表中有a,b两个字段
B表中有c,d两个字段
我只需要指定dropdownlist的属性就可以绑定A表的a字段活B表的b字段。
不知道描述的是否清楚?
DropDownList1.DataSource = SqlDataSource1;
DropDownList1.DataSource = SqlDataSource2;
直接封装一下,方法罢了
DropDownList1.DataSource = ds;
DropDownList1.DataBind();
DropDownList1.DataValueField = "a";
DropDownList1.DataTextField = "b";
string strSQL ="select * from A join B on XXXX";
DataSet ds = XXXX;
DropDownList1.DataTextField = "表A中的字段";
DropDownList1.DataValueField = "表B中的字段";
//返回数据源dt(DataTable)
DropDownList1.DataSource = dt;
DropDownList1.DataBind();
DropDownList1.DataValueField = "a";
DropDownList1.DataTextField = "b";
写个方法传入4个参数就行了
依次是
public void ddlbind(dropdownlist ddl,DataTable dt,DataValueField value,DataTextField text)
{
..
};//调用2次就行了
public static class DropDownListExtend
{
public static DropDownList BindDropDownListData(this DropDownList ddl, DataSet ds, string text, string value)
{
ddl.DataSource = ds;
ddl.DataTextField = text;
ddl.DataValueField = value;
ddl.DataBind();
return ddl;
}
}在页面的Page_Load事件中这样
DM dm = new DM();
string strSQL = "select * from 表名";
DataSet ds = dm.getsql(strSQL);
this.DropDownList1.BindDropDownListData(ds, "字段一", "字段二");
这里尽是做一个简单举例,你可以根据需要对扩展方法进行参数重新设置,你可以加上其他参数来满足你的要求。