Search code examples
c#teleriktelerik-grid

Cannot find a control within Telerik RadGrid


I am trying to find a control that is within a Telerik RadGrid edit form. I need to be able to do this in page load, however most examples I have seen have been just on itemDataBound, but i need to be able to set a value on page load and save a value on button click.

<telerik:RadGrid ID="rgNotes" runat="server" GroupPanelPosition="Top">
    <GroupingSettings CollapseAllTooltip="Collapse all groups"></GroupingSettings>
    <MasterTableView NoDetailRecordsText="No notes for this Appointment" AutoGenerateColumns="False" CommandItemDisplay="Top" CommandItemSettings-AddNewRecordText="Add Notes" AllowAutomaticInserts="true" EditMode="PopUp">
        <Columns>
            <telerik:GridEditCommandColumn UniqueName="EditCommandColumn">
            </telerik:GridEditCommandColumn>
            <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">
            <FormTemplate>
                Subject
                <p>
                    <telerik:RadTextBox ID="txtSubjectNotes" Width="200px" runat="server"></telerik:RadTextBox>
                </p>
                <p>
                </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>

As an example I am trying to access it in code behind in my save event.

protected void rdSaveNotes_Click(object sender, EventArgs e)
{
    try
    {
        int id = Convert.ToInt32(Request.QueryString["id"]);
        tblApertureNetNote _note = new tblApertureNetNote();
        _note.appointment_id = id;

        _note.isActive = true;
        _note.isDeleted = false;
        _note.subject = txtSubjectNotes.Text; //It's here i can't find the textbox

        _dal.Addnotes(_note);
        rgNotes.DataBind();
    }
    catch (Exception ex)
    {
        logger.Error("Error in rdSaveNotes_Click function calandar edit.aspx" + ex.ToString());
    }
}

Solution

  • This is because when controls are placed in a grid, they are not declared in the designer file as page controls.

    You will have to get hold of them differently. In the case of the save button click, you should be able to get hold of the textbox in relation to the button.

    Try:

    var button = (Control)sender;  // sender is the button
    
    // then ask the button's parent control to find the textbox
    var txtSubjectNotes = button.Parent.FindControl("txtSubjectNotes") as RadTextBox; 
    
    if(txtSubjectNotes != null) 
    {
        // make sure it's not null
        _note.subject = txtSubjectNotes.Text;
    }