asp.net 数据绑定

来源:未知 责任编辑:智问网络 发表时间:2013-08-28 11:55 点击:

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

            { www.2cto.com

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

            }

        }

    }//实现省市二级联动

}

 

摘自haitaoDoit

    发表评论
    请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
    用户名: 验证码:点击我更换图片
    最新评论 更多>>

    推荐热点

    • 浅析.NET下XML数据访问新机制
    • asp.net 面试+笔试题目第1/2页
    • C# 邮件地址是否合法的验证
    • asp.net 设置GridView的选中行的实现代码
    • C#高级编程:数据库连接[1]
    • 经典C++程序1
    • IIS 自动回收导致后台定时器失效的问题解决
    • ASP.NET&#160;GridView列表代码示例
    • Asp.net MVC源码分析--Action Filter的链式调用
    网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
    Copyright © 2008-2015 计算机技术学习交流网. 版权所有

    豫ICP备11007008号-1