如何在 Repeater 的事件中得到 當前 Item 繫結資料?

來源:互聯網
上載者:User

如何在 Repeater 的事件中得到 當前 Item 繫結資料?

我用Repeater實現一個留言本,我的 Repeater(id為Rp1) 的 ItemTemplate 中有一個用於刪除留言的 LinkButton(id為DelButton)。

留言本表結構
Table Message
(
ID,
UserName,
Message
)

我想用用戶端指令碼實現刪之前的確認操作。即在刪除前,提示出現一個指令碼對話方塊。“您確信要刪除 UserName 的留言嗎?”其中UserName來自於Repeater的綁定資料,不同的Item,綁定的UserName值也不同。

經過acewang(龍芯*Inside!) 和SVG(ben) 兩位大蝦的協助,我最後的實現辦法如下:

顯示頁面:

<SCRIPT LANGUAGE="JavaScript">
<!--
function confirm_del(UserName)
{
 return confirm('您確定要刪除“' + UserName + '”嗎?');
}
//-->
</SCRIPT>

<asp:Repeater id="repGuestbook" runat="server">
        <ItemTemplate>
        ....
        ....
        <asp:LinkButton id="DelButton" runat="server" OnCommand="Del_Click" CommandName="Del" CommandArgument= '<%# DataBinder.Eval(Container.DataItem,"ID") %>'>刪除留言</asp:LinkButton>
        ....
        ....
        </ItemTemplate>
</asp:Repeater>

字碼頁面:

private void InitializeComponent()
{   
      ....
      this.Rp1.ItemDataBound += new System.Web.UI.WebControls.RepeaterItemEventHandler(this.Repeater1_ItemDataBound);
      ....
}

private void Repeater1_ItemDataBound(object source, System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
      if (e.Item.ItemType==ListItemType.Item)
      {
          LinkButton lb=(LinkButton)e.Item.FindControl("MyLB");
          lb.Attributes["onClick"]="JavaScript:return confirm_del('" + DataBinder.Eval(e.Item.DataItem, "UserName") +"');return false;";
          // lb.Attributes["onClick"]="JavaScript:return confirm_del('" + ((DataRowView)(e.Item.DataItem))["UserName"] +"');return false;";
      }

}

上面的 onClick的實現示範了兩種實現的辦法。(第一種被注釋掉了。)
實際應用的時候只需要一種就可以了。

DataBinder.Eval(e.Item.DataItem, "UserName") 這種方法使用簡單。
((DataRowView)(e.Item.DataItem))["UserName"] 這種方法執行效率高。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.