简体   繁体   English

从父控件删除所有子控件时出错

[英]Error while deleting all child controls from the parent control

I have the next aspx page: 我有下一个aspx页面:

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.Master" AutoEventWireup="true"
    CodeBehind="newsEditor.aspx.cs" Inherits="ExpertSiteV2.newsEditor" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<%@ Register Assembly="ExpertSiteV2" Namespace="ExpertSiteV2" TagPrefix="custom" %>
<asp:Content ID="Content3" ContentPlaceHolderID="Main" runat="server">
    <asp:Panel ID="Panel1" runat="server">
        <asp:Panel ID="Panel2" runat="server" Width="660" Style="margin-bottom: 10px;">
            <asp:Label ID="Label1" runat="server" Text="Label" Width="150">Заголовок новости</asp:Label>
            <asp:TextBox ID="newsTitle" runat="server" Width="500" Style="float: right;"></asp:TextBox>
        </asp:Panel>
        <custom:CustomEditor ID="Editor3" runat="server" Height="300" Width="660" BackColor="White" />
        <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
        </asp:ToolkitScriptManager>
        <asp:Panel ID="Panel3" runat="server" Style="margin-top: 5px;" CssClass="buttonPanel">
            <asp:ImageButton ID="SaveImageButton1" runat="server" ImageUrl="img/save_32.png"
                ToolTip="Сохранить новость" />
            <asp:LinkButton ID="SaveLinkButton1" runat="server" ToolTip="Сохранить новость">Сохранить</asp:LinkButton>
            <asp:ImageButton ID="ImageButton2" runat="server" CausesValidation="False" ImageUrl="img/block_32.png"
                PostBackUrl="news.aspx" ToolTip="Вернуться к странице новостей" />
            <asp:LinkButton ID="LinkButton2" runat="server" ToolTip="Вернуться к странице новостей"
                CausesValidation="False" PostBackUrl="news.aspx">Отмена</asp:LinkButton>
        </asp:Panel>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Заголовок должен быть заполнен!"
            ControlToValidate="newsTitle" Display="Dynamic">
        </asp:RequiredFieldValidator>
    </asp:Panel>
</asp:Content>

I want to delete all the contents of Panel1 . 我要删除Panel1所有内容。 I write the code: 我写代码:

Panel1.Controls.Clear();

But it doesn't work and I get the message: 但这不起作用,我得到消息:

Page cannot be null. 页面不能为空。 Please ensure that this operation is being performed in the context of an ASP.NET request. 请确保正在ASP.NET请求的上下文中执行此操作。 Description: An unhandled exception occurred during the execution of the current web request. 说明:执行当前Web请求期间发生未处理的异常。 Please review the stack trace for more information about the error and where it originated in the code. 请查看堆栈跟踪,以获取有关错误及其在代码中起源的更多信息。

Exception Details: System.InvalidOperationException: Page cannot be null. 异常详细信息:System.InvalidOperationException:页面不能为null。 Please ensure that this operation is being performed in the context of an ASP.NET request. 请确保正在ASP.NET请求的上下文中执行此操作。

Source Error: 源错误:

An unhandled exception was generated during the execution of the current web request. 当前Web请求的执行期间生成了未处理的异常。 Information regarding the origin and location of the exception can be identified using the exception stack trace below. 可以使用下面的异常堆栈跟踪来标识有关异常的来源和位置的信息。

Stack Trace: 堆栈跟踪:

[InvalidOperationException: Page cannot be null. [InvalidOperationException:页面不能为null。 Please ensure that this operation is being performed in the context of an ASP.NET request.] 请确保正在ASP.NET请求的上下文中执行此操作。]
System.Web.UI.ScriptManager.get_IPage() +373832 System.Web.UI.ScriptManager.OnPagePreRenderComplete(Object sender, EventArgs e) +54 System.Web.UI.ScriptManager.get_IPage()+373832 System.Web.UI.ScriptManager.OnPagePreRenderComplete(对象发送方,EventArgs e)+54
System.Web.UI.Page.OnPreRenderComplete(EventArgs e) +8698462 System.Web.UI.Page.OnPreRenderComplete(EventArgs e)+8698462
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1029 System.Web.UI.Page.ProcessRequestMain(布尔includeStagesBeforeAsyncPoint,布尔includeStagesAfterAsyncPoint)+1029

Blockquote 块引用

What's wrong? 怎么了? How should I do it properly? 我应该如何正确做?

Can't you not just hide the panel? 您不能不只是隐藏面板吗? Panel1.Visible = false. Panel1.Visible = false。 why do you wanna remove the controls. 您为什么要删除控件。

Aha ok I get the question now: Check out this project, http://www.codeproject.com/KB/user-controls/DynamicUC.aspx 啊,好吧,我现在得到的问题是:检出此项目, http://www.codeproject.com/KB/user-controls/DynamicUC.aspx

Okay, i've written something like this: 好吧,我写了这样的东西:

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.Master" AutoEventWireup="true"
CodeBehind="newsEditor.aspx.cs" Inherits="ExpertSiteV2.newsEditor" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<%@ Register Assembly="ExpertSiteV2" Namespace="ExpertSiteV2" TagPrefix="custom" %>
<asp:Content ID="Content3" ContentPlaceHolderID="Main" runat="server">
    <asp:Panel ID="Panel4" runat="server">
        <asp:Panel ID="Panel1" runat="server">
            <asp:Panel ID="Panel2" runat="server" Width="660" Style="margin-bottom: 10px;">
                <asp:Label ID="Label1" runat="server" Text="Label" Width="150">Заголовок новости</asp:Label>
                <asp:TextBox ID="newsTitle" runat="server" Width="500" Style="float: right;"></asp:TextBox>
            </asp:Panel>
            <custom:CustomEditor ID="Editor3" runat="server" Height="300" Width="660" BackColor="White" />
            <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
            </asp:ToolkitScriptManager>
            <asp:Panel ID="Panel3" runat="server" Style="margin-top: 5px;" CssClass="buttonPanel">
                <asp:ImageButton ID="SaveImageButton1" runat="server" ImageUrl="img/save_32.png"
                    ToolTip="Сохранить новость" />
                <asp:LinkButton ID="SaveLinkButton1" runat="server" ToolTip="Сохранить новость">Сохранить</asp:LinkButton>
                <asp:ImageButton ID="ImageButton2" runat="server" CausesValidation="False" ImageUrl="img/block_32.png"
                    PostBackUrl="news.aspx" ToolTip="Вернуться к странице новостей" />
                <asp:LinkButton ID="LinkButton2" runat="server" ToolTip="Вернуться к странице новостей"
                    CausesValidation="False" PostBackUrl="news.aspx">Отмена</asp:LinkButton>
            </asp:Panel>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Заголовок должен быть заполнен!"
                ControlToValidate="newsTitle" Display="Dynamic">
            </asp:RequiredFieldValidator>
        </asp:Panel>
    </asp:Panel>
</asp:Content>

Here is one more panel that contains everything. 这是另一个包含所有内容的面板。 And I've written: 我写了:

Panel4.Controls.Clear();

It works fine. 工作正常。 I still don't know why the Panel1 doesn't want to remove child controls. 我仍然不知道为什么Panel1不想删除子控件。 If someone knows just write the answer. 如果有人知道,请写下答案。

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

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