天天看點

将Button等控件嵌入到repeater中

aspx頁面:

增加兩個事件,及傳值。

<asp:repeater id="rptlist" onitemdatabound="rptlist_itemdatabound" onitemcommand="rptlist_itemcommand" runat="server" datasourceid="sqldatasource1">

   <itemtemplate>

     <%#eval("title") %>   

    <asp:button id="btnpass" runat="server" text="pass" cssclass="btn" commandname="btnpass" itemeventargs=<%# eval("title") %> commandargument=<%# eval("title") %> />     

  </itemtemplate>

</asp:repeater>

cs頁面:

     protected void rptlist_itemcommand(object source, repeatercommandeventargs e)

    {

        {

            if (e.commandname == "btnpass")

            {

                string strca = e.commandargument.tostring();

            }

            response.write(e.commandargument.tostring());

        }

    }

    protected void rptlist_itemdatabound(object sender, repeateritemeventargs e)

        response.write(e.item.tostring());

     }

簡單說明四處

1:onitemdatabound="rptlist_itemdatabound"

     這個事件,跟按鈕點選事件,并沒有關系。在這裡。隻是用于控制按鈕的顯示狀态。

2:onitemcommand="rptlist_itemcommand"

     這個事件,就是重點啦。用于當觸發command相關的事件,用于,當點選了按鈕後,觸發要執行的代碼。

3:commandname="btnpass" 

    主要是每個按鈕的辨別,相當于id一樣。用于背景代碼找到,你點選的是哪個按鈕。

4:commandargument="<%# eval("title") %> "

     傳值,object 類型。 比如你可以把這一行資料的id傳進去。當然可以把整個entity傳進去。

繼續閱讀