简体   繁体   English

无法在页面母版页上找到控件

[英]Not Being able to find control on page master page

I am trying to find a control that is on a webpage that is attached to aa 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. 控件本身位于内容页面上,这应该是一件简单的事情,但无论我使用何种方法,每次都会返回null。

At these two lines its crashing and saying the parameter cannot be null 在这两行中它崩溃并且说参数不能为空

TextBox txtSubjectNotes = (TextBox)item.FindControl("txtSubjectNotes"); TextBox txtSubjectNotes =(TextBox)item.FindControl(“txtSubjectNotes”);

TextBox txtMultiNotes = (TextBox)item.FindControl("txtMultiNotes"); 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控件的,这是能够在页面上找到控件的标准.net代码。

 <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. 感谢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 编辑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;
                }

Use recursive control look up to find control Recursive control lookup Either You can do direct cast. 使用递归控件查找控件递归控件查找要么你可以做直接转换。
Control ctrl = (TextBox)this.FindControlRecursive("my_control_id");

or you can cast after checking the control type. 或者您可以在检查控件类型后进行投射。

TextBox myControl;
If(ctrl is TextBox)
  myControl = (TextBox)this.FindControlRecursive("my_control_id");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM