ASP.NET数据绑定的记忆碎片实现代码
程序员文章站
2024-03-05 14:22:15
asp.net数据绑定的一般情况1、<%= c#代码 %> //调用代码隐藏页面的方法、属性、或者字段 这里一般是调用属性和方法比较多,要注意调用的属性、方法或...
asp.net数据绑定的一般情况
1、<%= c#代码 %> //调用代码隐藏页面的方法、属性、或者字段
这里一般是调用属性和方法比较多,要注意调用的属性、方法或者字段的作用域,必须是可以在aspx页面可以访问到的。
代码示例(aspx):<%=property%>
在(cs)是: public string property{ get { return "this is a property";} }
属性是这样使用的,方法和字段的使用类似,也是这样实现的。
2、<%#数据绑定表达式%>//是在列表控件里面使用的
使用方式一:<%# eval("firstname")%>
使用方式二:<%# databinder.eval(container.dataitem, "secondname")%>
下面附上我调试的源码,可以复制过去看看
在aspx页面:
<%@ page language="c#" autoeventwireup="true" codebehind="default.aspx.cs" inherits="databindex._default" %>
<%@ import namespace="system.data" %>
<%@ import namespace="system.collections.generic" %>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form runat="server">
<div>
<%=property%>
<br />
<asp:textbox id="textbox1" text="this is textbox of serverclient " runat="server"></asp:textbox>
<br />
<%=method()%>
<br />
<br />
<asp:label id="label1" runat="server"><%=textbox1.text %></asp:label>
<br />
<%=(property + " " + method())%>
</div>
<div>
<asp:repeater id="repeater1" runat="server" onitemdatabound="rptallonitemdatabound">
<headertemplate>
this is header<br />
</headertemplate>
<itemtemplate>
firstname:<%# eval("firstname")%>
secondname:<%# databinder.eval(container.dataitem, "secondname")%>
fullname:<%# (container.dataitem as databindex.person).fullname%>
<asp:literal id="others" runat="server"></asp:literal>
<br />
</itemtemplate>
<footertemplate>
this is footer<br />
</footertemplate>
</asp:repeater>
</div>
</form>
</body>
</html>
在cs页面:
using system;
using system.collections.generic;
using system.linq;
using system.web;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.mobilecontrols;
namespace databindex
{
public class person
{
public string firstname
{
get;
set;
}
public string secondname
{
get;
set;
}
public string fullname
{
get
{
return firstname + secondname;
}
}
}
public partial class _default : system.web.ui.page
{
public string property
{
get
{
return "this is a property";
}
}
protected void page_load(object sender, eventargs e)
{
string str = textbox1.text;
person per = new person();
per.firstname= "刘";
per.secondname= "明丰";
person per1 = new person();
per1.firstname = "林";
per1.secondname = "旺";
person per2 = new person();
per2.firstname = "陈";
per2.secondname = "仁峰";
list<person> list = new list<person>();
list.add(per);
list.add(per1);
list.add(per2);
repeater1.datasource = list;
repeater1.databind();
}
protected void rptallonitemdatabound(object sender, repeateritemeventargs e)
{
person pe = (person)e.item.dataitem;
literal lit = e.item.findcontrol("others") as literal;
if (pe !=null)
switch (pe.firstname)
{
case "刘":
lit.text = "刘喜欢打球";
break;
case "林":
lit.text = "林喜欢下棋";
break;
default:
lit.text = "陈喜欢c#";
break;
}
}
protected string method()
{
return "this is a method";
}
}
}
1、<%= c#代码 %> //调用代码隐藏页面的方法、属性、或者字段
这里一般是调用属性和方法比较多,要注意调用的属性、方法或者字段的作用域,必须是可以在aspx页面可以访问到的。
代码示例(aspx):<%=property%>
在(cs)是: public string property{ get { return "this is a property";} }
属性是这样使用的,方法和字段的使用类似,也是这样实现的。
2、<%#数据绑定表达式%>//是在列表控件里面使用的
使用方式一:<%# eval("firstname")%>
使用方式二:<%# databinder.eval(container.dataitem, "secondname")%>
下面附上我调试的源码,可以复制过去看看
在aspx页面:
复制代码 代码如下:
<%@ page language="c#" autoeventwireup="true" codebehind="default.aspx.cs" inherits="databindex._default" %>
<%@ import namespace="system.data" %>
<%@ import namespace="system.collections.generic" %>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form runat="server">
<div>
<%=property%>
<br />
<asp:textbox id="textbox1" text="this is textbox of serverclient " runat="server"></asp:textbox>
<br />
<%=method()%>
<br />
<br />
<asp:label id="label1" runat="server"><%=textbox1.text %></asp:label>
<br />
<%=(property + " " + method())%>
</div>
<div>
<asp:repeater id="repeater1" runat="server" onitemdatabound="rptallonitemdatabound">
<headertemplate>
this is header<br />
</headertemplate>
<itemtemplate>
firstname:<%# eval("firstname")%>
secondname:<%# databinder.eval(container.dataitem, "secondname")%>
fullname:<%# (container.dataitem as databindex.person).fullname%>
<asp:literal id="others" runat="server"></asp:literal>
<br />
</itemtemplate>
<footertemplate>
this is footer<br />
</footertemplate>
</asp:repeater>
</div>
</form>
</body>
</html>
在cs页面:
复制代码 代码如下:
using system;
using system.collections.generic;
using system.linq;
using system.web;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.mobilecontrols;
namespace databindex
{
public class person
{
public string firstname
{
get;
set;
}
public string secondname
{
get;
set;
}
public string fullname
{
get
{
return firstname + secondname;
}
}
}
public partial class _default : system.web.ui.page
{
public string property
{
get
{
return "this is a property";
}
}
protected void page_load(object sender, eventargs e)
{
string str = textbox1.text;
person per = new person();
per.firstname= "刘";
per.secondname= "明丰";
person per1 = new person();
per1.firstname = "林";
per1.secondname = "旺";
person per2 = new person();
per2.firstname = "陈";
per2.secondname = "仁峰";
list<person> list = new list<person>();
list.add(per);
list.add(per1);
list.add(per2);
repeater1.datasource = list;
repeater1.databind();
}
protected void rptallonitemdatabound(object sender, repeateritemeventargs e)
{
person pe = (person)e.item.dataitem;
literal lit = e.item.findcontrol("others") as literal;
if (pe !=null)
switch (pe.firstname)
{
case "刘":
lit.text = "刘喜欢打球";
break;
case "林":
lit.text = "林喜欢下棋";
break;
default:
lit.text = "陈喜欢c#";
break;
}
}
protected string method()
{
return "this is a method";
}
}
}