我正在尝试找到附加到母版页的网页上的控件。控件本身位于内容页面上,这应该是一件简单的事情,但无论我使用何种方法,我每次都会返回null。
在这两行中它崩溃并说参数不能为空
TextBox txtSubjectNotes =(TextBox)item.FindControl(" txtSubjectNotes");
TextBox txtMultiNotes =(TextBox)item.FindControl(" txtMultiNotes");
我的标记下面是我试图访问的这两个控件
<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>
能够解决这个问题的任何帮助都会很棒,而且这不是特定于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());
}
}
感谢mig下面的评论。我尝试了这个方法,它找到了它,但是它将它作为控制类型转换为一个实习生如何将其转换为文本框,这是我试图找到的控件应该是那么难
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;
}
即如何将此行投射到文本框?
Control ctrl = this.FindControlRecursive("my_control_id");
修改2
好的所以我试过这个,但现在我得到了
{&#34;对象引用未设置为对象的实例。&#34;}
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;
}
答案 0 :(得分:0)
使用递归控制查找控件
Recursive control lookup
你可以做直接演员
Control ctrl = (TextBox)this.FindControlRecursive("my_control_id");
或者您可以在检查控件类型后进行投射。
TextBox myControl;
If(ctrl is TextBox)
myControl = (TextBox)this.FindControlRecursive("my_control_id");