I have a Radgrid and a Radlistbox on my page. There is a GridButtonColumn in the grid which executes commandname "Show". This command requeries the database and rebinds the Radlistbox.
What I want to accomplish is this. When the page is rendered, if the Radgrid contains only a single record, then I want the Show command to be executed automatically without the user clicking the Show button.
Is there a way to do this? The fire command events documentation page (http://www.telerik.com/help/aspnet-ajax/grid-fire-command-event-from-code.html) does not appear to cover invoking the ItemCommand.
Thanks.
What I want to accomplish is this. When the page is rendered, if the Radgrid contains only a single record, then I want the Show command to be executed automatically without the user clicking the Show button.
Is there a way to do this? The fire command events documentation page (http://www.telerik.com/help/aspnet-ajax/grid-fire-command-event-from-code.html) does not appear to cover invoking the ItemCommand.
Thanks.
4 Answers, 1 is accepted
0
Accepted
Jayesh Goyani
Top achievements
Rank 2
answered on 09 Aug 2012, 04:52 PM
Hello,
Thanks,
Jayesh Goyani
protected void Page_PreRender(object sender, EventArgs e) { if (RadGrid1.MasterTableView.Items.Count > 0) { foreach (GridDataItem item in RadGrid1.MasterTableView.Items) { item.FireCommandEvent("MyCommand", "MyArgument"); } } } protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e) { if (e.CommandName == "MyCommand") { // Your logic comes here } }Thanks,
Jayesh Goyani
0
Dan
Top achievements
Rank 2
answered on 09 Aug 2012, 05:03 PM
Excellent. Thank you. I'll give it a try.
Dan
Dan
0
Dan
Top achievements
Rank 2
answered on 09 Aug 2012, 08:05 PM
Jayesh, your code suggestion worked except that the FireCommandEvent logic had to go into the RadGrid1_PreRender method rather than the Page_PreRender method. At Page_PreRender the grid MasterTableView Items were not yet initialized.
Thanks!
Thanks!
0
Jayesh Goyani
Top achievements
Rank 2
answered on 10 Aug 2012, 02:26 PM
Hello,
Please try with below code snippet.
Thanks,
Jayesh Goyani
Please try with below code snippet.
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" OnNeedDataSource="RadGrid1_NeedDataSource" OnItemCommand="RadGrid1_ItemCommand" onprerender="RadGrid1_PreRender"> <MasterTableView > <Columns> <telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID"> </telerik:GridBoundColumn> <telerik:GridBoundColumn DataField="Name" UniqueName="Name" HeaderText="Name"> </telerik:GridBoundColumn> </Columns> </MasterTableView> </telerik:RadGrid>protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e) { dynamic data = new[] { new { ID = 1, Name ="Parent"}, new { ID = 2, Name = "Parent"}, new { ID = 3, Name = "Parent"}, new { ID = 4, Name = "Parent"}, new { ID = 5, Name ="Parent"} }; RadGrid1.DataSource = data; }protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e) { if (e.CommandName == "MyCommand") { // Your logic comes here } }protected void RadGrid1_PreRender(object sender, EventArgs e) { if (RadGrid1.MasterTableView.Items.Count > 0) { foreach (GridDataItem item in RadGrid1.MasterTableView.Items) { item.FireCommandEvent("MyCommand", "MyArgument"); } } }Thanks,
Jayesh Goyani