Imports Infragistics.Web.UI.GridControls
The ItemCommand server-side event of WebDataGrid allows you to easily respond to events (e.g., button click) from controls placed inside TemplateField column.
This walkthrough demonstrates how to add an ASP.NET button inside an bound TemplatedField column, and handle WebDataGrid’s ItemCommand server-side event to display some information when the button is clicked.
ection at the end of this topic.
Before you start writing any code, you should place using/Imports directives in your code-behind so you don’t need to always type out a member’s fully qualified name.
In Visual Basic:
Imports Infragistics.Web.UI.GridControls
In C#:
using Infragistics.Web.UI.GridControls;
In Design View select the control/component and select Properties.
In the Properties window, locate the Columns collection, and click the ellipsis button (…).
Add a TemplateField to the collection.
From Available Fields section select TemplateField and click the Add Field Button.
New column of type TemplateField will be added to the columns collection.
Another way to add TemplateField is to convert Grid Field into a Template Field. Select some Grid field and press “Convert selected Grid Field into a Template Field”.
Click OK button to close the Columns Collection Editor
Add an ASP.NET Button control to the TemplatedField.
From the Design view of your page select the WebDataGrid’s smart tag and click on “Edit Templates”
After the Template Editing Mode is open find the “Freight” TemplateField.
From the toolbox, drag a standard ASP.NET button control into ItemTemplate
Select “End Template Editing” to finish the edit.
Add a server-side event handler to the ItemCommand event (code-behind).
In Visual Basic:
Protected Sub WebDataGrid1_ItemCommand(sender As Object, e As HandleCommandEventArgs)
End Sub
In C#:
protected void WebDataGrid1_ItemCommand(object sender, HandleCommandEventArgs e){ }
Add code to the ItemCommand event to retrieve some row information. When a control sends back an event, the server will want to respond that event. The ItemCommand event will capture the event thrown by the control and expose that event to you through the HandleCommandEventArgs object.
In ASPX:
<ig:TemplateDataField Key="Freight"><ItemTemplate> <asp:Button ID="Button1" runat="server" Text="Button" CommandArgument='<%# Eval("Freight") %>' CommandName="Button1Click" /> </ItemTemplate> <Header Text="Freight"> </Header> </ig:TemplateDataField>
In Visual Basic:
Protected Sub WebDataGrid1_ItemCommand(sender As Object, e As HandleCommandEventArgs)
'e.CommandArgument will give you the value from CommandArgument attribute of the corresponding button
Dim commandArgument As Object = e.CommandArgument
'Make some calculations with Freight field value and pass it to a Label
FreightValueLbl.Text = commandArgument.ToString()
End Sub
In C#:
protected void WebDataGrid1_ItemCommand(object sender, HandleCommandEventArgs e)
{
//e.CommandArgument will give you the value from CommandArgument
attribute of the corresponding button
object commandArgument = e.CommandArgument;
//Make some calculations with Freight field value and pass it to a Label
FreightValueLbl.Text = commandArgument.ToString();
}
Run the application. Click one of the buttons, and notice that Freight field value is displayed above the WebDataGrid, as shown in the screen shot below.
Related Topics