asp.net 数据绑定的实例代码
程序员文章站
2024-02-28 21:44:16
复制代码 代码如下:public partial class _default : system.web.ui.page{ protec...
复制代码 代码如下:
public partial class _default : system.web.ui.page
{
protected string title="大家好"; //前台代码<title><%#title %></title>
protected void page_load(object sender, eventargs e)
{
dataset ds = new dataset();
string sql = configurationmanager.connectionstrings["strsql"].connectionstring;
using (sqlconnection sqlcnn=new sqlconnection(sql))
{
using (sqlcommand sqlcmm=sqlcnn.createcommand())
{
sqlcmm.commandtext = "select * from list";
sqldataadapter adapter = new sqldataadapter(sqlcmm);
adapter.fill(ds);
}
this.radiobuttonlist1.datasource = ds.tables[0];
this.radiobuttonlist1.datatextfield = "listname";
this.radiobuttonlist1.datavaluefield = "id";
//this.radiobuttonlist1.databind();
this.checkboxlist1.datasource = ds.tables[0];
this.checkboxlist1.datatextfield = "listname";
this.checkboxlist1.datavaluefield = "id";
//this.radiobuttonlist1.databind();
this.databind();
} //数据绑定到radiobuttonlist,checkboxlist
if (!ispostback)
{
dataset ds1 = new dataset();
using (sqlconnection sqlcnn1 = new sqlconnection(sql))
{
using (sqlcommand sqlcmm1 = sqlcnn1.createcommand())
{
sqlcmm1.commandtext = "select provinceid,provincename from province";
sqldataadapter adapter = new sqldataadapter(sqlcmm1);
adapter.fill(ds1);
this.dropdownlist1.datasource = ds1.tables[0];
this.dropdownlist1.datatextfield = "provincename";
this.dropdownlist1.datavaluefield = "provinceid";
this.dropdownlist1.databind();
}
}
}
}
protected void dropdownlist1_selectedindexchanged(object sender, eventargs e)
{
dataset ds = new dataset();
string str = configurationmanager.connectionstrings["strsql"].connectionstring;
using (sqlconnection sqlcnn = new sqlconnection(str))
{
using (sqlcommand sqlcmm = sqlcnn.createcommand())
{
sqlcmm.commandtext = "select cityid,cityname from city where provinceid='" + this.dropdownlist1.selectedvalue + "'";
sqldataadapter adapter = new sqldataadapter(sqlcmm);
adapter.fill(ds);
this.dropdownlist2.datasource = ds.tables[0];
this.dropdownlist2.datatextfield = "cityname";
this.dropdownlist2.datavaluefield = "cityid";
this.dropdownlist2.databind();
}
}
}//实现省市二级联动
}