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();//网页内容进行绑定,每个控件的绑定就可以省略
}
}