还是正在做的项目中遇到了使用Rpt的问题。查过MSDN后得到解决。
以前的后台系统用到了大量的GridView,前台都是Repeater。现在后台也用Repeater了~
代码如下:
ASPX:
<asp:UpdatePanel ID="UpDPanelRptNews" runat="server" ChildrenAsTriggers="true" UpdateMode="Always">
<ContentTemplate>
<asp:Repeater ID="RptNew" runat="server" onitemdatabound="RptNew_ItemDataBound">
<HeaderTemplate>
<table border="0" align="center" cellpadding="1" cellspacing="1" width="580px" class="UserTable">
<tr class="UserTableRow2"><td width="150px"><b>标题</b></td><td width="80px"><b>作者</b></td><td width="75px"><b>类型</b></td><td width="75px"><b>是否显示</b></td><td width="100px"><b>删除</b></td></tr>
</HeaderTemplate>
<ItemTemplate>
<tr onmouseover="c=this.style.backgroundColor;this.style.backgroundColor='#00A9FF'" onmouseout="this.style.backgroundColor=c"><td width="150px"><a href='Admin_News.aspx?id=<%# Eval("id") %>' title='<%# Eval("title")+"["+Eval("adddate")+"}" %>'><%# WebFunction.StrHelper.CutStringMit(Eval("title").ToString(),20) %></a></td><td width="80px"><%# Eval("addname") %></td><td width="75px"><%# Eval("flag").ToString()=="1"?"动态":"新闻" %></td>
<td width="75px">
<asp:DropDownList ID="DListNewsIsShow" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DListNewsIsShow_SelectedIndexChanged">
<asp:ListItem Value="1">是</asp:ListItem>
<asp:ListItem Value="0">否</asp:ListItem>
</asp:DropDownList><asp:Label ID="LabId" runat="server" Visible="false" Text='<%# Eval("id") %>'></asp:Label>
</td>
</ItemTemplate>
<FooterTemplate></table></FooterTemplate>
</asp:Repeater>
</ContentTemplate>
</asp:UpdatePanel>
CS:
//遍历Rpt中DListNewsIsShow的数据读取
protected void RptNew_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.SelectedItem)
{
DataRowView rowv = (DataRowView)e.Item.DataItem;
DropDownList DListNewsIsShow = (DropDownList)e.Item.FindControl("DListNewsIsShow");
DListNewsIsShow.Items.FindByValue(rowv["isshow"].ToString()).Selected = true;
}
}
//异步更新新闻是否显示
protected void DListNewsIsShow_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList DListFNewsIsShow = sender as DropDownList;
RepeaterItem item = DListFNewsIsShow.Parent as RepeaterItem; //转为所在Item
Label LabFId = item.FindControl("LabId") as Label; //查找
NewsInfo newsInfoRpt = new NewsInfo();
newsInfoRpt.Id = LabFId.Text.ToString();
newsInfoRpt.IsShow = Convert.ToInt16(DListFNewsIsShow.SelectedValue.ToString());
if(news.Update(newsInfoRpt,"1")<=0)
{
ScriptManager.RegisterStartupScript(UpDPanelRptNews, typeof(UpdatePanel), "JsAlert", "alert('对不起,更新失败!!')", true);
}
}