手机
当前位置:查字典教程网 >编程开发 >asp.net教程 >asp.net 数据绑定的实例代码
asp.net 数据绑定的实例代码
摘要:复制代码代码如下:publicpartialclass_Default:System.Web.UI.Page{protectedstring...

复制代码 代码如下:

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();

}

}

}//实现省市二级联动

}

【asp.net 数据绑定的实例代码】相关文章:

asp.net Linq把数据导出到Excel的代码

asp.net中调用winrar实现压缩解压缩的代码

Asp.net 无限级分类实例代码

asp.net求3位不同数字的组合数

asp.net读取excel中的数据并绑定在gridview

asp用户登录模块实例代码

c# .net 生成图片验证码的代码

使用ajax局部刷新gridview进行数据绑定示例

litjson读取数据示例

asp.net刷新本页面的六种方法

精品推荐
分类导航