Cant access controls in edit form

2 posts, 0 answers
  1. Philip
    Philip avatar
    69 posts
    Member since:
    Mar 2016

    Posted 26 Apr 2016 Link to this post

    Trying to find controls within edit form fails every time

    <telerik:RadAjaxPanel ID="rpNotes" runat="server" LoadingPanelID="RadAjaxLoadingPanel1" >
                                   <telerik:RadGrid ID="rgNotes" runat="server" GroupPanelPosition="Top" OnItemCommand="rgNotes_ItemCommand"  >
                                       <GroupingSettings CollapseAllTooltip="Collapse all groups"></GroupingSettings>
                                       <MasterTableView NoDetailRecordsText="No notes for this Appointment" AutoGenerateColumns="False" DataKeyNames="notes_id" CommandItemDisplay="Top" CommandItemSettings-AddNewRecordText="Add Notes" AllowAutomaticInserts="true" EditMode="PopUp">
                                               <telerik:GridEditCommandColumn UniqueName="EditCommandColumn">
                                               <telerik:GridBoundColumn DataField="notes_id" FilterControlAltText="Filter notes_id column" HeaderText="notes_id" ReadOnly="True" SortExpression="notes_id" Visible="true" UniqueName="notes_id">
                                               <telerik:GridBoundColumn DataField="Subject" FilterControlAltText="Filter Subject column" HeaderText="Subject" ReadOnly="True" SortExpression="Subject" UniqueName="Subject">
                                           <EditFormSettings EditFormType="Template" InsertCaption="Add new Note" CaptionFormatString="Please enter or update note">
                                                   <telerik:RadTextBox ID="txtNotesId" Visible="false" Width="200px" runat="server"></telerik:RadTextBox>
                                                       <telerik:RadTextBox ID="txtSubjectNotes" Width="200px" runat="server"></telerik:RadTextBox>
                                                       Notes<br />
                                                       <telerik:RadTextBox ID="txtMultiNotes" TextMode="MultiLine" Rows="10" Columns="10" Width="200px" runat="server"></telerik:RadTextBox>
                                                   <telerik:RadButton ID="rdSaveNotes" OnClick="rdSaveNotes_Click" Skin="Bootstrap" BackColor="#512479" ForeColor="White" runat="server" Text="Save Notes"></telerik:RadButton>
                                                   <telerik:RadButton ID="rdCancel" OnClick="rdCancel_Click1" CommandName="Cancel" Skin="Bootstrap" BackColor="#512479" ForeColor="White" runat="server" Text="Cancel"></telerik:RadButton>
                                           <ClientEvents OnPopUpShowing="PopUpShowing" />
                                           <Selecting AllowRowSelect="true" />

    I am using this function here which i found on stackoverflow


    /// <summary>
       /// Recursively searches for a server control with the given ID.
       /// </summary>
       /// <param name="id">ID of control to find</param>
       /// <returns>The matching control or null if no match was found</returns>
       public static Control FindControlRecursive(this Control control, string id)
           if (control == null) return null;
           //try to find the control at the current level
           Control ctrl = control.FindControl(id);
           if (ctrl == null)
               //search the children
               foreach (Control child in control.Controls)
                   ctrl = FindControlRecursive(child, id);
                   if (ctrl != null) break;
           return ctrl;

    And this is how i use it

    // TextBox txtSubjectNotes = (TextBox)item.FindControl("txtSubjectNotes");
    // TextBox txtMultiNotes = (TextBox)item.FindControl("txtMultiNotes");
    //add custom logic here
    RadTextBox ctrl = (RadTextBox)this.FindControlRecursive("txtSubjectNotes");
    RadTextBox myControl;
    if (ctrl is RadTextBox)
        myControl = (RadTextBox)this.FindControlRecursive("txtSubjectNotes");
        //  _note.note = txtMultiNotes.Text;
        _note.subject = myControl.Text;

    But I am getting object not found.
  2. Eyup
    Eyup avatar
    3399 posts

    Posted 28 Apr 2016 Link to this post

    Hello Philip,

    In this case there is no need to recursively traverse the control in order to access them. The FindControl method should be enough by itself. Please try accessing them during the ItemDataBound event handler using the approach demonstrated in the following section:

    I hope this will prove helpful.

    Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
Back to Top