Always e.Item.Expanded =false in RadGrid ItemCommand Event

21 posts, 1 answers
  1. Prab
    Prab avatar
    15 posts
    Member since:
    Oct 2012

    Posted 28 Sep 2012 Link to this post

    I want to know status of group whether it is expanded or collapsed. For that I am using RadGridInbox_Itemcommand  event .when ever i click expand or collapse icon, i am always getting e.Item.Expanded =false .The following code i used in my project .please give solution to solve this issue.
      public  void RadGridInbox_Itemcommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
          {
              ....................................       
    ..................................
          }
            if (e.CommandName == "ExpandCollapse")
            { 
              if (e.Item.Expanded)
             {            
            ....................................
    .........................................
              }
              else
                {
      ..........................
    ..............
        ........................       
          }
            }
    }
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 28 Sep 2012 Link to this post

    Hi,

    You can check for the status of expanded group as shown below.
    C#:
    protected void RadGrid1_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
     if (e.CommandName == RadGrid.ExpandCollapseCommandName)
     {
     }
    }

    Thanks,
    Shinu.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Prab
    Prab avatar
    15 posts
    Member since:
    Oct 2012

    Posted 28 Sep 2012 Link to this post

    Thank you for your reply .but i want to know whether it is collapsed or not
  5. Answer
    Eyup
    Admin
    Eyup avatar
    3015 posts

    Posted 02 Oct 2012 Link to this post

    Hi Prabhakaran,

    I have created a sample RadGrid web site to test the reported issue. On my side the Expanded property works properly and returns the correct value. Could you please check out the attached application and let me know if I am missing something out to reproduce the issue?

    Regards,
    Eyup
    the Telerik team
    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 their blog feed now.
  6. Prab
    Prab avatar
    15 posts
    Member since:
    Oct 2012

    Posted 03 Oct 2012 Link to this post

    Thank you so much
  7. Murat
    Murat avatar
    1 posts
    Member since:
    Nov 2012

    Posted 29 Mar 2013 Link to this post

    I had a similar problem. There was a detail table that i can expand but never collapse. In my case, i had my radGrid in radDockLayout and radDockLayout's EnableViewState value was false. I just deleted that property (default value for EnableViewState is true) and collapse started working. I hope that helps someone.
  8. Tim
    Tim avatar
    10 posts
    Member since:
    Apr 2012

    Posted 06 Jun 2013 Link to this post

    I am having this exact problem and I've been searching the web and this forum for over a day and have yet to find a solution.  Has anybody figured out why the Expanded property is always returning false when using a NestedViewTemplate?
  9. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 06 Jun 2013 Link to this post

    Hi Tim,
    I have tried an example,and it works fine on my side,please look at it,or if it didn't help,please provide your code.

    ASPX:
    <telerik:RadGrid ID="RadGrid1" DataSourceID="SqlDataSource1" runat="server" AutoGenerateColumns="False"
    AllowSorting="True" AllowPaging="True" PageSize="5" GridLines="None" ShowGroupPanel="True"
    OnItemCommand="RadGrid1_ItemCommand1">
    <MasterTableView DataSourceID="SqlDataSource1" DataKeyNames="CustomerID" AllowMultiColumnSorting="True"
    GroupLoadMode="Server">
    <Columns>
        <telerik:GridBoundColumn DataField="ContactName" HeaderText="Contact name" SortExpression="ContactName"
            UniqueName="ContactName">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="ContactTitle" HeaderText="Contact title" SortExpression="ContactTitle"
            UniqueName="ContactTitle">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="CompanyName" HeaderText="Company name" SortExpression="CompanyName"
            UniqueName="CompanyName">
        </telerik:GridBoundColumn>
    </Columns>
    <NestedViewSettings DataSourceID="SqlDataSource2">
        <ParentTableRelation>
            <telerik:GridRelationFields DetailKeyField="CustomerID" MasterKeyField="CustomerID" />
        </ParentTableRelation>
    </NestedViewSettings>
    <NestedViewTemplate>
        <asp:Panel ID="NestedViewPanel" runat="server" CssClass="viewWrap">
            <div class="contactWrap">
                <fieldset style="padding: 10px;">
                    <legend style="padding: 5px;"><b>Detail info for Customer:   <%#Eval("ContactName") %></b></legend>
                    <table>
                        <tbody>
                            <tr>
                                <td>
                                    <table>
                                        <tbody>
                                            <tr>
                                                <td>
                                                    ContactTitle:
                                                </td>
                                                <td>
                                                    <asp:Label ID="cityLabel" Text='<%#Bind("ContactTitle") %>' runat="server"></asp:Label>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>
                                                    Address:
                                                </td>
                                                <td>
                                                    <asp:Label ID="Label2" Text='<%#Bind("Address") %>' runat="server"></asp:Label>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>
                                                    City:
                                                </td>
                                                <td>
                                                    <asp:Label ID="Label3" Text='<%#Bind("City") %>' runat="server"></asp:Label>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>
                                                    PostalCode:
                                                </td>
                                                <td>
                                                    <asp:Label ID="Label4" Text='<%#Bind("PostalCode") %>' runat="server"></asp:Label>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>
                                                    Country:
                                                </td>
                                                <td>
                                                    <asp:Label ID="Label5" Text='<%#Bind("Country") %>' runat="server"></asp:Label>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>
                                                    Phone:
                                                </td>
                                                <td>
                                                    <asp:Label ID="Label6" Text='<%#Bind("Phone") %>' runat="server"></asp:Label>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>
                                                    Fax:
                                                </td>
                                                <td>
                                                    <asp:Label ID="Label7" Text='<%#Bind("Fax") %>' runat="server"></asp:Label>
                                                </td>
                                            </tr>
                                        </tbody>
                                    </table>
                                </td>
                                <td>
                                    <img src='<%# Page.ResolveUrl("~/Grid/Examples/Hierarchy/NestedViewTemplateDeclarativeRelations/Img/") + Eval("CustomerID")  %>.jpg'
                                        alt="Customer Image" />
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </fieldset>
            </div>
        </asp:Panel>
    </NestedViewTemplate>
    </MasterTableView>
    <PagerStyle Mode="NumericPages"></PagerStyle>
    <ClientSettings AllowDragToGroup="true" />
    </telerik:RadGrid>

    C#:
    protected void RadGrid1_ItemCommand1(object sender, GridCommandEventArgs e)
    {
        {
            if (e.CommandName == RadGrid.ExpandCollapseCommandName)
            {
                if (e.Item.Expanded)
                {
                    Response.Write("<script language='javascript'>alert('Expanded');</script>");
                }
                else
                {
                    Response.Write("<script language='javascript'>alert('Collapsed');</script>");
                }
            }
     
        }
    }

    Thanks,
    Princy
  10. Tim
    Tim avatar
    10 posts
    Member since:
    Apr 2012

    Posted 10 Jun 2013 Link to this post

    I see 2 major things that are different from your example and what I have. First, my NestedViewTemplate is not using a table, it is using another RadGrid to display an unknown number of secondary data that is associated with the data in the original RadGrid.  Second, I am not specifying a DataSource in the aspx file, I am binding the data in the code behind using the NeedDataSource on the Main RadGrid, and then in the ItemCommand of that grid I am calling ReBind on the secondary RadGrid when the CommandName is RadGrid.ExpandCollapseCommandName.  That in turn calls the NeedDataSource of the secondary grid which then binds the data.
  11. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 11 Jun 2013 Link to this post

    Hi,

    I have tried as you have said,and it works fine at my side.Just check the below code.Hope it helps.

    ASPX:
    <telerik:RadGrid ID="RadGrid1"  runat="server" AutoGenerateColumns="False"AllowPaging="True" onitemcommand="RadGrid1_ItemCommand"
                onneeddatasource="RadGrid1_NeedDataSource">
             <MasterTableView  DataKeyNames="CustomerID">
                    <Columns>
                        <telerik:GridBoundColumn DataField="CustomerID" HeaderText="CustomerID" UniqueName="CustomerID">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="ContactTitle" HeaderText="Contact title" UniqueName="ContactTitle">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="CompanyName" HeaderText="Company name" UniqueName="CompanyName">
                        </telerik:GridBoundColumn>
                    </Columns>
                    <NestedViewSettings >
                        <ParentTableRelation>
                            <telerik:GridRelationFields DetailKeyField="CustomerID" MasterKeyField="CustomerID" />
                        </ParentTableRelation>
                    </NestedViewSettings>
                    <NestedViewTemplate>
                        <telerik:RadGrid ID="RadGrid2" runat="server" AutoGenerateColumns="False" PageSize="5"
                            AllowPaging="True" OnNeedDataSource="RadGrid2_NeedDataSource">
                            <MasterTableView>
                                <Columns>
                                    <telerik:GridBoundColumn HeaderText="OrderID" DataField="OrderID" UniqueName="OrderID">
                                    </telerik:GridBoundColumn>
                                    <telerik:GridBoundColumn HeaderText="EmployeeID" DataField="EmployeeID" UniqueName="EmployeeID">
                                    </telerik:GridBoundColumn>
                                </Columns>
                            </MasterTableView>
                        </telerik:RadGrid>
                    </NestedViewTemplate>
           </MasterTableView>
     </telerik:RadGrid>

    C#:
    public DataTable GetDataTable(string query)
       {
           String ConnString = ConfigurationManager.ConnectionStrings["Northwind_newConnectionString3"].ConnectionString;
           SqlConnection conn = new SqlConnection(ConnString);
           SqlDataAdapter adapter = new SqlDataAdapter();
           adapter.SelectCommand = new SqlCommand(query, conn);
     
           DataTable myDataTable = new DataTable();
     
           conn.Open();
           try
           {
               adapter.Fill(myDataTable);
           }
           finally
           {
               conn.Close();
           }
     
           return myDataTable;
       }
     
    protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            RadGrid1.DataSource = GetDataTable("SELECT CustomerID, CompanyName, ContactTitle FROM Customers");
        }
     
    protected void RadGrid2_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
       {
            GridDataItem parentItem = ((sender as RadGrid).NamingContainer as GridNestedViewItem).ParentItem as GridDataItem;
            (sender as RadGrid).DataSource = GetDataTable("SELECT OrderID, EmployeeID, CustomerID FROM Orders where CustomerID='" + parentItem.GetDataKeyValue("CustomerID").ToString() + "' ");
       }
     
    protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
       {
           if (e.CommandName == RadGrid.ExpandCollapseCommandName && !e.Item.Expanded)
           {
               GridDataItem parentItem = e.Item as GridDataItem;
               RadGrid rg = parentItem.ChildItem.FindControl("RadGrid2") as RadGrid;
               rg.Rebind();
            }
       }

    Thanks,
    Princy
  12. Jignesh
    Jignesh avatar
    18 posts
    Member since:
    Dec 2011

    Posted 12 Nov 2013 Link to this post

    In Item_Created Event,I am always getting item.Expanded as false.

    I want to add a usercontrol in NestedViewTemplate dynamically and want to add it only when it is expanded.

    Pl follow my post mentioned below:
    http://www.telerik.com/community/forums/aspnet-ajax/grid/bind-control-when-expanding-nestedviewtemplate.aspx

    Kindly let me know if you guys have come across any solution or whether it is a bug in RadGrid or if I am doing something which I am not suppose to do..........

    Remind you that I need it in Item_Created Event.
  13. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 13 Nov 2013 Link to this post

    Hi Jignesh,

    Set the HierarchyLoadMode="Client" and try the following code snippet in the ItemCreated event of the RadGrid.

    C#:
    protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            GridDataItem parentItem = e.Item as GridDataItem;
            if (parentItem.Expanded)
            {
                // Your code goes here
            }
        }
    }

    Thanks,
    Princy
  14. Rick
    Rick avatar
    4 posts
    Member since:
    Aug 2011

    Posted 17 Feb 2014 in reply to Princy Link to this post

    This just produces parentItem.Expanded that is always false. Thanks for trying though.
  15. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 18 Feb 2014 in reply to Rick Link to this post

    Hi Rick,

    If you want to know a row is expanded you can check it in the PreRender event as follows:

    C#:
    protected void RadGrid1_PreRender(object sender, EventArgs e)
    {
     foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
      {
       if (item.Expanded)
       {
        // Code here
       }
      }
    }

    Thanks,
    Princy
  16. Gayathri
    Gayathri avatar
    25 posts
    Member since:
    Oct 2014

    Posted 11 Nov 2014 in reply to Tim Link to this post

    In my case, neither expand nor collapse is working. i am much tired... please help. i have checked both the expand and collapse conditions in item command and created.

    please help
  17. Gayathri
    Gayathri avatar
    25 posts
    Member since:
    Oct 2014

    Posted 11 Nov 2014 in reply to Princy Link to this post

    Princy,

    Thanks so much. i tried this including in mastertableview HierarchyLoadMode="Client".
    but i got the following error 
    JavaScript runtime error: Unable to get property '_toggleExpand' of undefined or null reference and so had to include the following line 
    <meta http-equiv="X-UA-Compatible" content="IE=8" > between <head></head>

    but i am worried will it raise any error in higher versions of IE?
    and more over i feel like there is a processing icon running when i click the expand button  each time.
    any chance to over come such issues?

    thanks in advance


  18. Gayathri
    Gayathri avatar
    25 posts
    Member since:
    Oct 2014

    Posted 12 Nov 2014 in reply to Gayathri Link to this post

    Princy,

    I am sorry nothing worked... HierarchyLoadMode="Client"  as per my requirement i cannot use this.
    i need to check only in item created if it is expanded or collapsed.
    please help,.
  19. Eyup
    Admin
    Eyup avatar
    3015 posts

    Posted 12 Nov 2014 Link to this post

    Hi Gayathri,

    Please note that the ItemCreated event handler is raised too early in the page life cycle for this logic. Instead, you can use the following approach:
    protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            e.Item.PreRender += (s, a) =>
            {
                if (e.Item.Expanded)
                {
                    // execute custom logic
                }
            };
        }
    }

    Hope this helps.

    Regards,
    Eyup
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  20. Gayathri
    Gayathri avatar
    25 posts
    Member since:
    Oct 2014

    Posted 12 Nov 2014 in reply to Eyup Link to this post

    Eyup,

    Thanks for the reply. applied your logic but still the expand button does not display the container.
    any idea where will be the problem?

    -Thanks
    Gayathri
  21. Gayathri
    Gayathri avatar
    25 posts
    Member since:
    Oct 2014

    Posted 13 Nov 2014 Link to this post

    Eyup,

    I found out that in my code (prewritten code)radgrid1_prerender  there is a validation code logic to set the cell changed values and a rebind happens. so i am getting an error as follows
    httpexception: Control collection cannot be modified in prerender, init ,load.

    it is required that the radgrid1_prerender validation must exist.
    totally blocked with this error from proceeding further. please help.



  22. Kostadin
    Admin
    Kostadin avatar
    1713 posts

    Posted 17 Nov 2014 Link to this post

    Hi Gayathri,

    Could you please provide the grid declaration in order to investigate the cause for this issue further? Keep in mind that if you modify the grid structure you have to use the approach from the following help article.

    Regards,
    Kostadin
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017