How to get the data bound to the current item in the repeater event?
I use repeater to implement a message book. In the itemtemplate of my repeater (ID: RP1), there is a linkbutton (ID: delbutton) used to delete the message ).
Message Base table structure
Table message
(
ID,
Username,
Message
)
I want to use a client script to confirm the deletion. A script dialog box is displayed before deletion. "Are you sure you want to delete the username message ?" Here, the username is from the bound data of the repeater. The bound username values for different items are also different.
Go through acewang (guest core * Inside !) With the help of SVG (Ben), my final implementation is as follows:
Display page:
<Script language = "JavaScript">
<! --
Function confirm_del (username)
{
Return confirm ('Are you sure you want to delete '+ 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") %> '> delete a message </ASP: linkbutton>
....
....
</Itemtemplate>
</ASP: repeater>
Code Page:
Private void initializecomponent ()
{
....
This. rp1.itemdatabound + = new system. Web. UI. webcontrols. repeateritemeventhandler (this. repeaterincluitemdatabound );
....
}
Private void repeaterincluitemdatabound (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 ;";
}
}
The onclick implementation above demonstrates two implementation methods. (The first one is commented out .)
In actual application, only one type is needed.
Databinder. eval (E. Item. dataitem, "username") is easy to use.
(Datarowview) (E. Item. dataitem) ["username"] This method is highly efficient.