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

发布时间:2022-04-16 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了asp.net 数据绑定的实例代码脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
复制代码 代码如下:

public partial class _Default : System.Web.UI.Page
{
&nbsp;   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 数据绑定的实例代码所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。