ASP.NET 引入了新的声明性数据绑定语法。这种非常灵活的语法允许开发人员不仅可以绑定到数据源,而且可以绑定到简单属性、集合、表达式甚至是从方法调用返回的结果。下表显示了新语法的一些示例
ASP.NET 引入了新的声明性数据绑定语法。这种非常灵活的语法允许开发人员不仅可以绑定到数据源,而且可以绑定到简单属性、集合、表达式甚至是从方法调用返回的结果。下表显示了新语法的一些示例。
简单属性 |
Customer: <%# custID %> |
集合 |
Orders: <asp:ListBox id="List1" datasource='<%# myArray %>' runat="server"> |
表达式 |
Contact: <%# ( customer.First Name + " " + customer.LastName ) %> |
方法结果 |
Outstanding Balance: <%# GetBalance(custID) %> |
1. 绑定到属性
C#:
void Page_Load(Object sender, EventArgs e)


{
Page.DataBind();
}
string custID


{

get
{
return "ALFKI";
}
}
int orderCount


{

get
{
return 11;
}
}
ASP:
<body>

<h3><font face="宋体">到页属性的数据绑定</font></h3>

<form runat=server>

客户:<b><%
# custID %></b><br>

未结的订单:<b><%
# orderCount %></b>

</form>

</body>

2. 绑定到集合和列表
绑定到Array List
C# :
void Page_Load(Object Sender, EventArgs E)


{
if (!Page.IsPostBack)

{
ArrayList values = new ArrayList();
values.Add ("IN");
values.Add ("KS");
values.Add ("MD");
values.Add ("MI");
values.Add ("OR");
values.Add ("TN");

DropDown1.DataSource = values;
DropDown1.DataBind();
}
}

void SubmitBtn_Click(Object sender, EventArgs e)


{
Label1.Text = "您选择了:" + DropDown1.SelectedItem.Text;
}
ASP:
<body>
<h3><font face="宋体">数据绑定 DropDownList</font></h3>

<form runat=server>
<asp:DropDownList id="DropDown1" runat="server" />

<asp:button Text="提交" OnClick="SubmitBtn_Click" runat=server/>

<p>
<asp:Label id=Label1 font-name="宋体" font-size="10.5pt" runat="server" />

</form>

</body>

绑定到Hash Table
C#:
void Page_Load(Object sender, EventArgs e)


{
if (!Page.IsPostBack)

{
Hashtable h = new Hashtable();
h.Add ("键 1", "值 1");
h.Add ("键 2", "值 2");
h.Add ("键 3", "值 3");

MyDataList.DataSource = h;
MyDataList.DataBind();
}
}
ASP:
3. 绑定到方法或表达式
<body>

<h3><font face="宋体">到哈希表的数据绑定</font></h3>

<form runat=server>

<asp:DataList id="MyDataList" runat="server"
BorderColor="black"
BorderWidth="1"
GridLines="Both"
CellPadding="4"
CellSpacing="0"
>

<ItemTemplate>

<%
# ((DictionaryEntry)Container.DataItem).Key %> :

<%
# ((DictionaryEntry)Container.DataItem).Value %>
</ItemTemplate>

</asp:DataList>

</form>

</body>


绑定时的表达式:<%# CType(DataBinder.Eval(Container.DataItem, "BoolValue"), Boolean) %>
C#:
void Page_Load(Object Src, EventArgs E)


{
if (!Page.IsPostBack)

{
ArrayList values = new ArrayList();

values.Add (0);
values.Add (1);
values.Add (2);
values.Add (3);
values.Add (4);
values.Add (5);
values.Add (6);

DataList1.DataSource = values;
DataList1.DataBind();
}
}

String EvenOrOdd(int number)


{
if ((number % 2) == 0)
return "偶数";
else
return "奇数";
}
ASP:
<body>

<h3><font face="宋体">到方法和表达式的数据绑定</font></h3>

<form runat=server>

<asp:DataList id="DataList1" runat="server"
BorderColor="black"
BorderWidth="1"
GridLines="Both"
CellPadding="3"
CellSpacing="0"
>

<ItemTemplate>

数字值:<%
# Container.DataItem %>

偶/奇:<%
# EvenOrOdd((int) Container.DataItem) %>
</ItemTemplate>

</asp:datalist>

</form>

</body>

Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1716585
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步