asp.net 数据绑定的实例代码

所属分类: 网络编程 / ASP.NET 阅读数: 791
收藏 0 赞 0 分享
复制代码 代码如下:

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();
            }
        }
    }//实现省市二级联动
}
更多精彩内容其他人还在看

使用UserControl做网站导航条的思路 分析

使用UserControl做网站导航条的思路 分析
收藏 0 赞 0 分享

ASP.NET中使用AspnetAccessProvider

ASP.NET中使用AspnetAccessProvider
收藏 0 赞 0 分享

asp.net下实现URL重写技术的代码

asp.net下实现URL重写技术的代码
收藏 0 赞 0 分享

为大家经常为md5加密过的常用admin,admin888,0000密码

为大家经常为md5加密过的常用admin,admin888,0000密码
收藏 0 赞 0 分享

利用MS AJAX注册Javascript命名空间并创建类

利用MS AJAX注册Javascript命名空间并创建类
收藏 0 赞 0 分享

asp.net下gridview 批量删除的实现方法第1/3页

asp.net下gridview 批量删除的实现方法
收藏 0 赞 0 分享

用CSS实现图片倾斜 只支持IE

用CSS实现图片倾斜 只支持IE
收藏 0 赞 0 分享

asp.net(c#)中取得文件物理路径

asp.net(c#)中取得文件物理路径
收藏 0 赞 0 分享

垃圾代码二三行 ASPX小马

垃圾代码二三行 ASPX小马
收藏 0 赞 0 分享

.NET 2.0获取配置文件AppSettings和ConnectionStrings节数据的方法

.NET 2.0获取配置文件AppSettings和ConnectionStrings节数据的方法
收藏 0 赞 0 分享
查看更多