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

Cant access controls in edit form

1 Answer 120 Views
AjaxPanel
This is a migrated thread and some comments may be shown as answers.
Philip
Top achievements
Rank 1
Philip asked on 26 Apr 2016, 11:53 AM

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">
                                       <Columns>
                                           <telerik:GridEditCommandColumn UniqueName="EditCommandColumn">
                                           </telerik:GridEditCommandColumn>
                                           <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>
                                           <telerik:GridBoundColumn DataField="Subject" FilterControlAltText="Filter Subject column" HeaderText="Subject" ReadOnly="True" SortExpression="Subject" UniqueName="Subject">
                                           </telerik:GridBoundColumn>
                                       </Columns>
 
                                       <EditFormSettings EditFormType="Template" InsertCaption="Add new Note" CaptionFormatString="Please enter or update note">
                                           <FormTemplate>
 
                                               <telerik:RadTextBox ID="txtNotesId" Visible="false" Width="200px" runat="server"></telerik:RadTextBox>
 
                                               Subject
                                               <p>
                                                   <telerik:RadTextBox ID="txtSubjectNotes" Width="200px" runat="server"></telerik:RadTextBox>
                                               </p>
                                               <p>
                                                   Notes<br />
                                                   <telerik:RadTextBox ID="txtMultiNotes" TextMode="MultiLine" Rows="10" Columns="10" Width="200px" runat="server"></telerik:RadTextBox>
                                               </p>
 
                                               <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>
                                           </FormTemplate>
                                       </EditFormSettings>
                                   </MasterTableView>
                                   <ClientSettings>
                                       <ClientEvents OnPopUpShowing="PopUpShowing" />
                                       <Selecting AllowRowSelect="true" />
                                   </ClientSettings>
                               </telerik:RadGrid>
                           </telerik:RadAjaxPanel>

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.

1 Answer, 1 is accepted

Sort by
0
Eyup
Telerik team
answered on 29 Apr 2016, 06:05 AM
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:
http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/rows/accessing-cells-and-rows#accessing-controls-in-editinsert-mode

I hope this will prove helpful.

Regards,
Eyup
Telerik
Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
Tags
AjaxPanel
Asked by
Philip
Top achievements
Rank 1
Answers by
Eyup
Telerik team
Share this question
or