Using Selected Rows in an Editable Grid in a Server-Side C# Action in Infor CRM Web

In my previous article I discussed using the ClientContextService to pass values from client-side code to a server-side C# action. We will use that idea here to allow a server-side C# action to know which rows are selected in an Editable Grid.

The Editable Grid control in Infor CRM isn’t an ASP.NET control. Instead, it adds Javascript to create a completely client-side Dojo grid that uses SData. It’s a 100% client-side control. This means, if you want to use it from a server-side action you’re out of luck. However, using the ClientContextService you can pass information from client-side code to your server-side action, allowing the C# code to work with the Editable Grid.

In this scenario, we have an Editable Grid, with standard buttons next to it to perform actions on the rows selected by the user. The buttons have C# actions to perform updates and other actions on the selected rows in the grid. The user can select one, or many, rows and click one of the buttons to trigger a postback and perform some actions. The tricky part is, we need to let our server-side C# action know which grid rows the user has selected.

The first step is, we need to wire up some Javascript to the OnClientClick of the buttons. The OnClientClick Javascript code executes prior to the postback. We can return true or false to allow, or prevent, the postback from occurring. In the code below we get the selected rows from the Editable Grid and then set it in the ClientContextService. If no rows are selected, we alert the user and return false and everything stops (the C# action will not execute).

The following code is in a LoadAction to wire up the Javascript to the OnClientClick of our button.

// first we need to determine the client-side name of our Editable Grid
var gridId = string.Empty;
foreach (Control ctrl in Controls)
{
    var resCtrl = ctrl as ScriptResourceProvider;
    if (resCtrl != null)
    {
        gridId = resCtrl.ID.Replace("_Strings","");
        break;
    }
}

// this is the client-side script. In this code, we get a reference to the grid
// and then get the selected rows. We set the selected rows in the client context
// service. If no rows are selected we alert the user.
var script = @"var grid = dijit.byId('{0}'), 
				   selectedIds = grid.selection.getSelected().map( function(row) { return row.$key; }).join(); 

			   if (selectedIds != '') { 
				   var context = Sage.Services.getService('ClientContextService'); 
				   context.remove('SelectedGridRows'); 
				   context.add('SelectedGridRows', selectedIds); 
				   return true; 
			   } 
			   else { 
				   alert('There are no selected rows.'); 
				   return false; 
			   } ";

// now wire up the OnClientClick of our button 
button1.OnClientClick = string.Format(script, gridId); 

Now, when our button is selected, the Javascript we wired up to the button will execute and the ID’s of the selected rows will be added to the client context service. Now, in the code for the button, we can use the client context service to get the IDs of the selected rows. The code for the button will look like this:

var contextService = PageWorkItem.Services.Get<Sage.Platform.WebPortal.Services.ClientContextService>();
if (contextService.CurrentContext.ContainsKey("SelectedGridRows"))
{
	var selectedIds = contextService.CurrentContext["SelectedGridRows"].Split(',');
	// now the selectedIds variable is an array of the ID values of the selected rows
	// we can loop through them and do whatever is needed
	
	// for example:
	foreach (var id in selectedIds)
	{
		// this is just an example...
		var contact = Sage.Platform.EntityFactory.GetById<Sage.Entity.Interfaces.IContact>(id);
		contact.Status = "Something";
		contact.Save();
	}
}

Not too bad. It gets the context of the selected Editable Grid rows back to C# so you can do anything you’d like at that point.

ABOUT THE AUTHOR

Ryan Farley

Ryan Farley is the Director of Development for Customer FX and creator of slxdeveloper.com. He's been blogging regularly about SalesLogix, now Infor CRM, since 2001 and believes in sharing with the community. His new passion for CRM is Creatio, formerly bpm'online. He loves C#, Javascript, web development, open source, and Linux. He also loves his hobby as an amateur filmmaker.

7 Comments

  1. I used this in Infor CRM 8.2. Are there changes required for it to work in 8.4?

    Reply
    • It should work fine in 8.4, but I’ve not specifically tested it.

  2. I’m having difficulty with this:
    Does it have to be an editable grid?
    What if the grid is in a Dialog Workspace?
    Where, exactly, does this code go? (I have tried in the main.js file for the top bit and the OnClick C# action for the bottom bit)
    Is there another method for passing selected rows?
    Is this enough questions?

    Many thanks

    Reply
    • This method only works with editable grids, it doesn’t have to be made as “editable”, but it does work only with editable grids. There are other methods for doing similar with standard datagrids. It works if the editable grid is in the dialog workspace. This is all C# code. The C# does inject javascript onto the page, but it’s C# code doing this. The first bit of code needs to go in a LoadAction. The second bit goes wherever you need to retrieve the selected rows, such as the action for a button click.

    • Thank you very much Ryan, I appreciate the help.

    • I’m getting there, but now I am getting an ‘Input string is not in correct format’ error. It has something to do with the script var. I have injected a simple alert(); and the blank message window pops open but something is going awry with the script variable. Is this a known issue? I shall persevere and report back what I figure out.

    • Update: Input string error has been resolved by building the string in two stages.
      var script = string.Format(“var grid = dijit.byId(‘{0}’)”, gridId);
      script += @””;

      Now, however, the .selection property of the grid is reading as ‘undefined’ but I can’t figure out why. Any guidance or alternative methods of getting the data in a selected row more than welcome.

      Many thanks,
      Matt

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe To Our Newsletter

Join our mailing list to receive the latest Infor CRM (Saleslogix) and Creatio (bpm'online) news and product updates!

You have Successfully Subscribed!