asp:Repeater 使用备忘录

假设在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();
}

转载于:https://www.cnblogs.com/fightLonely/archive/2011/03/15/1985032.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值