Articles in this section
Category / Section

How to add a custom command with server side event handling?

1 min read

Sometimes you may like to handle the server side events for the custom command buttons.

Solution

Since we don’t have server side events support for custom command buttons, we can achieve it using the below workaround.

The template feature of the grid is used to bind the custom button to the grid.

ASPX

 

<ej:Grid ID="OrdersGrid" runat="server" AllowPaging="True">        
        <Columns>
            <ej:Column HeaderText="Details" Template="true" TemplateID="#buttonTemplate" TextAlign="Center" Width="75"/>            
        </Columns>
    </ej:Grid>

JS Render

<script type="text/x-jsrender" id="buttonTemplate">
        <button class="Details" name="Details">Details</button>       
    </script>

 

ASPX

The OnServerRecordClick event of the Grid is enabled in order to trigger the server side record click event.

<ej:Grid ID="OrdersGrid" runat="server" AllowPaging="True" ClientIDMode="Static" OnServerRecordClick="onClick">  
<ClientSideEvents RecordClick="RecordClick" />      
</ej:Grid>

 

JS

 

The button created using column template is converted to ejButton.

<script type="text/javascript">
        
        $(function () {
            $(".Details").ejButton();
            });
   </script>

 

On the click event of the button, the recordClick of the grid is explicitly triggered and the arguments are passed explicitly to the recordClick event of the grid.

 

<script type="text/javascript">
        
        $(function () {
            $(".Details").click(function (e) {
                triggerEvent(e);
            });
            });                     
       
        function triggerEvent(e) {
            var obj = $("#OrdersGrid").data("ejGrid");
            var args = { currentTarget: e.currentTarget.name, selectedRecord: obj.getSelectedRecords(), selectedIndex: obj.model.selectedRowIndex };
            obj._trigger("recordClick", args);
        }
 
        function RecordClick(e) {           
            if (e.currentTarget != "Details")
                return false
            else {
                triggerEvent(e);
            }
        }
 
    </script>

At the server side event of the recordClick, the target details and the selectedRecord details are obtained in the GridEventArgs.

ASPX.CS

protected void onClick(object Sender, GridEventArgs e)
        {
            var currentTarget = e.Arguments["currentTarget"];//returns current target details
            var selectedRecord = e.Arguments["selectedRecord"];//yields selected record details
            var selectedIndex = e.Arguments["selectedIndex"];//yields selectedIndex value
        }

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments
Please sign in to leave a comment