2009-04-20 30 views
3

我在我的web應用程序中實現Repeater以顯示數據。我想在類似於GridView中的內置功能的列中添加功能性操作鏈接。任何人都可以給我所需的步驟嗎?我假設我將爲每行添加一個LinkBut​​ton控件,以某種方式設置OnClick事件處理程序指向相同的方法,並以某種方式傳遞行中的唯一標識符作爲參數。在Repeater控件中實現功能鏈接

謝謝!

回答

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); 
} 
+0

* GREAT *答案。正是我想要的。謝謝!我從來沒有注意到LinkBut​​ton控件上的OnCommand和CommandArgument屬性。 – 2009-04-20 16:11:09

0

使用LinkBut​​tons。這樣,您就可以在後面的代碼中處理OnClick事件。

+0

是的,這就是我的意思。感謝您指出了這一點。我將如何去設置中繼器的事件處理程序,並傳入唯一的ID有一個參數? – 2009-04-20 15:44:03

-1

首先,您將在標記中設置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"]); 
    } 
}