RadEditor 的 FindControl return null
FindControl return null for RadEditor
我下面有下面的 RadGrid。
我在编辑时尝试访问 RadEditor,并通过以下方式将可见设置为 false,但收到错误消息:
protected void ItemCommand(object sender, GridCommandEventArgs e)
{
if (e.CommandName == "Edit")
{
var gridItem = e.Item as GridEditableItem;
if ("ActionItems".Equals(e.Item.OwnerTableView.Name))
{
GridEditableItem editItem = e.Item as GridEditableItem;
RadEditor radEditor = editItem.FindControl("LimitEdit") as RadEditor;
radEditor.Visible = false; // get error message saying radEditor is null.
}
}
}
<telerik:RadGrid HeaderStyle-ForeColor="White" ID="SupportItemGrid" runat="server" CellSpacing="0" DataSourceID="SupportItemSource"
GridLines="None" OnInsertCommand="ItemInsert" OnItemCommand="ItemCommand" Skin="Metro" OnDeleteCommand="RadGrid1_DeleteCommand"
OnDataBound="SupportItemGrid_DataBound" OnItemDataBound="ItemDatabound" AllowSorting="true" AllowAutomaticUpdates="true">
<ValidationSettings EnableValidation="True" CommandsToValidate="Insert"></ValidationSettings>
<MasterTableView AutoGenerateColumns="False" DataKeyNames="CompanyId,ItemId" DataSourceID="ActionItemSource"
AllowAutomaticInserts="True" AllowAutomaticUpdates="True" CommandItemDisplay="Top" AllowSorting="true"
Name="ActionItems" AllowPaging="True" PageSize="20" ExpandCollapseColumn-HeaderText="Comments"
ExpandCollapseColumn-UniqueName="Comments" HierarchyLoadMode="Client">
<HeaderStyle HorizontalAlign="Center" ForeColor="White"></HeaderStyle>
<CommandItemSettings AddNewRecordText="Add New Support Item" ShowExportToExcelButton="true"></CommandItemSettings>
<EditFormSettings>
<EditColumn FilterControlAltText="Filter EditCommandColumn column">
</EditColumn>
</EditFormSettings>
<DetailTables>
<telerik:GridTableView runat="server" AllowAutomaticInserts="True" DataKeyNames="CommentId"
Name="Comments" DataSourceID="CommentSource" CommandItemDisplay="Top" AutoGenerateColumns="False"
NoDetailRecordsText="No Comments To Display">
<ParentTableRelation>
<telerik:GridRelationFields DetailKeyField="CompanyId" MasterKeyField="CompanyId" />
<telerik:GridRelationFields DetailKeyField="ItemId" MasterKeyField="ItemId" />
</ParentTableRelation>
<CommandItemSettings ExportToPdfText="Export to PDF" AddNewRecordText="Add New Comment" />
<RowIndicatorColumn FilterControlAltText="Filter RowIndicator column" Visible="True">
<HeaderStyle Width="20px" />
</RowIndicatorColumn>
<ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column" Visible="True">
<HeaderStyle Width="20px" />
</ExpandCollapseColumn>
<EditFormSettings>
<EditColumn FilterControlAltText="Filter EditCommandColumn column">
</EditColumn>
</EditFormSettings>
<Columns>
<telerik:GridTemplateColumn DataField="Comment" FilterControlAltText="Filter Comment column"
HeaderText="Comment" SortExpression="Comment" UniqueName="Comment">
<EditItemTemplate>
<asp:TextBox ID="DescriptionTextBox" runat="server" TextMode="MultiLine" Height="100px"
Width="400" Text='<%# Bind("Comment") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="CommentLabel" runat="server" Text='<%# Eval("Comment") %>'></asp:Label>
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridBoundColumn DataField="UserName" DataType="System.String" FilterControlAltText="Filter UserName column"
HeaderText="User Name" SortExpression="UserName" UniqueName="UserName" ReadOnly="True">
<HeaderStyle Width="150"></HeaderStyle>
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="DateEntered" DataType="System.DateTime" FilterControlAltText="Filter DateEntered column"
HeaderText="Date Entered" SortExpression="DateEntered" UniqueName="DateEntered"
ReadOnly="True">
<HeaderStyle Width="150"></HeaderStyle>
</telerik:GridBoundColumn>
<telerik:GridTemplateColumn UniqueName="DeleteDetail" Visible="false">
<ItemTemplate>
<telerik:RadButton ID="RemoveButton" runat="server" CommandName="Delete" Text="Delete" Width="75px"></telerik:RadButton>
</ItemTemplate>
<HeaderStyle Width="75"></HeaderStyle>
</telerik:GridTemplateColumn>
</Columns>
</telerik:GridTableView>
</DetailTables>
<CommandItemSettings ExportToPdfText="Export to PDF"></CommandItemSettings>
<RowIndicatorColumn Visible="True" FilterControlAltText="Filter RowIndicator column">
<HeaderStyle Width="20px"></HeaderStyle>
</RowIndicatorColumn>
<ExpandCollapseColumn Visible="True" FilterControlAltText="Filter ExpandColumn column">
<HeaderStyle Width="20px"></HeaderStyle>
</ExpandCollapseColumn>
<Columns>
<telerik:GridDropDownColumn DataSourceID="SitesDS" ListTextField="SiteName"
ListValueField="SiteId" UniqueName="SiteId" SortExpression="SiteId"
HeaderText="Site" DataField="SiteId" DropDownControlType="DropDownList" AllowVirtualScrolling="true"
ShowMoreResultsBox="true" ItemsPerRequest="10">
</telerik:GridDropDownColumn>
<telerik:GridBoundColumn DataField="DateEntered" FilterControlAltText="Filter DateEntered column"
HeaderText="Date Entered" SortExpression="DateEntered" UniqueName="DateEntered"
DataType="System.DateTime">
<HeaderStyle Width="85"></HeaderStyle>
</telerik:GridBoundColumn>
<telerik:GridDropDownColumn DataSourceID="ItemTypeSource" ListTextField="ItemType"
ListValueField="ItemTypeId" UniqueName="ItemTypeId" SortExpression="ItemTypeId"
HeaderText="Type" DataField="ItemTypeId" DropDownControlType="DropDownList" AllowVirtualScrolling="true"
ShowMoreResultsBox="true" ItemsPerRequest="10">
<HeaderStyle Width="120"></HeaderStyle>
</telerik:GridDropDownColumn>
<telerik:GridTemplateColumn DataField="Description" FilterControlAltText="Filter Description column"
HeaderText="Description" SortExpression="Description" UniqueName="Description">
<EditItemTemplate>
<asp:TextBox ID="txtDescription" UniqueName="txtDescription" runat="server" Text='<%# Bind("Description") %>'></asp:TextBox>
<telerik:RadEditor StripFormattingOnPaste="MSWord" ID="LimitEdit" runat="server" Content='<%# Bind("Description") %>'
Skin="Windows7">
</telerik:RadEditor>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="DescriptionLabel" runat="server" Text='<%# Eval("Description") %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Left" />
</telerik:GridTemplateColumn>
<telerik:GridButtonColumn ConfirmText="Delete?" ConfirmDialogType="RadWindow" HeaderText="Delete" SortExpression="DeleteMain" UniqueName="DeleteMain"
Display="false" ConfirmTitle="Delete" ButtonType="ImageButton" CommandName="Delete" />
</Columns>
<EditFormSettings>
<EditColumn FilterControlAltText="Filter EditCommandColumn column">
</EditColumn>
</EditFormSettings>
<EditItemStyle HorizontalAlign="Left" />
</MasterTableView>
<FilterMenu EnableImageSprites="False">
</FilterMenu>
<HeaderContextMenu CssClass="GridContextMenu GridContextMenu_Default">
</HeaderContextMenu>
</telerik:RadGrid>
虽然您没有使用中继器,但您可能遇到了与此 SO 问题中描述和解决的相同问题 - How to FindControls in repeater control?
也就是说,在绑定 RadGrid 之前,控件不存在。
我下面有下面的 RadGrid。 我在编辑时尝试访问 RadEditor,并通过以下方式将可见设置为 false,但收到错误消息:
protected void ItemCommand(object sender, GridCommandEventArgs e)
{
if (e.CommandName == "Edit")
{
var gridItem = e.Item as GridEditableItem;
if ("ActionItems".Equals(e.Item.OwnerTableView.Name))
{
GridEditableItem editItem = e.Item as GridEditableItem;
RadEditor radEditor = editItem.FindControl("LimitEdit") as RadEditor;
radEditor.Visible = false; // get error message saying radEditor is null.
}
}
}
<telerik:RadGrid HeaderStyle-ForeColor="White" ID="SupportItemGrid" runat="server" CellSpacing="0" DataSourceID="SupportItemSource"
GridLines="None" OnInsertCommand="ItemInsert" OnItemCommand="ItemCommand" Skin="Metro" OnDeleteCommand="RadGrid1_DeleteCommand"
OnDataBound="SupportItemGrid_DataBound" OnItemDataBound="ItemDatabound" AllowSorting="true" AllowAutomaticUpdates="true">
<ValidationSettings EnableValidation="True" CommandsToValidate="Insert"></ValidationSettings>
<MasterTableView AutoGenerateColumns="False" DataKeyNames="CompanyId,ItemId" DataSourceID="ActionItemSource"
AllowAutomaticInserts="True" AllowAutomaticUpdates="True" CommandItemDisplay="Top" AllowSorting="true"
Name="ActionItems" AllowPaging="True" PageSize="20" ExpandCollapseColumn-HeaderText="Comments"
ExpandCollapseColumn-UniqueName="Comments" HierarchyLoadMode="Client">
<HeaderStyle HorizontalAlign="Center" ForeColor="White"></HeaderStyle>
<CommandItemSettings AddNewRecordText="Add New Support Item" ShowExportToExcelButton="true"></CommandItemSettings>
<EditFormSettings>
<EditColumn FilterControlAltText="Filter EditCommandColumn column">
</EditColumn>
</EditFormSettings>
<DetailTables>
<telerik:GridTableView runat="server" AllowAutomaticInserts="True" DataKeyNames="CommentId"
Name="Comments" DataSourceID="CommentSource" CommandItemDisplay="Top" AutoGenerateColumns="False"
NoDetailRecordsText="No Comments To Display">
<ParentTableRelation>
<telerik:GridRelationFields DetailKeyField="CompanyId" MasterKeyField="CompanyId" />
<telerik:GridRelationFields DetailKeyField="ItemId" MasterKeyField="ItemId" />
</ParentTableRelation>
<CommandItemSettings ExportToPdfText="Export to PDF" AddNewRecordText="Add New Comment" />
<RowIndicatorColumn FilterControlAltText="Filter RowIndicator column" Visible="True">
<HeaderStyle Width="20px" />
</RowIndicatorColumn>
<ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column" Visible="True">
<HeaderStyle Width="20px" />
</ExpandCollapseColumn>
<EditFormSettings>
<EditColumn FilterControlAltText="Filter EditCommandColumn column">
</EditColumn>
</EditFormSettings>
<Columns>
<telerik:GridTemplateColumn DataField="Comment" FilterControlAltText="Filter Comment column"
HeaderText="Comment" SortExpression="Comment" UniqueName="Comment">
<EditItemTemplate>
<asp:TextBox ID="DescriptionTextBox" runat="server" TextMode="MultiLine" Height="100px"
Width="400" Text='<%# Bind("Comment") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="CommentLabel" runat="server" Text='<%# Eval("Comment") %>'></asp:Label>
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridBoundColumn DataField="UserName" DataType="System.String" FilterControlAltText="Filter UserName column"
HeaderText="User Name" SortExpression="UserName" UniqueName="UserName" ReadOnly="True">
<HeaderStyle Width="150"></HeaderStyle>
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="DateEntered" DataType="System.DateTime" FilterControlAltText="Filter DateEntered column"
HeaderText="Date Entered" SortExpression="DateEntered" UniqueName="DateEntered"
ReadOnly="True">
<HeaderStyle Width="150"></HeaderStyle>
</telerik:GridBoundColumn>
<telerik:GridTemplateColumn UniqueName="DeleteDetail" Visible="false">
<ItemTemplate>
<telerik:RadButton ID="RemoveButton" runat="server" CommandName="Delete" Text="Delete" Width="75px"></telerik:RadButton>
</ItemTemplate>
<HeaderStyle Width="75"></HeaderStyle>
</telerik:GridTemplateColumn>
</Columns>
</telerik:GridTableView>
</DetailTables>
<CommandItemSettings ExportToPdfText="Export to PDF"></CommandItemSettings>
<RowIndicatorColumn Visible="True" FilterControlAltText="Filter RowIndicator column">
<HeaderStyle Width="20px"></HeaderStyle>
</RowIndicatorColumn>
<ExpandCollapseColumn Visible="True" FilterControlAltText="Filter ExpandColumn column">
<HeaderStyle Width="20px"></HeaderStyle>
</ExpandCollapseColumn>
<Columns>
<telerik:GridDropDownColumn DataSourceID="SitesDS" ListTextField="SiteName"
ListValueField="SiteId" UniqueName="SiteId" SortExpression="SiteId"
HeaderText="Site" DataField="SiteId" DropDownControlType="DropDownList" AllowVirtualScrolling="true"
ShowMoreResultsBox="true" ItemsPerRequest="10">
</telerik:GridDropDownColumn>
<telerik:GridBoundColumn DataField="DateEntered" FilterControlAltText="Filter DateEntered column"
HeaderText="Date Entered" SortExpression="DateEntered" UniqueName="DateEntered"
DataType="System.DateTime">
<HeaderStyle Width="85"></HeaderStyle>
</telerik:GridBoundColumn>
<telerik:GridDropDownColumn DataSourceID="ItemTypeSource" ListTextField="ItemType"
ListValueField="ItemTypeId" UniqueName="ItemTypeId" SortExpression="ItemTypeId"
HeaderText="Type" DataField="ItemTypeId" DropDownControlType="DropDownList" AllowVirtualScrolling="true"
ShowMoreResultsBox="true" ItemsPerRequest="10">
<HeaderStyle Width="120"></HeaderStyle>
</telerik:GridDropDownColumn>
<telerik:GridTemplateColumn DataField="Description" FilterControlAltText="Filter Description column"
HeaderText="Description" SortExpression="Description" UniqueName="Description">
<EditItemTemplate>
<asp:TextBox ID="txtDescription" UniqueName="txtDescription" runat="server" Text='<%# Bind("Description") %>'></asp:TextBox>
<telerik:RadEditor StripFormattingOnPaste="MSWord" ID="LimitEdit" runat="server" Content='<%# Bind("Description") %>'
Skin="Windows7">
</telerik:RadEditor>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="DescriptionLabel" runat="server" Text='<%# Eval("Description") %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Left" />
</telerik:GridTemplateColumn>
<telerik:GridButtonColumn ConfirmText="Delete?" ConfirmDialogType="RadWindow" HeaderText="Delete" SortExpression="DeleteMain" UniqueName="DeleteMain"
Display="false" ConfirmTitle="Delete" ButtonType="ImageButton" CommandName="Delete" />
</Columns>
<EditFormSettings>
<EditColumn FilterControlAltText="Filter EditCommandColumn column">
</EditColumn>
</EditFormSettings>
<EditItemStyle HorizontalAlign="Left" />
</MasterTableView>
<FilterMenu EnableImageSprites="False">
</FilterMenu>
<HeaderContextMenu CssClass="GridContextMenu GridContextMenu_Default">
</HeaderContextMenu>
</telerik:RadGrid>
虽然您没有使用中继器,但您可能遇到了与此 SO 问题中描述和解决的相同问题 - How to FindControls in repeater control?
也就是说,在绑定 RadGrid 之前,控件不存在。