前些日子,做个企业网站。在前台用repeater控件绑定数据,觉得这个例子很典型,所以就记录下来。
不敢说为大虾们提供什么?只为刚出道的类似于我现在这样状况的小鸟提供参考吧
标题显示在前台,点击查看详细信息
首先添加一个页面,用于绑定数据
再添加一个页面,用于显示数据的详细信息 Detailed.aspx
<div>
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate> //头模板,只显示一次
<ul style=" list-style: none;"> //一般都把一些<ul>啊<table>啊,等放在这里,把他们的尾部放在<FooterTemplate>
</HeaderTemplate>
//ItemTemplate模板,用于绑定数据,你懂的
//关键是链接里面那个绑定ID的参数,这个是绑定到数据库中数据的唯一标识(参数,红色部分),跳到详细页面
//浅色部分是绑定显示的标题
<ItemTemplate>
<li><a href="Detailed.aspx?id=<%#Eval("ID") %>"><%#Eval("Subject") %></a></li>
</ItemTemplate>
//您可以在 <ItemTemplate> 元素后添加 <AlternatingItemTemplate> 元素,这样就可以描述交替行的外观了。
//这个模版用来显示偶数行的数据。
<AlternatingItemTemplate>
<tr bgcolor="lime">
<td><%#DataBinder.Eval(Container.DataItem, "Subject")%></td>
<td><%#DataBinder.Eval(Container.DataItem, "Contents")%></td>
</tr>
</AlternatingItemTemplate>
//<SeparatorTemplate> 元素能够用于描述每个记录之间的分隔符。下面的例子在每个表格行之间插入了一条水平线:
<SeparatorTemplate>
<li colspan="6"><hr style=" border: dotted 1px green;"/></li>
</SeparatorTemplate>
<FooterTemplate></ul></FooterTemplate>
</asp:Repeater>
</div>
后台数据绑定的方法和gridview一样的
然后在 Detailed.aspx页面用
string id = Request.QueryString["id"].ToString(); 接收传过来的参数id。
然后根据这个id取得model实体
public void detail()
{
string id = Request.QueryString["id"].ToString();
model.tb_article article = arbll.GetModel(Convert.ToInt32(id));
this.Label1.Text = article.Subject;
this.Label2.Text = article.Contents;
}
然后在 Detailed.aspx页面离得两个label控件取值就好了,简单吧