简体   繁体   中英

Error while deleting all child controls from the parent control

I have the next aspx page:

<%@ 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 . 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. Description: An unhandled exception occurred during the execution of the current web request. 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. Please ensure that this operation is being performed in the context of an ASP.NET request.

Source Error:

An unhandled exception was generated during the execution of the current web request. 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. Please ensure that this operation is being performed in the context of an ASP.NET request.]
System.Web.UI.ScriptManager.get_IPage() +373832 System.Web.UI.ScriptManager.OnPagePreRenderComplete(Object sender, EventArgs e) +54
System.Web.UI.Page.OnPreRenderComplete(EventArgs e) +8698462
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1029

Blockquote

What's wrong? How should I do it properly?

Can't you not just hide the panel? 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

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. If someone knows just write the answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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