假设在Repeater中执行删除操作,有两种处理方式,第一种是使用<a>重新转回到本页,在url中添加参数。
第二种方式,是使用服务器控件,引发repeater的onitemcommand事件,这是一种服务器回发操作(IsPostBack=true),
类似于使用GridView时用deletecommand,但repeater要求必须使EnableViewState=true.
结合自定义分页控件(uercontrol),倘若要求删除操作后仍然回到当前页,则分页控件必须通过url参数获得当前页等值。
第二种方式,可封装到uercontrol的内部用默认的viewstate处理
<div id="d_officeright">
<asp:Repeater ID="repInfos" runat="server" EnableViewState="true" OnItemCommand="repInfos_ItemCommand">
<HeaderTemplate>
<table>
<tr>
<th>
信息标题
</th>
<th>
发布日期
</th>
<th>
信息状态
</th>
<th>
操作
</th>
<th>
删除
</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<%# Eval("title")%>
</td>
<td>
<%# DataBinder.Eval(Container.DataItem,"addTime","{0:yyyy-MM-dd}")%>
</td>
<td>
<%# ConvertStatusToZh(Eval("infoStatus").ToString())%>
</td>
<td>
<a href="GoViewInfo.aspx?id=<%# Eval("id").ToString()%>&infotype=<%# Eval("infotype").ToString()%>">
查看"<%# Eval("id").ToString()%>"</a>
</td>
<td>
<a href='<%# @"?action=delete&id="+Eval("id").ToString() +@"&infotype="+Eval("infotype").ToString() %>'
runat="server">删除</a>
<asp:LinkButton ID="lb1" runat="server" CommandName="Delete" CommandArgument='<%# Eval("id").ToString()+"|" + Eval("infotype").ToString()%>'>回发删除</asp:LinkButton>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
<tr>
<td>
</td>
</tr>
</table>
</FooterTemplate>
</asp:Repeater>
<uc2:PageBar ID="PageBar1" runat="server" />
</div>
override protected void OnInit(EventArgs e)
{
//InitializeComponent();
this.PageBar1.PageChangeEvent += new EventHandler(PageBar1_PageChangeEvent);
this.PageBar1.PageSize = 2;
base.OnInit(e);
}
protected void Page_Load(object sender, EventArgs e)
{
if (WebBusiness.IsSessionTimeOut(this)) return;
string action = Request.QueryString["action"];
DoAction(action);
}
void PageBar1_PageChangeEvent(object sender, EventArgs e)
{
BoundData();
}
public string ConvertStatusToZh(string s)
{
switch (s)
{
case "0":
return "<font color='green'>待审核</font>";
case "1":
return "使用中";
case "2":
return "<font color='gray'>已删除</font>";
case "3":
return "<font color='yellow'>已过期</font>";
default:
return "<font color='green'>待审核</font>";
}
}
protected void DoAction(string action)
{
switch (action)
{
case "delete":
DeleteItem(Request.QueryString["id"], Request.QueryString["infotype"]);
BoundData();
break;
default:
if (!Page.IsPostBack) BoundData();
return;
}
}
protected void BoundData()
{
int itemcount = 0;
ICollection<LgInfo> c = LogisticInfo.GetInfoListByUser(Session["userid"].ToString(),
this.PageBar1.PageIndex, this.PageBar1.PageSize, out itemcount);
if (c != null)
{
this.repInfos.DataSource = c;
this.repInfos.DataBind();
}
this.PageBar1.RecordCount = itemcount;
}
protected void DeleteItem(string id, string infotype)
{
string err;
LgInfo c = new LgInfo();
c.Id = int.Parse(id);
c.InfoType = int.Parse(infotype);
LogisticInfo.DeleteLgInfo(c, out err);
}
protected void repInfos_ItemCommand(object source, RepeaterCommandEventArgs e)
{
string cmd = e.CommandName;
string parm=e.CommandArgument.ToString();
if (cmd == "Delete")
{
DeleteItem(parm.Split('|')[0], parm.Split('|')[1]);
}
BoundData();
}