This is a migrated thread and some comments may be shown as answers.

Invoke ItemCommand from code behind?

4 Answers 684 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Dan
Top achievements
Rank 2
Dan asked on 09 Aug 2012, 01:59 PM
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.

4 Answers, 1 is accepted

Sort by
0
Accepted
Jayesh Goyani
Top achievements
Rank 2
answered on 09 Aug 2012, 04:52 PM
Hello,

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
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!
0
Jayesh Goyani
Top achievements
Rank 2
answered on 10 Aug 2012, 02:26 PM
Hello,

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
Tags
Grid
Asked by
Dan
Top achievements
Rank 2
Answers by
Jayesh Goyani
Top achievements
Rank 2
Dan
Top achievements
Rank 2
Share this question
or