Repeater 控件用于显示重复的项目列表,这些项目被限制在该控件。Repeater 控件可被绑定到数据库表、XML 文件或者其他项目列表。
以下的完整的读取Repeater的值的方法:
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<script language="C#" runat="server">
void Page_Load(Object sender, EventArgs e)
{
// 创建连接到pubs数据库
// 访问本地的数据库
SqlConnection myConnection = new SqlConnection("server=localhost" +
"database=pubsTrusted_Connection=Yes")
// 使用select语句查询title表中的所有记录
SqlDataAdapter myCommand = new SqlDataAdapter("SELECT * FROM" +
" Titles", myConnection)
// 创建并且填充结果集
DataSet ds = new DataSet()
myCommand.Fill(ds)
// 绑定数据到MyRepeater
MyRepeater.DataSource = ds
MyRepeater.DataBind()
}
</script>
<%-- 循环读取 MyRepeater中的数据--%>
<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:c}") %>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</Table>
</FooterTemplate>
</ASP:Repeater>
</body>
</html>
一种是你前台使用javascript脚本控制,第一次加载时其他数据隐藏,点击“展开”时显示出来。另一种方法是在后台,第一次加载你只查询一条数据绑定,点击展开时,查询多条。你可以把绑定写成一个方法,把条件作为参数,可以分别调用就可以了。
暂时只想到这2个,希望对你有帮助
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)