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

CommandItemTemplate in NestedViewTemplate

4 Answers 212 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Tom
Top achievements
Rank 1
Tom asked on 10 Nov 2009, 03:00 AM
Hello,

I have 3 grids nested inside a main grid using the NestedViewTemplate.  Everything works fine for binding and displaying the data.

The problem is I want to use a CommandItemTemplate in the nested grid to popup another window for inserting new parts.  I can make the
window pop-up, I just can not get the primary key from the top level grid to go with it.

I have tried all kinds of stuff, but I was hoping to do something like the snippet below to include the PlanID as an argument
to my javascript function:

<CommandItemTemplate> 
     <asp:Label ID="lblAddNewPart" Text="Add" runat="server"/>  
                      <href="#" onclick="return ShowAddPartForm('<%# Bind( "PlanID" ) %>');">Add New </a> 
</CommandItemTemplate> 


I am open to C# code behind to set the onclick attributes or javascript to dig around if necessary to get the PlanID from
the parent grid or the nested grid, it exists in both.

I have inserted sample markup below if that helps.

Thanks!

Tom

(Sorry for the blue, I should have typed before using the format code block?)




<
telerik:RadGrid ID="gridJobPlans" runat="server" DataSourceID="objJobPlans"   
    GridLines="None" AllowFilteringByColumn="True" AllowSorting="True"   
            AutoGenerateColumns="False"   
        onupdatecommand="gridJobPlans_UpdateCommand"   
         > 
              
<MasterTableView DataKeyNames="ID"   
        DataSourceID="obj1" CommandItemDisplay="TopAndBottom">  
          
 
     
 
    <NestedViewTemplate> 
    <asp:Label ID="PlanID" runat="server" Text='<%# Eval("ID") %>' /> 
        <telerik:RadTabStrip ID="RadTabStrip1" runat="server" SelectedIndex="1"   
            MultiPageID="MultiPageSelectedJobPlan">  
            <Tabs> 
                <telerik:RadTab runat="server" Text="Documents"   
                    PageViewID="SelectedJobPlanDocs">  
                </telerik:RadTab> 
            </Tabs> 
        </telerik:RadTabStrip> 
        <telerik:RadMultiPage ID="MultiPageSelectedJobPlan" Runat="server"   
            Width="703px" SelectedIndex="2">  
            <telerik:RadPageView ID="Parts" runat="server">  
                <telerik:RadGrid ID="Grid" runat="server"   
                    AllowFilteringByColumn="True" AllowPaging="True" AllowSorting="True"   
                    AutoGenerateDeleteColumn="True" AutoGenerateEditColumn="True"   
                    DataSourceID="objSelected2" GridLines="None" AllowAutomaticDeletes="true">  
                    <MasterTableView AutoGenerateColumns="False" DataKeyNames="ID, PlanID" 
                        CommandItemDisplay="TopAndBottom">  
                        <Columns> 
                            <telerik:GridBoundColumn DataField="ID" DataType="System.Int64" HeaderText="ID"   
                                ReadOnly="True" SortExpression="ID" UniqueName="ID" Display="false">  
                            </telerik:GridBoundColumn> 
                              
                            <telerik:GridBoundColumn DataField="PlanID" DataType="System.Int64"   
                                HeaderText="PlanID" SortExpression="PlanID" UniqueName="PlanID" Display="true">  
                            </telerik:GridBoundColumn> 
                              
                        </Columns> 
                          
                       <CommandItemTemplate> 
                    <asp:Label ID="lblAddNewPart" Text="Add" runat="server"/>  
                      <href="#" onclick="return ShowAddPartForm('<%# Bind( "PlanID" ) %>');">Add New </a> 
                    </CommandItemTemplate>    
                          
                    </MasterTableView> 
                </telerik:RadGrid> 
            </telerik:RadPageView> 
              
        </telerik:RadMultiPage> 
    </NestedViewTemplate> 
 
     
 
</MasterTableView> 
      
</telerik:RadGrid> 
 

4 Answers, 1 is accepted

Sort by
0
Tom
Top achievements
Rank 1
answered on 10 Nov 2009, 03:37 PM
I figured out a solution if anyone comes across a similar situation.  There is probably a more elegant solution, but I have to keep going....

What I did was add the client ID of the label I am using in the nestedviewtemplate as the source parameter for all my nested grids in the code behind for the add new button in the nested grids.

Something like this:

1) Add the onitemcreated to get the CommandItemTemplate when it is created.  This must be added to the grid in the NestedView.

 


<telerik:RadGrid ID="Grid" runat="server"   
AllowFilteringByColumn="True" AllowPaging="True" AllowSorting="True" 
AutoGenerateDeleteColumn="True" AutoGenerateEditColumn="True"   
OnUpdateCommand="GridUpdate" 
DataSourceID="objSelected" GridLines="None" AllowAutomaticDeletes="true" 
onitemcreated="grid_ItemCreated"

2)  Add the code behind to get the client ID of the label in the nested view, you can see the commented lines where I used the IE developer
tools to figure out what the ID comming in was and what it should be.  You can see that I am trying to get to PlanID, which is the label right at the top of the nested view.  That is the label with the parent ID that the nested tables are bound to.

protected void grid_ItemCreated(object sender, GridItemEventArgs e)  
        {  
            if (e.Item is GridCommandItem)  
            {  
                GridCommandItem commandItem = e.Item as GridCommandItem;  
                 
                //replace the owner grid with the label I want
                string lblID = commandItem.OwnerGridID.Replace("SelectedJobPlanPartsGrid", "");  
                lblID += "PlanID";  
                //"ctl00_Content_Page.ascx_gridJobPlans_ctl00_ctl06_Grid"  
                //ctl00_Content_Page.ascx_gridJobPlans_ctl00_ctl06_PlanID  
 
                LinkButton lbl = commandItem.FindControl("lblAddNew") as LinkButton;  
                string temp = "return ShowAddForm('" + lblID + "');";  
                lbl.Attributes.Add("onclick", temp);  
            }  
 
        } 


3)  Add the javascript to get the value.  It is actually in a Span rather than a label when it is finally displayed to the client.

function ShowAddForm(PlanID) {  
        var theDiv = document.getElementById(PlanID);  
                  
         window.radopen("AddNew.aspx?PlanID=" + theDiv.innerHTML, "AddNewDialog");  
         return false;  
     } 


4) I think the only thing I forgot was the CommandItemTemplate that goes inside the nested grid.

<CommandItemTemplate> 
                        <asp:LinkButton ID="lblAddNew" Text="Add New" runat="server"></asp:LinkButton> 
</CommandItemTemplate>  


If anyone wants me to attach a sample solution I can.  Otherwise I would still like to hear other ways to do this.

Tom
0
Phil DeVeau
Top achievements
Rank 1
answered on 08 Dec 2009, 07:39 PM
I've got the exact same scenario; have you found a better way to do this?
0
Tom
Top achievements
Rank 1
answered on 08 Dec 2009, 10:14 PM
Sorry, I can't be of more help on this one.  I decided I did not like how it was done and started over with an inline insert.

I added the Parent Primary key as a hidden (display=false) bound column in the columns collection of the nested grid and enabled automatic inserts. 

Tom
0
eamon
Top achievements
Rank 1
answered on 09 Apr 2012, 09:16 PM
just came across similar issue 3 years on : )

used above method (so thanks for that!!!)

just changed the part where we're trying to derive the client IDs in favor of just looking to the parent ...

e.g
protected void grid_ItemCreated(object sender, GridItemEventArgs e)  
        { 
            if (e.Item is GridCommandItem)  
            {  
                GridCommandItem commandItem = e.Item as GridCommandItem;  
                 
RadGrid rg = (RadGrid)sender;
                RadPageView pv = (RadPageView)rg.Parent;

                Label lblcid = (Label)pv.FindControl("PlanID");
                Int32 intPlanID= Int32.Parse(lblcid.Text);
 
                LinkButton lbl = commandItem.FindControl("lblAddNew") as LinkButton;  
                string temp = "return ShowAddForm('" + intPlanID + "');";  
                lbl.Attributes.Add("onclick", temp);  
            }  
 
        } 
Tags
Grid
Asked by
Tom
Top achievements
Rank 1
Answers by
Tom
Top achievements
Rank 1
Phil DeVeau
Top achievements
Rank 1
eamon
Top achievements
Rank 1
Share this question
or