I am trying to find a control that is on a webpage that is attached to a a master page. The control itself is on a content page, this should be a simple thing to do but I am getting back null every time no matter what method I have used.
At these two lines its crashing and saying the parameter cannot be null
TextBox txtSubjectNotes = (TextBox)item.FindControl("txtSubjectNotes");
TextBox txtMultiNotes = (TextBox)item.FindControl("txtMultiNotes");
And my markup below it is these two controls as to which i am trying to access
<telerik:RadTextBox ID="txtSubjectNotes" Width="200px" runat="server"></telerik:RadTextBox>
<telerik:RadTextBox ID="txtMultiNotes" TextMode="MultiLine" Rows="10" Columns="10" Width="200px" runat="server"></telerik:RadTextBox>
Any Help in being able to asolve this would be great and no this is not specific to telerik controls this is standard .net code of being able to find a control on a page.
<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>
protected void rdSaveNotes_Click(object sender, EventArgs e)
{
try
{
int id = Convert.ToInt32(Request.QueryString["id"]);
tblApertureNetNote _note = new tblApertureNetNote();
_note = _dal.GetNotesById(new Guid(notes_id),_myuser.UserId);
_note.appointment_id = id;
_note.authUserId = _myuser.UserId;
_note.isActive = true;
_note.isDeleted = false;
var editFormItems = rgNotes.MasterTableView.GetItems(GridItemType.EditFormItem);
foreach (GridEditFormItem item in editFormItems)
{
if (!item.IsInEditMode)
{
continue;
}
TextBox txtSubjectNotes = (TextBox)item.FindControl("txtSubjectNotes");
TextBox txtMultiNotes = (TextBox)item.FindControl("txtMultiNotes");
//add custom logic here
_note.note = txtMultiNotes.Text;
_note.subject = txtSubjectNotes.Text;
}
if (_note.EntityState == System.Data.EntityState.Detached)
_dal.Addnotes(_note);
rgNotes.DataBind();
}
catch (Exception ex)
{
logger.Error("Error in rdSaveNotes_Click function calandar edit.aspx" + ex.ToString());
}
}
Thanks to comment below by mig. I tried this method and it finds it but it casts it type as control how does one intern cast that to textbox which is what the contorl is im trying to find this should be that hard
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;
}
Ie how do i cast this line to a textbox ?
Control ctrl = this.FindControlRecursive("my_control_id");
Edit 2
Ok SO i have tried this but now i am getting
{"Object reference not set to an instance of an object."}
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;
}