Articles in this section
Category / Section

How to add a custom command with server-side event handling in .NET Grid?

4 mins 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 using  Essential Studio for JavaScript

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
        }

Note:

A new version of Essential Studio for ASP.NET is available. Versions prior to the release of Essential Studio 2014, Volume 2 will now be referred to as a classic versions.The new ASP.NET suite is powered by Essential Studio for JavaScript providing client-side rendering of HTML 5-JavaScript controls, offering better performance, and better support for touch interactivity. The new version includes all the features of the old version, so migration is easy.

The Classic controls can be used in existing projects; however, if you are starting a new project, we recommend using the latest version of Essential Studio for ASP.NET. Although Syncfusion will continue to support all Classic Versions, we are happy to assist you in migrating to the newest edition.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls. If you have any queries or require clarifications, please let us know in the comments section below.

You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

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
Access denied
Access denied