asp.net数据绑定

简单绑定:

前台:<%#    %>  中间写字符串的名字 例如:<%# color%> 

后台: protected string   color="blue";

<%# %>可以写在前台任何地方;

this.DataBind();

复杂绑定:

例如:对DropDownList1和RadioButtonList的绑定

string str = "server=.;database=students;integrated security=sspi";
        DataSet ds = new DataSet();
        using (SqlConnection sqlCnn = new SqlConnection())
        {
            sqlCnn.ConnectionString = str;
            SqlCommand sqlcmm = sqlCnn.CreateCommand();
            sqlcmm.CommandText = "select id,name from province";
            SqlDataAdapter da = new SqlDataAdapter(sqlcmm);
            da.Fill(ds);
        }
        this.DropDownList1.DataSource = ds.Tables[0];
        this.DropDownList1.DataTextField = "name";
        this.DropDownList1.DataValueField = "id";
        //this.DropDownList1.DataBind();
        this.RadioButtonList1.DataSource = ds.Tables[0];
        this.RadioButtonList1.DataTextField = "name";
        this.RadioButtonList1.DataValueField = "id";
        //this.RadioButtonList1.DataBind();

        this.GridView1.DataSource = ds.Tables[0];

        this.DataBind();

绑定对类的使用:

后台代码:

 List<DataHelper.Province> list = DataHelper.Class1.GetProvince();

        this.DropDownList1.DataSource = list;
        this.DropDownList1.DataTextField = "provincename";
        this.DropDownList1.DataValueField = "provinceid";

        this.GridView1.DataSource = list;
        this.DataBind();

类的代码:

 public class Class1
    {
        public static List<Province> GetProvince()
        {
            string str = "server=.;database=students;integrated security=sspi";
            DataSet ds = new DataSet();
            using (SqlConnection sqlCnn = new SqlConnection())
            {
                sqlCnn.ConnectionString = str;
                SqlCommand sqlcmm = sqlCnn.CreateCommand();
                sqlcmm.CommandText = "select id,name from province";
                SqlDataAdapter da = new SqlDataAdapter(sqlcmm);
                da.Fill(ds);
            }
            List<Province> list = new List<Province>();
            Province p;
            foreach (DataRow item in ds.Tables[0].Rows)
            {
                p = new Province();
                p.ProvinceName = item["name"].ToString();
                p.ProvinceID = item["id"].ToString();
                list.Add(p);
            }
            return list;
        }
    }
    public class Province
    {
        public string ProvinceName{get;set;}
        public string ProvinceID{get;set;}
    }

 

绑定数据实现两级联动:

public partial class Default2 : System.Web.UI.Page
{
    string str = ConfigurationManager.ConnectionStrings["sqlstring"].ConnectionString;
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            using (SqlConnection cnn = new SqlConnection(str))
            {
                using (SqlCommand cmm = cnn.CreateCommand())
                {
                    cmm.CommandText = "select AreaCode,AreaName from TB_Area where ParentAreaCode='0001'";
                    DataSet ds = new DataSet();
                    SqlDataAdapter da = new SqlDataAdapter(cmm);
                    da.Fill(ds);
                    DropDownList1.DataSource = ds.Tables[0];
                    DropDownList1.DataTextField = "AreaName";
                    DropDownList1.DataValueField = "AreaCode";
                }
            }
            this.DataBind();
        }


    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {

        using (SqlConnection cnn = new SqlConnection(str))
        {
            using (SqlCommand cmm = cnn.CreateCommand())
            {
                cmm.CommandText = "select AreaCode,AreaName from TB_Area  where AreaCode like '" + DropDownList1.SelectedValue + "__' ";
                DataSet ds = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(cmm);
                da.Fill(ds);
                DropDownList2.DataSource = ds.Tables[0];
                DropDownList2.DataTextField = "AreaName";
                DropDownList2.DataValueField = "AreaCode";
            }
        }
        this.DataBind();
    }
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值