在 Telerik RadGrid 中找不到控件

Cannot find a control within Telerik RadGrid

我试图在 Telerik RadGrid 编辑表单中找到一个控件。我需要能够在页面加载时执行此操作,但是我看到的大多数示例都只是在 itemDataBound 上,但我需要能够在页面加载时设置一个值并在单击按钮时保存一个值。

<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>

例如,我试图在我的保存事件中以代码隐藏的方式访问它。

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());
    }
}

这是因为当控件放置在网格中时,它们没有在设计器文件中声明为页面控件。

您将不得不以不同的方式掌握它们。在单击保存按钮的情况下,您应该能够获取与按钮相关的文本框。

尝试:

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;
}