您的位置:首页 > 其它

如何使用DataBinder.Eval()方法进行数据绑定

2006-11-14 21:56 1116 查看
<%@ Import Namespace="System.Data" %>


<%@ Import Namespace="System.Data.SqlClient" %>


<html>


<script language="C#" runat="server">




void Page_Load(Object semder, EventArgs e)






{


// 创建数据库连接字符串及SqlDataAdapter对象


string ConnStr = System.Configuration.ConfigurationSettings.AppSettings["ConnectionSqlServer_pubs"];


SqlConnection myConnection = new SqlConnection(ConnStr);


SqlDataAdapter myCommand = new SqlDataAdapter("select * from Titles", myConnection);


// 生成DataSet对象并填充数据


DataSet ds = new DataSet();


myCommand.Fill(ds, "Titles");


// 将Repeater控件进行数据绑定


MyRepeater.DataSource = ds.Tables["Titles"].DefaultView;


MyRepeater.DataBind();


}




</script>


<body topmargin="0" leftmargin="0" marginwidth="0" marginheight="0">


<ASP:Repeater id="MyRepeater" runat="server">


<HeaderTemplate>


<table width="100%" style="font: 8pt verdana">


<tr style="background-color:DFA894">


<th>Title</th>


<th>Title ID</th>


<th>Type</th>


<th>Publisher ID</th>


<th>Price</th>


</tr>


</HeaderTemplate>


<ItemTemplate>


<tr style="background-color:FFECD8">


<td>


<%# DataBinder.Eval(Container.DataItem, "title") %>


</td>


<td>


<%# DataBinder.Eval(Container.DataItem, "title_id") %>


</td>


<td>


<%# DataBinder.Eval(Container.DataItem, "type") %>


</td>


<td>


<%# DataBinder.Eval(Container.DataItem, "pub_id") %>


</td>


<td>


<%# DataBinder.Eval(Container.DataItem, "price", "$ {0}") %>


</td>


</tr>


</ItemTemplate>


<FooterTemplate>


</table>


</FooterTemplate>


</ASP:Repeater>


</body>


</html>


=============================================================

DataBinder.Eval 方法

全部显示
在运行时使用反射来分析和计算对象的数据绑定表达式。此方法允许 RAD 设计器(如 Visual Studio .NET)轻松地生成和分析数据绑定语法。该方法也可通过声明方式在 Web 窗体页上使用,以简化类型之间的转换。

重载列表
运行时计算数据绑定表达式。

[C#] public static object Eval(object, string);

在运行时计算数据绑定表达式,并将结果格式化为要在请求浏览器中显示的文本。

[C#] public static string Eval(object, string, string);

示例
[Visual Basic, C#, JScript] 下面的示例说明如何以声明方式使用 Eval 方法以绑定到 Price 字段。本示例使用的容器语法假定您正在使用一个列表 Web 服务器控件。格式参数将数字格式化为将由请求浏览器显示的区域设置特定的货币字符串。

[Visual Basic, C#, JScript] 注意 此示例显示如何使用 Eval 的一个重载版本。有关其他可用示例,请参阅单独的重载主题。

[C#]
<%# DataBinder.Eval(Container.DataItem, "Price", "{0:c}") %>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐