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

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

21 Answers 593 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Prab
Top achievements
Rank 1
Prab asked on 28 Sep 2012, 06:23 AM
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
            {
  ..........................
..............
    ........................       
      }
        }
}

21 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 28 Sep 2012, 07:39 AM
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.
0
Prab
Top achievements
Rank 1
answered on 28 Sep 2012, 07:59 AM
Thank you for your reply .but i want to know whether it is collapsed or not
0
Accepted
Eyup
Telerik team
answered on 02 Oct 2012, 03:08 PM
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.
0
Prab
Top achievements
Rank 1
answered on 03 Oct 2012, 05:29 AM
Thank you so much
0
Murat
Top achievements
Rank 1
answered on 29 Mar 2013, 02:52 PM
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.
0
Tim
Top achievements
Rank 1
answered on 06 Jun 2013, 05:33 AM
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?
0
Princy
Top achievements
Rank 2
answered on 06 Jun 2013, 06:55 AM
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
0
Tim
Top achievements
Rank 1
answered on 10 Jun 2013, 06:27 PM
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.
0
Princy
Top achievements
Rank 2
answered on 11 Jun 2013, 07:06 AM
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
0
Jignesh
Top achievements
Rank 1
answered on 12 Nov 2013, 04:35 PM
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.
0
Princy
Top achievements
Rank 2
answered on 13 Nov 2013, 06:14 AM
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
0
Rick
Top achievements
Rank 1
answered on 17 Feb 2014, 11:53 PM
This just produces parentItem.Expanded that is always false. Thanks for trying though.
0
Princy
Top achievements
Rank 2
answered on 18 Feb 2014, 08:36 AM
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
0
Gayathri
Top achievements
Rank 1
answered on 12 Nov 2014, 05:22 AM
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
0
Gayathri
Top achievements
Rank 1
answered on 12 Nov 2014, 05:43 AM
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


0
Gayathri
Top achievements
Rank 1
answered on 12 Nov 2014, 06:16 AM
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,.
0
Eyup
Telerik team
answered on 13 Nov 2014, 01:04 AM
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.

 
0
Gayathri
Top achievements
Rank 1
answered on 13 Nov 2014, 05:16 AM
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
0
Gayathri
Top achievements
Rank 1
answered on 13 Nov 2014, 04:01 PM
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.



0
Kostadin
Telerik team
answered on 17 Nov 2014, 02:37 PM
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.

 
0
Gan
Top achievements
Rank 1
answered on 13 Dec 2016, 09:51 AM
Try HierarchyLoadMode="Conditional" attribute on RadGrid (the parent grid). Hope this helps.
Tags
Grid
Asked by
Prab
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Prab
Top achievements
Rank 1
Eyup
Telerik team
Murat
Top achievements
Rank 1
Tim
Top achievements
Rank 1
Princy
Top achievements
Rank 2
Jignesh
Top achievements
Rank 1
Rick
Top achievements
Rank 1
Gayathri
Top achievements
Rank 1
Kostadin
Telerik team
Gan
Top achievements
Rank 1
Share this question
or