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)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
Page.DataBind();
}
string custID
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{
return "ALFKI";
}
}
int orderCount
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{
return 11;
}
}
ASP:
<body>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<h3><font face="宋体">到页属性的数据绑定</font></h3>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<form runat=server>
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
客户:<b><%
# custID %></b><br>
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
未结的订单:<b><%
# orderCount %></b>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</form>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</body>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2. 绑定到集合和列表
绑定到Array List
C# :
void Page_Load(Object Sender, EventArgs E)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
if (!Page.IsPostBack)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ArrayList values = new ArrayList();
values.Add ("IN");
values.Add ("KS");
values.Add ("MD");
values.Add ("MI");
values.Add ("OR");
values.Add ("TN");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
DropDown1.DataSource = values;
DropDown1.DataBind();
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
void SubmitBtn_Click(Object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
Label1.Text = "您选择了:" + DropDown1.SelectedItem.Text;
}
ASP:
<body>
<h3><font face="宋体">数据绑定 DropDownList</font></h3>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<form runat=server>
<asp:DropDownList id="DropDown1" runat="server" />
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<asp:button Text="提交" OnClick="SubmitBtn_Click" runat=server/>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<p>
<asp:Label id=Label1 font-name="宋体" font-size="10.5pt" runat="server" />
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</form>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</body>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
绑定到Hash Table
C#:
void Page_Load(Object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
if (!Page.IsPostBack)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Hashtable h = new Hashtable();
h.Add ("键 1", "值 1");
h.Add ("键 2", "值 2");
h.Add ("键 3", "值 3");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
MyDataList.DataSource = h;
MyDataList.DataBind();
}
}
ASP:
3. 绑定到方法或表达式
<body>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<h3><font face="宋体">到哈希表的数据绑定</font></h3>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<form runat=server>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<asp:DataList id="MyDataList" runat="server"
BorderColor="black"
BorderWidth="1"
GridLines="Both"
CellPadding="4"
CellSpacing="0"
>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<ItemTemplate>
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%
# ((DictionaryEntry)Container.DataItem).Key %> :
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%
# ((DictionaryEntry)Container.DataItem).Value %>
</ItemTemplate>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</asp:DataList>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</form>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</body>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
绑定时的表达式:<%# CType(DataBinder.Eval(Container.DataItem, "BoolValue"), Boolean) %>
C#:
void Page_Load(Object Src, EventArgs E)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
if (!Page.IsPostBack)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ArrayList values = new ArrayList();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
values.Add (0);
values.Add (1);
values.Add (2);
values.Add (3);
values.Add (4);
values.Add (5);
values.Add (6);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
DataList1.DataSource = values;
DataList1.DataBind();
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
String EvenOrOdd(int number)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
if ((number % 2) == 0)
return "偶数";
else
return "奇数";
}
ASP:
<body>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<h3><font face="宋体">到方法和表达式的数据绑定</font></h3>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<form runat=server>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<asp:DataList id="DataList1" runat="server"
BorderColor="black"
BorderWidth="1"
GridLines="Both"
CellPadding="3"
CellSpacing="0"
>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<ItemTemplate>
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
数字值:<%
# Container.DataItem %>
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
偶/奇:<%
# EvenOrOdd((int) Container.DataItem) %>
</ItemTemplate>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</asp:datalist>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</form>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</body>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1716585