C# how to access the buttons of Telerik RadGrid
I am using Telerik RadG开发者_开发问答rid, i add a new button in the grid but how can i write an event for this button (Buy) for example when the user press (Buy) it will add this item to his cart with its price in order to calculate his bill. regards
You need to listen for the ItemCommand event:
<telerik:GridButtonColumn UniqueName="Buy" ButtonType="LinkButton"
Text="Buy" ConfirmText="Add to cart?"
OnItemCommand="rg_ItemCommand" CommandName="AddToBasket" />
In your codebehind
protected void rg_ItemCommand(object sender, GridCommandEventArgs e)
{
if(e.CommandName == "AddToBasket")
{
// Add to basket code here
}
}
You may also need to set the CommandArgument
during the ItemCreated
or ItemDatabound
events, or get it using something like rg.MasterTableView.DataKeyValues[e.Item.Index]["ItemId"].ToString();
after setting ClientDataKeyNames="ItemId"
in your MasterTableView settings part in the ascx file (if it is databound).
You need to use the ItemCommandEvent of grid. The ItemCommand event is raised when a button is clicked in the Telerik RadGrid control. This allows you to provide an event-handling method that performs a custom routine whenever this event occurs. Follow the LINK to have more details.
When you create the button you'll want to add an OnClick event to it to handle the click on the button. In this event you'll then add the item to the cart. You'll need to parse the parent row of the button to know which item it is.
Edit:
Since you are using a GridButtonColumn and not adding a button as you stated then this applies instead (from Telerik.com):
This column renderes a button of the specified ButtonType in each corresponding cell of the items of type GridDataItem and GridEditFormItem. You can use this buttons to fire command events that can be handeled in RadGrid.ItemCommand event handler. This, in combination with the event bubbling mechanism in Telerik RadGrid, allows you to create a column of custom button controls, such as Add, Remove, Select or Edit buttons.
So essentially you'll need to use the grids ItemCommand event to handle the button click.
精彩评论