データを表示するために Web アプリケーションに Repeater を実装しています。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);
}
LinkButton を使用します。そうすれば、コード ビハインドで OnClick イベントを処理できます。
まず、マークアップで linkbutton の 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"]);
}
}