C# how to access the buttons of Telerik RadGrid - c#

I am using Telerik RadGrid, 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.

Related

DevExpress: How to create click event handler?

In my app I have a table with some rows. When I press on a '+' next to row, I need to expand it and display another table. How can I add an event handler to code for this '+'? When I press twice on it, I create event handler for click event for all row and I need it only for a '+' button.
This is what it generates:
private void GridCandidates_Click(object sender, EventArgs e)
{
}
This is how I added '+':
[NotMapped]
public List<CandidateStatusLog> CandidateStatusesLog { get; set; }
Most of the events related to the GridControl are actually fired by the view, not the control. For instance, in this case you can handle the GridView's MasterRowExpanding event which is fired as the [+] is clicked and a detail table is about to be displayed.
If you want to dynamically change the details view at runtime, I would suggest reviewing the Load Details Dynamically by Handling Events documentation as well.
The GridControl should automatically be able to generate a detail view if the parent object has a collection property such as a List like you're doing. See the Binding to Objects with Collection Properties for information about this.

ASP.NET How to handle User Control Event in page that contains the User Control?

I have a page that that contains a User Control and a submit Button. The User Control contains a Drop Down List.
I want to make the Button on the page visible if the user selects an item in the User Control's Drop Down List.
How can I make the page recognise the Drop Down List OnSelectedIndexChanged event?
Expose a custom public event in your user control - MySelectedIndexChanged. Subscribe to this event in your page class. Effectively delegates and events.
Read more on MSDN
Add own event to your UserControl and subscribe on it in page's code-behind. In SelectedIndexChanged event handler fire your event if somebody subscribe on it. Detailse explanation with code you may find here: Easily Raise Events From ASP.NET ASCX User Controls

Adding LinkButtons to DataGrid rows in ItemDataBound

I have a fairly standard DataGrid. It contains a few BoundColumns.
I'm overloading one of these columns to contain either text or a LinkButton depending on some characteristics of the bound item.
So.. in the ItemDataBound event for the Grid, I check a few things and add either a Label Control, or LinkButton Control to the proper Cell.
If I click on one of these LinkButtons, a postback occurs, but it does not call the method in the event handler of the button.
I have seen some people say that events will not work if you create controls AFTER the PreInit page event. Is that true? If so, there must be a way I can manually wire these up?
I have tried creating all of the buttons in PreInit and only adding them in ItemDataBound which unsurprisingly did not work.
I have heard others say the buttons and each parent control must have a unique ID. Is that true?
Rather than blindly swing away at this, I'd like to understand exactly why I can't do this.
Thanks
As far as I understand it, if you put a link button inside a DataGrid you need to use the RowCommand, i.e.
<asp:LinkButton ID="btnUpdate" CommandName="Something" CommandArgument='<%# Bind("something") %>' runat="server">Update</asp:LinkButton>
Then you need to bind the OnRowCommand event to a function in your DataGrid. This will pass the Command Argument that was given to link button and the name of the command in an Event Argument. You can use this to re-act appropriately to the event.

How to know which generated button was pushed in listbox?

I'm developing a WP7 app and have a listbox with generated buttons all supposed to lead to somewhere specific. I can't figure out how to know which button was pushed at runtime. The list gets generated from a collection of objects with a couple of attributes in each. One of those attributes contain a value that I need to get to be able to know where to send the user.
So my desired process is that the user clicks on an item in the listbox, passing the value of the attribute in the object the button was generated from, to a click handler which sends the user to the right place.
Any suggestions?
I presume your ListBox contains a ItemTemplate which constructs a Button for each of the items bound to your list? if this is the case, within your Click event handler you need to inspect the DataContext of the button that was clicked:
private void Button_Click(object sender, RoutedEventArgs e)
{
Button btn = sender as Button;
var myObject = btn.DataContext;
}
As an aside, if you are using this for navigation, a ListBox will not give you very good performance. See the following blog post for an alternative:
http://www.scottlogic.co.uk/blog/colin/2011/04/a-fast-loading-windows-phone-7-navigationlist-control/
Check the sender property of the OnClick event handler for the click handling.
Alternatively you may want to handle the SelectionChanged event of the ListBox and then query the contents of the SelectedItem.

Generating event for button inside gridview

In my project I'm showing a gridview in which I'hv included a buttonfield...I want to download a file, when user clicks the button inside gridview whose path is stored in database
and the file is stored in localfile system...
thanks in advance.
You can use the RowCommand Event of a gridview to responde to a button click and perform an action.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcommand.aspx
You can use the RowCommand event of the GridView control. Make sure your button has setup the CommandName and CommandArgument. The CommandName will distinguish between different events handled by the GridView RowCommand event. And the CommandArgument will contain any value you need to send to the GridView RowCommand through the Button click event.

Categories