The ItemCommand server-side event of WebHierarchicalDataGrid 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 for Parent and Child band, and handle WebHierarchicalDataGrid’s ItemCommand server-side event to display some information when the button is clicked.
ection at the end of this topic.
Add TemplateField to Parent Band:
You can follow the steps from Using ItemCommand Event to Access Cell Item (WebDataGrid)
Add TemplateField to Child Band:
In Design View select the control/component and a Smart Tag anchor will appear. Select “Edit Bands”.
“Edit WebHierarchicalDataGrid Bands” window will appear. From there select the desired band.
From 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 WebHierarchicalDataGrid’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 onto ItemTemplate
Select “End Template Editing” to finish editing.
Add a server-side event handler to the ItemCommand event (code-behind).
In Visual Basic:
Protected Sub WebHierarchicalDataGrid1_ItemCommand(sender As Object,
e As HandleCommandEventArgs)
End Sub
In C#:
protected void WebHierarchicalDataGrid1_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 to that event. The ItemCommand will be fired on the server and will expose to you event arguments through the HandleCommandEventArgs object.
Parent TemplateDataField
In ASPX:
<ig:TemplateDataField Key="Freight">
<ItemTemplate>
<asp:Button ID="Button1" runat="server" Text="Show Freight"
CommandArgument='<%# Eval("Freight") %>' CommandName="Button1Click" />
</ItemTemplate>
<Header Text="Freight">
</Header>
</ig:TemplateDataField>
Child TemplateDataField
In ASPX:
<ig:TemplateDataField Key="Country">
<ItemTemplate>
<asp:Button ID="Button2" runat="server"
Text="Show country"
CommandArgument='<%# Eval("Country") %>'
CommandName="ChildButtonClick" />
</ItemTemplate>
<Header Text="Country">
</Header>
</ig:TemplateDataField>
In Visual Basic:
Protected Sub WebHierarchicalDataGrid1_ItemCommand(sender As Object, e As HandleCommandEventArgs)
If (e.CommandName = "ChildButtonClick") Then
'From Child Grid
'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
CountryValueLbl.Text = commandArgument.ToString()
Else
'From Parent Grid
Dim commandArgument As Object = e.CommandArgument
FreightValueLbl.Text = commandArgument.ToString()
End If
End Sub
In C#:
protected void WebHierarchicalDataGrid1_ItemCommand(object sender, HandleCommandEventArgs e)
{
if (e.CommandName == "ChildButtonClick")
{
//From Child Grid
//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
CountryValueLbl.Text = commandArgument.ToString();
}
else
{
//From Parent Grid
object commandArgument = e.CommandArgument;
FreightValueLbl.Text = commandArgument.ToString();
}
}
Run the application. Click one of the buttons, and notice that the field value is displayer above the WebHierarchicalDataGrid, as shown in the screen shot below.
Related Topics