If your DataList, Repeater, or GridView control template includes Button, LinkButton, or ImageButton Web Server controls, these buttons can send their Click events to the containing DataList, Repeater, or GridView control. This allows you to include buttons for functions not already defined for the DataList and GridView controls (edit, delete, update, and cancel) and to define functionality for the Repeater control.
To respond to button events in DataList, Repeater, and GridView controls
-
Include a Button, LinkButton, or ImageButton in a control template.
-
Set the button's CommandName property to a string that identifies its function, such as "sort" or "copy".
-
Create a method for the ItemCommand event of the containing control. In the method, do the following:
-
Check the CommandName property of the event-argument object to see what string was passed.
-
Perform the appropriate logic for the button that the user clicked.
The following example shows how you can respond to a button click in a DataList control. In the example, the ItemTemplate contains an ImageButton control that displays a shopping cart. The button sends the command AddToCart. The ItemCommand event handler determines which button was clicked, and — if it was the shopping cart button — performs the appropriate logic.
Visual BasicProtected Sub DataList1_ItemCommand(ByVal source As Object, _ ByVal e As DataListCommandEventArgs) If e.CommandName = "AddToCart" Then ' Add code here to add the item to the shopping cart. ' Use the value of e.Item.ItemIndex to find the data row ' in the data source. End If End Sub
C#protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e) { if (e.CommandName == "AddToCart") { // Add code here to add the item to the shopping cart. // Use the value of e.Item.ItemIndex to find the data row // in the data source. } }
-
For an example using the DataList Web server control, see How to: Allow Users to Select Items in DataList Web Server Controls.
See Also
ItemDataBound event handler:
Dim approvalID As Integer = DataBinder.Eval(e.Item.DataItem, "ApprovalID")
Dim rbl As RadioButtonList = e.Item.FindControl("radApproveOrDecline")
rbl.Items(0).Attributes.Add("OnClick", "show('approve', '" & approvalID & "');")
rbl.Items(1).Attributes.Add("OnClick", "show('decline', '" & approvalID & "');")
Those attributes don't get render during a postback so I lose my javascript. Is there some way to prevent this?
1. Make sure to bind your Repeater, DataGrid or DataList control only on "Not Page.IsPostBack".
If Not Page.IsPostBack Then
...
Repeater1.DataBind()
End If
I found this un-useful because it causes an invalid postback/callback argument exception.
In the itemTemplate region i placed the following button tag:
<asp:Button ID="btnReProcessOrder" runat="server" Text="Reprocess Order" CommandName="Process" />
and in the code behind i added the following event to my dataList
Private Sub dlOrders_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles dlOrders.ItemCommand
Dim row As DataRowView
row = CType(e.Item.DataItem, DataRowView)
Select Case e.CommandName
Case "Process"
'process subroutine here
End Select
End Sub
here is the exact text of the error message:
Invalid postback or callback argument. Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation
Is there something i am missing? do i actually have to turn off page validation just to get this ItemCommand to work? i find that silly.
Thanks,
Nate