我正在我的 Web 应用程序中实现一个中继器来显示数据。我想在类似于 GridView 中的内置功能的列中添加功能操作链接。任何人都可以给我所需的步骤吗?我假设我将为每一行添加一个 LinkButton 控件,以某种方式将 OnClick 事件处理程序设置为指向相同的方法,并以某种方式将行上的唯一标识符作为参数传递。
谢谢!
我猜这就是你想要的。
<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);
}
使用链接按钮。这样,您可以在后面的代码中处理 OnClick 事件。
首先,您将在标记中设置链接按钮的 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"]);
}
}