ASP.NET Repeater里有个 LinkButton 如何调用后台 带参数的方法
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ASP.NET Repeater里有个 LinkButton 如何调用后台 带参数的方法相关的知识,希望对你有一定的参考价值。
<asp:LinkButton runat="server" ID="lbMore" OnClick="btnSave_Click" CommandArgument='<%#Eval("user_NM") %>'>加关注</asp:LinkButton>
后台
protected void btnSave_Click(object sender, EventArgs e)
RepeaterCommandEventArgs b = (RepeaterCommandEventArgs)e;
Object nm = b.CommandArgument;
addfriend(nm);
public void addfriend(Object nm)
}
添加关注:
<asp:LinkButton ID="lbMore" EnableViewState="True" runat="server" Visible="false" CommandName="More" CommandArgument='<%#DataBinder.Eval(Container.DataItem,"绑定你要加关注的用户ID")%>'
OnClientClick="return confirm('确定要关注该好友吗?');" Style="color: Blue">[添加关注]</asp:LinkButton>
取消关注 :
<asp:LinkButton ID="lbUnMore" EnableViewState="True" runat="server" Visible="false" CommandName="UnMore" CommandArgument='<%#DataBinder.Eval(Container.DataItem,"绑定你要加关注的用户ID")%>'
OnClientClick="return confirm('确定要取消关注该好友吗?');" Style="color: Blue">[取消关注]</asp:LinkButton>
后台写你的操作:
protected void rpt_ItemCOmmand(object sender, RepeaterCommandEventArgs e)
switch (e.CommandName)
case "More":
//TODO:添加关注
break;
case "UnMore":
{
//TODO:取消关注
break;
}
最后控制两个控件的显示和隐藏:
protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
取到对象 如 Info
if (info != null)
(e.Item.FindControl("lbMore") as LinkButton).Visible = !info.是否已经关注;
(e.Item.FindControl("lbUnMore") as LinkButton).Visible = info.是否已经关注;
OK ,好运 参考技术A 不是添加Onclick事件哈。你需要在你的Repeater上添加OnItemCommand事件
如:<asp:Repeater OnItemCommand="rpt_ItemCommand" ...
然后为你的LinkButton添加CommandName属性 :
<asp:LinkButton runat="server" ID="lbMore" OnClick="btnSave_Click" CommandArgument='<%#Eval("user_NM") %>' CommandName="Save">加关注</asp:LinkButton>
之后在后台
protected void rpt_ItemCOmmand(object sender, RepeaterCommandEventArgs e)
switch (e.CommandName)
case "Save":
//TODO:添加关注
break;
参考技术B 1、写一个方法带参数的
2、给Repeater添加ItemCommand事件;然后引用方法
ASP.NET Repeater 分页显示
通过创建多一个Repeater 来作为分页用
<div style="overflow: hidden; padding-bottom: 20px; text-align: center;"> <asp:Repeater ID="rptPaging" runat="server" OnItemCommand="rptPaging_ItemCommand"> <ItemTemplate> <asp:LinkButton ID="btnPage" style="background:darkgray; border:solid 1px #666; font:8pt tahoma;" CommandName="Page" CommandArgument="<%# Container.DataItem %>" runat="server" ForeColor="White" Font-Bold="True"><%#Container.DataItem %> </asp:LinkButton> </ItemTemplate> </asp:Repeater> </div>
后台代码
//此属性将包含当前页码 public int PageNumber { get { if (ViewState["PageNumber"] != null) { return Convert.ToInt32(ViewState["PageNumber"]); } else { return 0; } } set { ViewState["PageNumber"] = value; } }
//绑定记录 public void BindList() { DataTable dt = new DataTable(); if (am.ReadMyBrowse(uiv.UserID, out dt) > 0) { if (dt != null && dt.Rows.Count > 0) { PagedDataSource pgitems = new PagedDataSource(); pgitems.DataSource = dt.DefaultView; pgitems.AllowPaging = true; pgitems.PageSize = 20; pgitems.CurrentPageIndex = PageNumber; if (pgitems.PageCount > 1) { rptPaging.Visible = true; ArrayList pages = new ArrayList(); for (int i = 0; i <= pgitems.PageCount - 1; i++) { pages.Add((i + 1).ToString()); } rptPaging.DataSource = pages; rptPaging.DataBind(); } else { rptPaging.Visible = false; } repeater1.DataSource = pgitems; repeater1.DataBind(); //repeater1.DataSource = dt; //repeater1.DataBind(); } else { repeater1.DataSource = ""; repeater1.DataBind(); rptPaging.DataSource = ""; rptPaging.DataBind(); } } else { repeater1.DataSource = ""; repeater1.DataBind(); rptPaging.DataSource = ""; rptPaging.DataBind(); } }
protected void rptPaging_ItemCommand(object source, System.Web.UI.WebControls.RepeaterCommandEventArgs e) { PageNumber = Convert.ToInt32(e.CommandArgument) - 1; BindRepeater(); }
效果:
以上是关于ASP.NET Repeater里有个 LinkButton 如何调用后台 带参数的方法的主要内容,如果未能解决你的问题,请参考以下文章
asp.net 中repeater控件和linkbutton 的使用方法有哪些?
为 asp.net 的 DataList/Repeater 实现寻呼机
如何在 ASP.NET 中使用带有 Repeater 控件的分页?