在Repeater控件中实现功能链接

时间:2009-04-20 15:35:35

标签: asp.net repeater

我在我的Web应用程序中实现了一个Repeater来显示数据。我想在类似于GridView中的内置功能的列中添加功能操作链接。任何人都可以给我所需的步骤吗?我假设我将为每一行添加一个LinkBut​​ton控件,以某种方式将OnClick事件处理程序设置为指向相同的方法,并以某种方式将行上的唯一标识符作为参数传递。

谢谢!

3 个答案:

答案 0 :(得分:8)

我猜这就是你想要的。

    <asp:Repeater ID="rpt" runat="server">
        <ItemTemplate>
            <asp:LinkButton ID="lbtn" runat="server" OnCommand="lbtn_Command" 
            CommandArgument='<%# DataBinder.Eval(Container.DataItem, "KeyIDColumn") %>' ></asp:LinkButton>
        </ItemTemplate>
    </asp:Repeater>

然后在你的代码中

protected void lbtn_Command(object sender, CommandEventArgs e)
{
    int id = Convert.ToInt32(e.CommandArgument);
}

答案 1 :(得分:0)

使用LinkBut​​tons。这样,您就可以在后面的代码中处理OnClick事件。

答案 2 :(得分:-1)

首先,您可以在标记中设置链接按钮的onclick。然后,您将要为转发器实现ItemDataBound事件。

  if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
         SomeObject obj = e.Item.DataItem as SomeObject; // w/e type of item you are bound to
         var linkButton = e.Item.FindControl("linkButtonId") as LinkButton;
         if(linkButton != null)
         {
              //either set a custom attribute or maybe append it on to the linkButton's ID
              linkButton.Attributes["someUniqueId"] = obj.SomeID;
         }
    }

然后在点击事件中

void lb_Click(object sender, EventArgs e)
{
    LinkButton lb = sender as LinkButton;
    if (lb != null)
    {
        // obviously do some checking to ensure the attribute isn't null
        // and make it the correct datatype.
        DoSomething(lb.Attributes["someUniqueId"]);
    }
}