华域联盟 .Net 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 Repeater 数据绑定的具体实现(图文详解)
  • ASP.NET Eval进行数据绑定的方法
  • asp.net的cms 绑定数据篇
  • asp.net Repeater 数据绑定代码
  • asp.net ListView 数据绑定

本文由 华域联盟 原创撰写:华域联盟 » asp.net 数据绑定的实例代码

转载请保留出处和原文链接:https://www.cnhackhy.com/51540.htm

本文来自网络,不代表华域联盟立场,转载请注明出处。

作者: sterben

发表回复

联系我们

联系我们

2551209778

在线咨询: QQ交谈

邮箱: [email protected]

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

关注微博
返回顶部