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

[Solved] Execute same method for ExpandCollapse or RowClick?

8 Answers 548 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Software
Top achievements
Rank 1
Software asked on 03 Jul 2008, 05:05 PM
I'm having a couple problems with NestedViewTemplates in RadGrid.  I have enabled PostbackOnRowClick for the grid and assigned an OnItemCommand to the RadGrid so that I can execute code when the row is expanded.  The problem is, the command seems to execute twice when the user clicks on the ExpandCollapse button.  Once for "ExpandCollapse" command and once for "RowClick".  What I'd like is for either an ExpandCollapse OR a RowClick to execute the ItemCommand method, and then stop, rather than executing it again.  Is there a way to do this? 

Also, how do I capture the "Expand" event on the client side?  And is it possible to hide the ExpandCollapse column in a NetsedViewTemplate?  As you can see from my code, I've tried just about everything to hide it but nothing seems to work...

Here's a code sample of what I'm doing:
<telerik:RadGrid runat="server" ID="radGridWts" GridLines="Horizontal"      
    AutoGenerateColumns="false" EnableAjaxSkinRendering="True" Skin="Office2007"      
    ClientSettings-EnableRowHoverStyle="true" OnItemCommand="radGridWts_ItemCommand"    
    OnItemDataBound="radGridWts_ItemDataBound" ShowDesignTimeSmartTagMessage="true">     
    <ClientSettings EnablePostBackOnRowClick="True" AllowExpandCollapse="false">     
        <Resizing AllowColumnResize="true" ResizeGridOnColumnResize="false" EnableRealTimeResize="true" />    
    </ClientSettings>    
    <MasterTableView DataKeyNames="WtID,WtRid">  
        <ExpandCollapseColumn Display="false" Visible="false">     
            <HeaderStyle Width="0px" />    
            <ItemStyle Width="0px" />    
        </ExpandCollapseColumn>    
        <RowIndicatorColumn Visible="False">     
            <HeaderStyle Width="20px" />    
        </RowIndicatorColumn>    
        <Columns> 
            <telerik:GridBoundColumn HeaderText="WtID" Visible="False" HeaderStyle-HorizontalAlign="Left" DataField="WtID" UniqueName="colWtId"></telerik:GridBoundColumn>    
            <telerik:GridBoundColumn HeaderText="Sev" HeaderStyle-HorizontalAlign="Left" DataField="Severity"></telerik:GridBoundColumn>    
            <telerik:GridBoundColumn HeaderText="RID" DataField="WtRid"></telerik:GridBoundColumn>    
            <telerik:GridBoundColumn HeaderText="Wt Number" DataField="WtNumber" ></telerik:GridBoundColumn>    
            <telerik:GridBoundColumn HeaderText="Date" DataField="DateIssued" DataFormatString="{0:d}"></telerik:GridBoundColumn>    
            <telerik:GridBoundColumn HeaderText="Type" DataField="WtType"></telerik:GridBoundColumn>    
            <telerik:GridBoundColumn HeaderText="Section" DataField="WtCode"></telerik:GridBoundColumn>    
            <telerik:GridBoundColumn HeaderText="Bail Amount" DataField="BailAmount" DataFormatString="{0:C}" UniqueName="colBailAmt"></telerik:GridBoundColumn>    
        </Columns>    
        <NestedViewTemplate>    
            <div style="padding:5px;">     
                <div id="buttons">     
                    <style="float:right; margin-left:-155px; margin-top:5px; position:absolute"      
                        href="" tabindex="32" onmouseup="javascript:window.focus()"      
                        onclick="javascript:CopyWtDetails('<%= strCopyWtDetails %>');return false;" class="copy"></a>    
                </div>    
                <asp:Label runat="server" ID="lblWtDetail"></asp:Label>    
                <div style="border-top: 1px solid #ccc; padding-top: 5px;">     
                    <telerik:RadPanelBar runat="server" ID="rPanelBarDdVisits" Width="100%" EnableEmbeddedSkins="false"    
                        Skin="Hay" ExpandAnimation-Type="InBounce" OnClientItemClicking="OnClientItemClicking">     
                        <Items>    
                            <telerik:RadPanelItem runat="server" Text="Due Diligence" Expanded="false" ExpandedImageUrl="../images/submenu_dn.gif"    
                                ImageUrl="../images/submenu.gif">     
                                <Items>    
                                    <telerik:RadPanelItem runat="server">     
                                        <ItemTemplate>    
                                            <div style="margin: 3px 0px 1px 0px;">     
                                                <telerik:RadGrid runat="server" ID="rGridDdVisits" Skin="Office2007" AutoGenerateColumns="false">  
                                                    <MasterTableView>    
                                                        <Columns>    
                                                            <telerik:GridBoundColumn HeaderText="DATE" DataField="CommentDate" DataFormatString="{0:d}">  
                                                            </telerik:GridBoundColumn>    
                                                            <telerik:GridBoundColumn HeaderText="DESCRIPTION" DataField="Comment">  
                                                            </telerik:GridBoundColumn>    
                                                            <telerik:GridBoundColumn HeaderText="LEA" DataField="Lea">  
                                                            </telerik:GridBoundColumn>    
                                                            <telerik:GridBoundColumn HeaderText="OFFICER" DataField="Officer">  
                                                            </telerik:GridBoundColumn>    
                                                        </Columns>    
                                                    </MasterTableView>    
                                                </telerik:RadGrid>    
                                            </div>    
                                        </ItemTemplate>    
                                    </telerik:RadPanelItem>    
                                </Items>    
                            </telerik:RadPanelItem>    
                        </Items>    
                    </telerik:RadPanelBar>    
                </div>    
            </div>                                             
        </NestedViewTemplate>    
    </MasterTableView>    
</telerik:RadGrid>   
 

protected void radGridWts_ItemCommand(object sender, GridCommandEventArgs e)  
{  
    if (e.Item is GridDataItem)  
    {                  
        string strWtID = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["WtID"].ToString();  
        string strRid = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["WtRid"].ToString();  
 
        if (e.CommandName == "RowClick" || e.CommandName == "ExpandCollapse")  
            if (!e.Item.Expanded)  
            {  
                e.Item.Expanded = true;  
                e.Item.Selected = true;  
 
                Wt w = Wt.getWtDetails(strWtID, strRid);  
                if (w != null)  
                {  
                    strCopyWtDetails = BuildCopyWtDetails(w);  
                    Label lblWtDetail;  
                    GridNestedViewItem nviWtDetails =  
                        ((GridTableView)e.Item.Parent.Parent).Items[e.Item.ItemIndex].ChildItem;  
                    lblWtDetail = nviWtDetails.FindControl("lblWtDetail"as Label;  
                    RadPanelBar rpb = nviWtDetails.FindControl("rPanelBarDueDiligence"as RadPanelBar;  
 
                    if (w.DdVisits != null && w.DdVisits.Count > 0)  
                    {  
                        if (rpb != null)  
                            if (rpb.Items[0].Text.IndexOf("(") < 0)  
                                rpb.Items[0].Text += " (" + w.DdVisits.Count + ")";  
 
                        RadGrid rGridDdVisits =  
                            nviWtDetails.FindControl("rPanelBarDueDiligence").Controls[0].Controls[0].  
                                FindControl("rGridDueDiligence"as RadGrid;  
                        if (rGridDdVisits != null)  
                        {  
                            rGridDdVisits.DataSource = w.DdVisits;  
                            rGridDdVisits.DataBind();  
                        }  
                    }  
                    else if (w.DdVisits == null || w.DdVisits.Count < 1) if (rpb != null) rpb.Visible = false;  
 
                    if (lblWtDetail != null)  
                        lblWtDetail.Text = WriteWtDetails(w).ToString();  
                }  
            }  
            else 
                e.Item.Expanded = false;  
    }  

Thanks for the help!
Eddie

8 Answers, 1 is accepted

Sort by
0
Veli
Telerik team
answered on 07 Jul 2008, 10:58 AM
Hi Eddie,

After setting HierarchyLoadMode="Client" for RadGrid's MasterTableView, RadGrid should not fire the ExpandCollapse event on the server any more. Thus, you can have Expand/Collapse on the server only on row click. Please see if you can use hierarchy loading on the client.

As for capturing events during expand/collapse on the client, RadGrid provides the following client events:

OnHierarchyExpanding
OnHierarchyExpanded
OnHierarchyCollapsing
OnHierarchyCollapsed

Sincerely yours,
Veli
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Chirag Dedhia
Top achievements
Rank 1
answered on 23 Jan 2010, 03:52 PM

Hi,

I am also facing the same problem. The itemcommand event fires twice when i expand the row, once for ExpandCollapse & then immediately again for Rowclick, because of which the detail table does not expands. After setting HierarchyLoadMode="Client" the detail table does expands, but then it fires the code written on Rowclick, which i do not want to be fired.

Do you have a solution for this?

 

Thanks & Regards

 

Chirag Dedhia

0
Veli
Telerik team
answered on 25 Jan 2010, 09:07 AM
Hello,

Let me clarify something that might be of importance. RadGrid's ItemCommand event fires for all item commands, even though some of these commands may have their own dedicated events. For example, RadGrid's ItemCommand will fire for Update/Insert/Expand/Collapse/RowClick, many other built-in RadGrid commands, as well as all custom commands you define. Commands that have their own dedicated events like Update/Insert/Delete will then fire too.

With this in mind, if you need to execute command specific logic in ItemCommand, you need to check the command name from the event argument. Otherwise, the logic in the event handler will be executed as many times as ItemCommand is fired. And RadGrid may fired more than one command on a postback. So you would need:

protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
    if (e.CommandName == "RowClick")
    {
        //execute logic for RowClick
    }
 
    if (e.CommandName == RadGrid.ExpandCollapseCommandName)
    {
        //execute logic for ExpandCollapse command
    }
 
    //...
}

In this way, you would have separated different command logic in the same event handler. For more info on event RadGrid commands:

Command reference (Event bubbling in RadGrid)

All the best,
Veli
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Chirag Dedhia
Top achievements
Rank 1
answered on 25 Jan 2010, 09:48 AM
Hi Veli,

I am doing exactly the way you mentioned i.e. checking the command name from the event argument. My code goes as follows

'Format Code Block'

if

 

(e.CommandName == "RowClick")

 

{

// Do Something

}

 

 

if (e.CommandName == "ExpandCollapse")

 

{

// Do Something
}

But the problem i am facing is when i click on ExpandCollapse button of Hierarchial radgrid, it enters the code block of ExpandCollapse, but immediately after that it enters the RowClick code block & executes the code there because of which my Hierarchial grid does not expands. Please explain how can i overcome this.

Thanks & Regards
Chirag Dedhia

0
Princy
Top achievements
Rank 2
answered on 25 Jan 2010, 11:09 AM
Hi,

You can cehck for the CommandSource in the RowClick event and wither cancel the event a\or return from the event as shown below:

C#
 if (e.CommandName == "RowClick"
        { 
            if (((Control)(e.CommandSource)).ID == "GECBtnExpandColumn"
            { 
                e.Canceled = true
            } 
            else 
            { 
                Response.Write("Hello"); 
            } 
        } 


Thanks,
Princy
0
Chirag Dedhia
Top achievements
Rank 1
answered on 25 Jan 2010, 11:34 AM
Hi Princy,

Thanks for reply, but that either didnt solved my problem. Let me explain you my complete scenario.

I have a Hierarchial Grid defined as below
ASPX Code:

    <telerik:RadGrid ID="rgAccount" runat="server" AutoGenerateColumns="false" height="200 px" 
        OnNeedDataSource="rgAccount_NeedDataSource"  OnDetailTableDataBind="rgAccount_DetailTableDataBind" 
        OnItemCommand="rgAccount_ItemCommand" 
        AllowFilteringByColumn="True" AllowSorting="True" AllowPaging="True" PageSize="10" GridLines="None">  
 
        <ClientSettings EnableRowHoverStyle="true" EnablePostBackOnRowClick="true">  
            <Scrolling AllowScroll="True" UseStaticHeaders="True" /> 
        </ClientSettings>               
 
        <MasterTableView DataKeyNames="UPIN">  
            <DetailTables> 
                <telerik:GridTableView EnableNoRecordsTemplate="true" > 
                        <Columns> 
                            <telerik:GridBoundColumn DataField="LongName" HeaderText="Funding Stream" FilterControlWidth="200px" CurrentFilterFunction="Contains" AutoPostBackOnFilter="true" /> 
                        </Columns> 
                        <NoRecordsTemplate> 
                        <asp:Label ID="lblNoRecords" runat="server" Text="No associated Funding Streams"></asp:Label> 
                        </NoRecordsTemplate> 
                </telerik:GridTableView> 
            </DetailTables> 
          
            <Columns> 
                <telerik:GridBoundColumn DataField="AccountId" HeaderText="Account ID" HeaderStyle-Width="75px" FilterControlWidth="50px" CurrentFilterFunction="Contains" AutoPostBackOnFilter="true" /> 
                <telerik:GridBoundColumn DataField="UPIN" HeaderText="UPIN" HeaderStyle-Width="75px" FilterControlWidth="50px" CurrentFilterFunction="Contains" AutoPostBackOnFilter="true" /> 
                <telerik:GridBoundColumn DataField="UKPRN" HeaderText="UKPRN" HeaderStyle-Width="75px" FilterControlWidth="50px" CurrentFilterFunction="Contains" AutoPostBackOnFilter="true" /> 
                <telerik:GridBoundColumn DataField="ProviderName" HeaderText="Name" HeaderStyle-Width="230px" FilterControlWidth="200px" CurrentFilterFunction="Contains" AutoPostBackOnFilter="true"/>   
                <telerik:GridBoundColumn DataField="ApprovalStatus" HeaderText="Approval" HeaderStyle-Width="75px" FilterControlWidth="50px" CurrentFilterFunction="Contains" AutoPostBackOnFilter="true"/>   
                <telerik:GridBoundColumn DataField="ManagedBy" HeaderText="Managed By" HeaderStyle-Width="75px" FilterControlWidth="50px" CurrentFilterFunction="Contains" AutoPostBackOnFilter="true"/>   
                <telerik:GridBoundColumn DataField="Status" HeaderText="Status" HeaderStyle-Width="75px" FilterControlWidth="50px" CurrentFilterFunction="Contains" AutoPostBackOnFilter="true"/>   
            </Columns> 
        </MasterTableView> 
    </telerik:RadGrid>     


Code Behind:

 

 

 

protected void rgAccount_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)  
 
{  
//Some code to bind Grid  
}  
 
protected void rgAccount_DetailTableDataBind(object source, GridDetailTableDataBindEventArgs e)  
 
{  
// Code to fill Detail table  
}  
 
 
protected void rgAccount_ItemCommand(object source, GridCommandEventArgs e)  
 
{  
if (e.CommandName == "RowClick")  
{  
// Some code  

Now as soon as i click on Expand Column following events fire
1.  rgAccount_ItemCommand - e.CommandName == "ExpandCollapse"
2. rgAccount_NeedDataSource
3. rgAccount_DetailTableDataBind
4. rgAccount_ItemCommand - e.CommandName == "RowClick"

Now because the 4th event fires my Hierarchial grid does not expands. I hope this explains my problem.

Thanks & Regards
Chirag Dedhia

 

0
Sabaratnam
Top achievements
Rank 1
answered on 08 Feb 2013, 11:46 PM
We are looking for exactly the same solution.  Any one out there ..Please help. 
0
Antonio Stoilkov
Telerik team
answered on 13 Feb 2013, 03:17 PM
Hello,

I have tried to replicate the issue but to no avail. Attached to this message you could find the project I used for testing. Additionally, I have created a video showing the behavior on my side and that only ExpandCollapse command is called. In order to further investigate the issue could open a formal ticket attaching the modified project showing the unwanted behavior so we could debug it and advise you with the best possible solution.

All the best,
Antonio Stoilkov
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.
Tags
Grid
Asked by
Software
Top achievements
Rank 1
Answers by
Veli
Telerik team
Chirag Dedhia
Top achievements
Rank 1
Princy
Top achievements
Rank 2
Sabaratnam
Top achievements
Rank 1
Antonio Stoilkov
Telerik team
Share this question
or