Item Command Event Not Fire on Dynamic Grid

7 posts, 0 answers
  1. Jyosana
    Jyosana avatar
    12 posts
    Member since:
    Aug 2013

    Posted 06 Sep 2013 Link to this post

    Hello, All

    I have one issue in my current page , when i am working in RadGrid and I have created grid dynamically and try handle item command event by writing my code as below mention but not working
    I want to create nested grid on clic

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="demotemp.aspx.cs" Inherits="HcareTemplate.demotemp" MasterPageFile="HTemplateMaster.Master" %>
     
    <%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" runat="server">
        <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
        <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
        </telerik:RadAjaxManager>
    </asp:Content>
     
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Data;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Telerik.Web.UI;
     
    namespace HcareTemplate
    {
        public partial class demotemp : System.Web.UI.Page
        {
            HcareTemplate.HcareTemplateService objService = new HcareTemplate.HcareTemplateService();
            DataSet datalist;
            public void DefineGridStructure()
            {
                RadGrid RadGrid1 = new RadGrid();
                RadGrid1.ItemCommand += new GridCommandEventHandler(RadGrid1_ItemCommand);
                RadGrid1.ID = "RadGrid1";
                datalist = objService.getSoapTemplateQuestions(1, 16);
                RadGrid1.DataSource = datalist;
                RadGrid1.MasterTableView.DataKeyNames = new string[] { "TemplateId" };
                RadGrid1.Skin = "Outlook";
                RadGrid1.Width = Unit.Percentage(30);
                RadGrid1.AllowPaging = true;
                RadGrid1.AutoGenerateColumns = false;
                RadGrid1.ClientSettings.Selecting.AllowRowSelect = true;
                //Add columns
                GridBoundColumn boundColumn;
                boundColumn = new GridBoundColumn();
                boundColumn.DataField = "TemplateId";
                boundColumn.HeaderText = "TemplateId";
                RadGrid1.MasterTableView.Columns.Add(boundColumn);
                boundColumn = new GridBoundColumn();
                boundColumn.DataField = "ProviderText";
                boundColumn.HeaderText = "ProviderText";
                RadGrid1.MasterTableView.Columns.Add(boundColumn);
                this.PlaceHolder1.Controls.Add(RadGrid1);
            }
     
            public void Page_Init(object sender, System.EventArgs e)
            {
                DefineGridStructure();
            }
     
            public void Page_Load(object sender, System.EventArgs e)
            {
                RadGrid grid = (RadGrid)PlaceHolder1.FindControl("RadGrid1");
                RadAjaxManager1.AjaxSettings.AddAjaxSetting(grid, grid);
     
            }
            public void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
            {
                if (e.CommandName == "RowClick")
                {
     
                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "test", "<script>alert('hi');</script>");
                    GridEditableItem item = e.Item as GridEditableItem;
                    int ParentId = Convert.ToInt32(item.GetDataKeyValue("TemplateId"));
                    DataSet ds = objService.getTemplateProiderText(ParentId);
                    
                    datalist = objService.getSoapTemplateQuestions(1, ParentId);
                    if (datalist.Tables[0].Rows.Count > 0)
                    {
                        DefineGridStructure();
                    }
                }
            }
        }
    }

    k on selected row of current grid

  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 08 Sep 2013 Link to this post

    Hi Jyosana,

    Please add the following code to your radgrid so as to raise the RowClick event in the ItemCommand event.

    C#:
    RadGrid1.ClientSettings.EnablePostBackOnRowClick = true;

    Thanks,
    Princy
  3. sanjay singhvi
    sanjay singhvi avatar
    2 posts
    Member since:
    Jul 2009

    Posted 09 Sep 2013 Link to this post

    Hello  Princy 

    Thanks for your reply..
    but I have already tried this solution by adding line 
    RadGrid1.ClientSettings.EnablePostBackOnRowClick = true;

     But its not fire the Itemcommand event. it will load whole page with page_load and page_init by skiping itemcommand event method.
  4. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 09 Sep 2013 Link to this post

    Hi Sanjay,

    I'm unable to replicate the issue,could you please share your full code so as to identify the issue.Below is code that I tried.

    ASPX:
    <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>

    C#:
    public void DefineGridStructure()
            {
                RadGrid RadGrid1 = new RadGrid();
                RadGrid1.ItemCommand += new GridCommandEventHandler(RadGrid1_ItemCommand);
                RadGrid1.ID = "RadGrid1";     
                RadGrid1.DataSourceID = "SqlDataSource1";
                RadGrid1.MasterTableView.DataKeyNames = new string[] { "OrderID" };
                RadGrid1.Skin = "Outlook";
                RadGrid1.Width = Unit.Percentage(30);
                RadGrid1.AllowPaging = true;
                RadGrid1.AutoGenerateColumns = false;
                RadGrid1.ClientSettings.Selecting.AllowRowSelect = true;
                RadGrid1.ClientSettings.EnablePostBackOnRowClick = true;
     
                //Add columns
                GridBoundColumn boundColumn;
                boundColumn = new GridBoundColumn();
                boundColumn.DataField = "OrderID";
                boundColumn.HeaderText = "OrderID";
                RadGrid1.MasterTableView.Columns.Add(boundColumn);
                boundColumn = new GridBoundColumn();
                boundColumn.DataField = "Shipcity";
                boundColumn.HeaderText = "Shipcity";
                RadGrid1.MasterTableView.Columns.Add(boundColumn);
                this.PlaceHolder1.Controls.Add(RadGrid1);
            }
      
            public void Page_Init(object sender, System.EventArgs e)
            {
                DefineGridStructure();
            }
          
            public void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
            {
                if (e.CommandName == "RowClick")
                {             
                 //Your Code
                }
            }

    Thanks,
    Princy
  5. sanjay singhvi
    sanjay singhvi avatar
    2 posts
    Member since:
    Jul 2009

    Posted 10 Sep 2013 Link to this post

    Hi Princy 

    In that code i need Recursive grid that means in one Radgrid ItemCommand event i want other grid with different data source and so on.. 
    See the following code i tried but in that after fire RadGrid1_itemCommand it  call DefineGridStructure() function with new grid but after that when i click
    on that newly added grid's row it will refresh the whole page and not calling
    the event method RadGrid2_itemcommand 


    i am trying to resolve this issue since long time but i am not able to solve this issue. 

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="demotemp.aspx.cs" Inherits="HcareTemplate.demotemp"
        MasterPageFile="HTemplateMaster.Master" %>
     
    <%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" runat="server">
        <asp:PlaceHolder ID="PlaceHolder1" runat="server">
            <telerik:RadGrid ID="RadGrid1" runat="server" OnItemCommand="RadGrid1_ItemCommand" OnNeedDataSource="RadGrid1_needDatasource"
                AutoGenerateColumns="false" Width="25%" Skin="Outlook">
                <ClientSettings Selecting-AllowRowSelect="true" EnablePostBackOnRowClick="true">
                </ClientSettings>
                <MasterTableView DataKeyNames="TemplateId">
                    <Columns>
                        <telerik:GridBoundColumn DataField="TemplateId" HeaderText="TemplateId">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="ProviderText" HeaderText="ProviderText">
                        </telerik:GridBoundColumn>
                    </Columns>
                </MasterTableView>
            </telerik:RadGrid>
        </asp:PlaceHolder>
        <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
        </telerik:RadAjaxManager>
    </asp:Content>
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Data;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Telerik.Web.UI;
    namespace HcareTemplate
    {
        public partial class demotemp : System.Web.UI.Page
        {
            HcareTemplate.HcareTemplateService objService = new HcareTemplate.HcareTemplateService();
             
            public void DefineGridStructure(DataSet datalist)
            {
                RadGrid RadGrid1 = new RadGrid();
                RadGrid1.ItemCommand += new GridCommandEventHandler(RadGrid2_ItemCommand);
                RadGrid1.ID = "RadGrid2";
                RadGrid1.DataSource = datalist;
                RadGrid1.MasterTableView.DataKeyNames = new string[] { "TemplateId" };
                RadGrid1.Skin = "Outlook";
                RadGrid1.Width = Unit.Percentage(30);
                RadGrid1.AllowPaging = true;
                RadGrid1.AutoGenerateColumns = false;
                RadGrid1.ClientSettings.Selecting.AllowRowSelect = true;
                RadGrid1.ClientSettings.EnablePostBackOnRowClick = true;
     
                //Add columns
                GridBoundColumn boundColumn;
                boundColumn = new GridBoundColumn();
                boundColumn.DataField = "ProviderText";
                boundColumn.HeaderText = "ProviderText";
                RadGrid1.MasterTableView.Columns.Add(boundColumn);
                boundColumn = new GridBoundColumn();
                boundColumn.DataField = "ProviderText";
                boundColumn.HeaderText = "ProviderText";
                RadGrid1.MasterTableView.Columns.Add(boundColumn);
                this.PlaceHolder1.Controls.Add(RadGrid1);
            }
     
            public void Page_Init(object sender, System.EventArgs e)
            {
                
            }
            public void RadGrid1_needDatasource(object sender, GridNeedDataSourceEventArgs e)
            {
                RadGrid1.DataSource = objService.getSoapTemplateQuestions(1, 16);
                
            }
            public void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
            {
                if (e.CommandName == "RowClick")
                {
                    DefineGridStructure(objService.getSoapTemplateQuestions(1, 19));
                     
                }
            }
            public void RadGrid2_ItemCommand(object sender, GridCommandEventArgs e)
            {
                if (e.CommandName == "RowClick")
                {
                    DefineGridStructure(objService.getSoapTemplateQuestions(1, 12494));
                }
            }
           
        }
    }


    Thanks
  6. Konstantin Dikov
    Admin
    Konstantin Dikov avatar
    2466 posts

    Posted 11 Sep 2013 Link to this post

    Hi Sanjay,

    You are trying to add event handlers too late in the page life cycle (ASP.NET Page Life Cycle Overview).

    You could see that if you move "DefineGridStructure" method call from "ItemCommand" event handler to "Page_Init" or "Page_Load" event handlers, the event will fire correctly.

    However, there is a one possible approach that could be used in order for the event to fire correctly with your requirements:
    • Create the new RadGrid on "Page_Load" or "Page_Init" but with set "Visible" property to "false";
    • On "ItemCommand" server-side event change the "Visible" property to "true".

    Here is the modified code:
    private RadGrid _grid;
     
    protected void Page_Load(object sender, EventArgs e)
    {
        DefineGridStructure();
    }
     
    public void DefineGridStructure()
    {
        RadGrid RadGrid1 = new RadGrid();
        RadGrid1.ItemCommand += new GridCommandEventHandler(RadGrid2_ItemCommand);
     
        RadGrid1.ID = "RadGrid2";
        RadGrid1.DataSourceID = "SqlDataSource1";
        RadGrid1.Width = Unit.Percentage(100);
        RadGrid1.PageSize = 15;
        RadGrid1.AllowPaging = true;
        RadGrid1.PagerStyle.Mode = GridPagerMode.NextPrevAndNumeric;
        RadGrid1.AutoGenerateColumns = false;
        RadGrid1.AllowPaging = true;
        RadGrid1.PageSize = 3;
        RadGrid1.ClientSettings.Selecting.AllowRowSelect = true;
        RadGrid1.ClientSettings.EnablePostBackOnRowClick = true;
        //Add Customers table 
        RadGrid1.MasterTableView.Width = Unit.Percentage(100);
        RadGrid1.MasterTableView.DataKeyNames = new string[] { "CustomerID" };
        GridBoundColumn boundColumn = new GridBoundColumn();
        boundColumn.DataField = "CustomerID";
        boundColumn.HeaderText = "Customer ID";
        RadGrid1.MasterTableView.Columns.Add(boundColumn);
        boundColumn = new GridBoundColumn();
        boundColumn.DataField = "ContactName";
        boundColumn.HeaderText = "Contact Name";
        RadGrid1.MasterTableView.Columns.Add(boundColumn);
        // Add the grid to the placeholder 
        this.PlaceHolder1.Controls.Add(RadGrid1);
        RadGrid1.Visible = false;
    }
     
    protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
    {
        if (e.CommandName == "RowClick")
        {
            PlaceHolder1.FindControl("RadGrid2").Visible = true;  
        }
    }

    Hope that helps.
     

    Regards,
    Konstantin Dikov
    Telerik
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
  7. Swati
    Swati avatar
    7 posts
    Member since:
    Jan 2017

    Posted 26 Jul 2017 in reply to Princy Link to this post

    Hi Princy,

     

    How can I access radgrid object 'grid' on itemcommand  i.e. outside of page_init event or any other events for dynamically created radgrid as we have created grid object in page_init event.

    Thank you in advance.

     

Back to Top