ASP.NET高级开发之数据绑定

1.页面中处理单一数据常用哪些控件
TextBox、Lable、span、div
如何放数据
TextBox1.Text="";
Lable1.Text="";
span1.Value="";
div1.InnerText="";
如何显示数据
this.TextBox.Text="hello word";(赋值)
string sr=this.TextBox.Text;取值
2.页面中处理复杂数据常用哪些控件
DropDownList 、ListBox、Table等等
如何显示数据
this.DropDownList1.Items.Add("河北");
this.DropDownList2.Items.Add("河南");

this.DropDownList1.Items.Add(new ListItem("河北","1"));
this.DropDownList1.Items.Add(new ListItem("河南", "2"));
什么是数据绑定
数据绑定是在应用程序UI与业务逻辑之间建立连接的过程
一.简单绑定(单值绑定)
绑定到属性(字段)知识
1.可以指定对象的某个属性或字段作为页面某元素的绑定值。

绑定语法:<%#字段名或属性名%>
如:

前台页面绑定:
<body style=‘<%#bodystyle%>’>…</body>
后台代码:(先定义一个字段,然后给这个字段赋值)
protected string bodystyle;

protected void Page_Load(object sender, EventArgs e)
{
   
bodystyle = "background-color:#ff0000";
this.DataBind();

}

2.要绑定的内容可以是复杂的表达式

绑定语法:<%# C#表达式%>
如:

页面:
<body style='<%# pagecolor+ ";font-size:40px" %>'>

代码:
protected string pagecolor;
 
protected void Page_Load(object sender, EventArgs e)
{
          
 pagecolor = "background-color:#ff0000";

 }
3.要绑定的内容还可以是方法
页面:
<body style='<%# GetStyleofBody()%>'>

代码:
protected string GetStyleofBody();
 
{
          
 string style="background-color:"+this.DropDownList1.SelectedValue+";font-size""+size;
return style;

 }
二.复杂绑定(多值绑定)


集合绑定知识点
要绑定的内容是复杂的集合对象

常用控件:DropDownList/ListBox/…

使用DataSource属性指定集合数据源,使用DataBind方法进行绑定。
注:常用的集合数据源有DataSet、 DataTable、 List、 ArryList 、DataReader、DataView
前台页面:
<body>
    <form id="form1" runat="server">
    <div>
   
        <asp:DropDownList ID="DropDownList1" runat="server">
        </asp:DropDownList>
        <br />
        <br />
        <asp:RadioButtonList ID="RadioButtonList1" runat="server">
        </asp:RadioButtonList>
        <br />
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
   
    </div>
    </form>
</body>
后台代码
 protected void Page_Load(object sender, EventArgs e)
        {
            //1获取一个数据源对象,使用sqlHelper中的ExcuteDataTable()方法就能获取一个dataTable数据源。也可以在
            //这里连接数据库得到dataSet或dataTable数据源。
            string sql="select * from T_Province";
           DataTable dt=SqlHealper.ExecuteDataTable(sql);
           this.DropDownList1.DataSource = dt;
           this.DropDownList1.DataTextField = "Pname";
           this.DropDownList1.DataValueField = "Id";
           
           //this.DropDownList1.DataBind();

           this.GridView1.DataSource = dt;
         //  this.GridView1.DataBind();
         //使用复杂绑定完成RadioButtonList控件的绑定
           this.RadioButtonList1.DataSource = dt;
           this.RadioButtonList1.DataTextField = "Pname";
           this.RadioButtonList1.DataValueField = "Id";
           //this.RadioButtonList1.DataBind();
           this.DataBind();//网页内容进行绑定,每个控件的绑定就可以省略
        }
    }

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值