This is a migrated thread and some comments may be shown as answers.

fireCommand syntax for Custom Command in header row

1 Answer 83 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Mark
Top achievements
Rank 1
Iron
Mark asked on 06 Feb 2020, 04:26 PM

Hopefully this question has a simple answer.  I'm using a RadGrid with 1 hard coded column, and several autogenerated columns.  The grid is mapped to a file uploaded by the user that has unknown columns.  In the header for each dynamic column, i'm inserting a RadComboBox that allows the user to map the column from a predefined list of valid column names.  I add these RadComboBoxes in the ItemCommand event handler.  This works without issue.  When the user is finished making their selections in each of the RadComboBoxes, i need to capture the values from each of the RadComboBoxes and i do this using a custom item command. This is also working well with one caveat.  It only works great if the button that fires the ItmCommand is in the RadGrid header.  This is not ideal just from a display point of view.  i'd prefer to have the button below the grid.  I read through the docs and saw that i can fire a command event via javascript using the masterTable.fireCommand("CustomCommandName");  This works except that i need to be able to pass in a context that tells the server-side event handler that i need access to the header row.  When i use the button in the header and i step through my server-side ItemCommand code i can see that e.Item is of type GridHeaderItem which is perfect, this allows me to access the e.Item.Cells[x].Controls collection to find the RadComboxBox and extract the value that was selected by the user.  When i step through the same server-side ItemCommand code using the button located outside the grid (called via)  masterTable.FireCommand(), e.Item is of type GridDataItem instead of GridHeaderItem and so i'm not able to access the header row controls to find the RadComboBoxes and extract their values.   I can see that fireCommand accepts a commandargument parameter after the command name, but so far i'm not able to figure out what value should be sent to indicate that the command should be fired in the context of the header row and not an item row.  I've tried several different numeric values including -1, 0 and numbers > 0.  In each instance when the event fires e.Item is of type GridDataItem, which is no good.

The one work around that does seem to work, but is a little bit of a hack is to copy the actual javascript code of the button in the header, and then create a button outside the grid that mimics that.  This works, but i end up with something hard coded that looks like this:

<a id="ctl00_MainContent_rgMapFields_ctl00_ctl02_ctl00_lbtnImport1" href="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;ctl00$MainContent$rgMapFields$ctl00$ctl02$ctl00$lbtnImport&quot;, &quot;&quot;, true, &quot;MapFields&quot;, &quot;&quot;, false, true))">Import</a>

This works but it doesn't leave me with a good feeling to rely on this too heavily.  It seems like there should be a simple answer as to what command argument i pass into the fireCommand Javascript method to indicate a command argument for the header.

 

Current javascript method is really basic and looks like this:

        function Import(sender) {
            var masterTable = $find("<%= rgMapFields.ClientID %>").get_masterTableView();
            masterTable.fireCommand("ValidateMapping", "");  // <-- something needs to go here other than ""
        }

I can provide code for the radgrid or any of the event handlers if needed, but i don't know that it's super relevant.  it is all working the way i want with the exception of just taking a button that fires an item command from the header and placing it below the grid.  

Thanks!

-Mark

 

1 Answer, 1 is accepted

Sort by
0
Eyup
Telerik team
answered on 11 Feb 2020, 12:22 PM

Hi Mark,

 

You have these 2 easy options to achieve this requirement:

1. Use the own OnClick or OnCommand event handlers of the external button and access the GridHeaderItem of the grid using the GetItems methods:
https://docs.telerik.com/devtools/aspnet-ajax/controls/grid/how-to/Common/using-the--getitems-getcolumn-and-getcolumnsafe-methods#items

2. Use the own OnClick or OnCommand event handlers of the external button, access the GridHeaderItem of the grid using the approach mentioned above, and then fire a custom grid command from the server (not really required, just sharing for optionality):
https://docs.telerik.com/devtools/aspnet-ajax/controls/grid/control-lifecycle/how-to-fire-command-events

I hope this was simple enough :)

 

Regards,
Eyup
Progress Telerik

Get quickly onboarded and successful with UI for ASP.NET AJAX with the Virtual Classroom technical trainings, available to all active customers. Learn More.
Tags
Grid
Asked by
Mark
Top achievements
Rank 1
Iron
Answers by
Eyup
Telerik team
Share this question
or