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

发布于 2024-09-14 21:37:24 字数 3141 浏览 2 评论 0原文

我有下一个 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>

我想删除 Panel1 的所有内容。我编写了代码:

Panel1.Controls.Clear();

但它不起作用,我收到消息:

页面不能为空。请确保 正在执行此操作 在 ASP.NET 请求的上下文中。 描述:未处理的异常 执行期间发生的 当前的网络请求。请查看 堆栈跟踪以获取有关的更多信息 错误及其起源 代码。

异常详细信息: System.InvalidOperationException:页面 不能为空。请确保 该操作正在执行 ASP.NET 请求的上下文。

来源错误:

生成了未处理的异常 当前执行期间 网络请求。有关信息 异常的起源和地点 可以使用异常来识别 下面的堆栈跟踪。

堆栈跟踪:

[InvalidOperationException:页面 不能为空。请确保 该操作正在执行 ASP.NET 请求的上下文。]
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.ProcessRequestMain(布尔值 includeStagesBeforeAsyncPoint,布尔值 includeStagesAfterAsyncPoint)+1029

块引用

有什么问题吗?我应该怎样做才正确呢?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不一样的天空 2024-09-21 21:37:24

好的,我写了这样的内容:

<%@ 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>

这是另一个包含所有内容的面板。我已经写道:

Panel4.Controls.Clear();

效果很好。
我仍然不知道为什么 Panel1 不想删除子控件。如果有人知道就写下答案。

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.

孤单情人 2024-09-21 21:37:24

不能直接隐藏面板吗? Panel1.Visible = false。为什么你想删除控件。

啊哈,好吧,我现在明白了问题:
查看此项目,http://www.codeproject.com/KB/user -controls/DynamicUC.aspx

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文