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

来源:www.13sc.com | 游客上传 | 328 次浏览 | 0 条评论

搜索: asp.net 数据 绑定 

这篇文章介绍了asp.net 数据绑定的实例代码,有需要的朋友可以参考一下

复制代码 代码如下:

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 字符串、二进制、编码数组转换函数

ASP.NET读取XML文件4种方法分析

Asp.net 获取指定目录下的后缀名为".doc" 的所有文件名和文件路径

asp.net下常用的加密算法MD5、SHA-1应用代码

Silverlight中动态获取Web Service地址

asp.net 包含文件的方法

asp.net使用母版页中使用ajax脚本取数据

GridView常用操作事件图文介绍

ASP.NET MVC 中实现基于角色的权限控制的处理方法

.NET调用控制台下生成的exe文件,传参及获取返回参数的思路及代码

几种判断asp.net中session过期方法的比较

asp.net动态载入用户控件的方法

Copyright 2009-2013 13sc.com All Rights Reserved.