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

[Solved] Trouble finding control RadGrid2 within EditFormSettings of RadGrid2

2 Answers 137 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jon-Jon Kershaw
Top achievements
Rank 2
Jon-Jon Kershaw asked on 07 Aug 2009, 10:40 PM
 protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)  
    {  
 
        if (e.CommandName == RadGrid.PerformInsertCommandName)  
        {  
        }  
        else if (e.CommandName == RadGrid.UpdateCommandName)  
        {  
        }  
        else if (e.CommandName == RadGrid.DeleteCommandName)  
        {  
        }  
        else if (e.CommandName == RadGrid.EditCommandName)  
             
        {  
           
                GridEditCommandColumn editColumn = (GridEditCommandColumn)RadGrid1.MasterTableView.GetColumn("EditCommandColumn");  
                GridEditableItem item = (GridEditableItem)e.Item;  
                RadGrid Roles = (RadGrid)FindControlRecursive(this, "RadGrid2");  
                DataTable dtTable2 = new DataTable();  
                string conn2 = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();  
                SqlDataAdapter sqladp2 = new SqlDataAdapter();  
                SqlConnection sqlconn2 = new SqlConnection(conn2);  
                sqlconn2.Open();  
                try  
                {  
                    string selectQuery2 = "SELECT [RoleID], [Rolename] FROM [vw_UsersinRoles] WHERE [UserID] = " + e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["UserID"].ToString() + " ORDER BY [Username]";  
                    sqladp2.SelectCommand = new SqlCommand(selectQuery2, sqlconn2);  
                    sqladp2.Fill(dtTable2);  
                    Roles.DataSource = dtTable2;  
                    Roles.DataBind();  
                      
                }  
                finally  
                {  
                    sqlconn2.Close();  
                }  
        }   
    }  
 
    public Control FindControlRecursive(Control root, string id)  
    {  
        if (root.ID == id)  
        {  
            return root;  
        }  
 
        foreach (Control c in root.Controls)  
        {  
            Control t = FindControlRecursive(c, id);  
            if (t != null)  
            {  
                return t;  
            }  
        }  
 
        return null;  
    } 
   <form id="form1" runat="server">  
    <telerik:RadGrid ID="RadGrid1" runat="server" AllowFilteringByColumn="True" AllowPaging="True" 
        AllowSorting="True" GridLines="None" Width="623px" AutoGenerateColumns="false" 
        AllowAutomaticDeletes="True" AllowAutomaticInserts="True" AllowAutomaticUpdates="True" 
        GroupingSettings-CaseSensitive="false" PageSize="20" EnableLinqExpressions="False" 
        ShowStatusBar="True" OnPageIndexChanged="RadGrid1_PageIndexChanged" OnItemCommand="RadGrid1_ItemCommand" 
        OnSelectedIndexChanged="RadGrid1_SelectedIndexChanged">  
        <MasterTableView ShowFooter="False" CommandItemSettings-AddNewRecordText="Add New Contact" 
            CommandItemDisplay="TopAndBottom" DataKeyNames="UserID">  
            <CommandItemSettings AddNewRecordText="Add New Form"></CommandItemSettings> 
            <RowIndicatorColumn> 
                <HeaderStyle Width="20px"></HeaderStyle> 
            </RowIndicatorColumn> 
            <ExpandCollapseColumn> 
                <HeaderStyle Width="20px"></HeaderStyle> 
            </ExpandCollapseColumn> 
            <Columns> 
                <telerik:GridEditCommandColumn HeaderStyle-Width="15px" EditText="View / Edit">  
                    <HeaderStyle Width="15px"></HeaderStyle> 
                </telerik:GridEditCommandColumn> 
                <telerik:GridTemplateColumn UniqueName="DeleteColumn" ItemStyle-Width="16px" AllowFiltering="false">  
                    <HeaderStyle Width="16px" /> 
                    <ItemTemplate> 
                        <asp:ImageButton ID="imgbtnDelete" Width="14px" ToolTip="Delete Contact" runat="server" 
                            CommandName="Delete" ImageUrl="~/images/recycle.jpg" OnClientClick="javascript:if(!confirm('This action will delete the selected contact. Are you sure?')){return false;}" /> 
                    </ItemTemplate> 
                    <ItemStyle Width="16px"></ItemStyle> 
                </telerik:GridTemplateColumn> 
                <telerik:GridBoundColumn DataField="Username" HeaderText="Username" SortExpression="Username" 
                    UniqueName="Username">  
                </telerik:GridBoundColumn> 
            </Columns> 
            <EditFormSettings CaptionFormatString="Edit details for User {0}" EditFormType="Template">  
                <EditColumn UniqueName="EditCommandColumn1">  
                </EditColumn> 
                <FormTemplate> 
                    <table> 
                        <tr> 
                            <td> 
                                <telerik:RadGrid ID="RadGrid2" runat="server" OnItemDataBound="RadGrid2_ItemDataBound" 
                                    AllowAutomaticDeletes="True" AllowAutomaticInserts="True" AutoGenerateColumns="False" 
                                    GridLines="None">  
                                </telerik:RadGrid> 
                            </td> 
                        </tr> 
                        <tr> 
                            <td> 
                                <asp:Button ID="btnUpdate" Text='<%# (Container is GridEditFormInsertItem) ? "Insert" : "Update" %>' 
                                    runat="server" CommandName='<%# (Container is GridEditFormInsertItem) ? "PerformInsert" : "Update" %>'>  
                                </asp:Button>&nbsp;<asp:Button CausesValidation="false" ID="btnEdit" Text="EditContact" 
                                    runat="server" CommandName="EditContact"></asp:Button> 
                                <asp:Button ID="btnCancel" Text="Cancel" runat="server" CausesValidation="False" 
                                    CommandName="Cancel"></asp:Button> 
                            </td> 
                        </tr> 
                        <table> 
                </FormTemplate> 
            </EditFormSettings> 
            <NoRecordsTemplate> 
                No Records</NoRecordsTemplate> 
        </MasterTableView> 
        <GroupingSettings CaseSensitive="False"></GroupingSettings> 
    </telerik:RadGrid> 
    <asp:ScriptManager ID="ScriptManager1" runat="server">  
    </asp:ScriptManager> 
    </form> 

2 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 10 Aug 2009, 08:04 AM
Hi,

The Edit Command is actually too early an event to access the EditForm controls, since the edit form will not be rendered then. Instead you can try accessing the controls in the ItemDataBound event of the grid as shown:
c#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
    { 
        if (e.Item is GridEditFormItem && e.Item.IsInEditMode) 
        { 
            GridEditFormItem item = (GridEditFormItem)e.Item; 
            RadGrid grid = (RadGrid)item.FindControl("RadGrid2"); 
 
        } 
    } 

Thanks
Princy.
0
Jon-Jon Kershaw
Top achievements
Rank 2
answered on 11 Aug 2009, 06:24 PM
awesome thanks.
Tags
Grid
Asked by
Jon-Jon Kershaw
Top achievements
Rank 2
Answers by
Princy
Top achievements
Rank 2
Jon-Jon Kershaw
Top achievements
Rank 2
Share this question
or