ASP.NET資料繫結的記憶片段實現代碼

來源:互聯網
上載者:User

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";
}
}
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.