I would like to select and fire the event of the first row of a grid on loading time, server side.
How to select a row is easy
RagGrid1.Items[0].Selected = true;
but this doesn't fire the event
OnItemCommand="..."
any idea ?
thanks
Renato
1 Answer, 1 is accepted
0
Accepted
Attila Antal
Telerik team
answered on 26 Apr 2023, 05:09 PM
Hi Renato,
The ItemCommand is an event that is triggered only when the user interacts with it. Only setting a row's Selected property, will not trigger the event. You can, however, programmatically invoke the ItemCommand.
Items are not available at page load, so selecting them can be done by adding the item index to the selected index collection.
Accessing the item to trigger a command event, can be done in the PreRender event.
Capturing the triggered command can be done in the ItemCommand event.
protectedvoidPage_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Select an item by adding the itemindex to the SelectedIndexes collection in the PageLoad
RadGrid1.SelectedIndexes.Add(newint[] { 0 });
}
}
protectedvoidPage_PreRender(object sender, EventArgs e)
{
if (!IsPostBack)
{
// access the first item (NOT available in Page Load, that is why using the PreRender)
GridItem firstItem = RadGrid1.Items[0];
// trigger a Custom Command event with params
firstItem.FireCommandEvent("MyCustomCommand", "SomeArgumentsHere");
}
}
protectedvoidRadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
if(e.CommandName == "MyCustomCommand")
{
string arguments = e.CommandArgument.ToString(); //returns: SomeArgumentsHere
}
}
For more details, please refer to the following articles:
Heads up! Telerik UI for ASP.NET AJAX versions for .NET 3.5 and 4.0 are retired. Progress will continue shipping assemblies compatible with .NET 4.5 and later. See whether this affects your apps in this article.