取消对页面加载控件时ASP.NET问题 [英] ASP.NET issue when removing Control on Page Load

查看:89
本文介绍了取消对页面加载控件时ASP.NET问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的Index.aspx Web表单:

I have following Index.aspx web form:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="ProblematicWebApplication.Index" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <asp:Label ID="dummyLabel" runat="server" Text="This is dummy label." />
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="intDropDownList" runat="server"/>
        <br />
        <asp:Button Text="Submit" OnClick="OnSubmitClicked" runat="server"/>
        <br />
        Selected value: <asp:Label ID="selectedValueLabel" runat="server" />
    </div>
    </form>
</body>
</html>

和紧随其后Index.aspx.cs文件code:

And following code behind Index.aspx.cs file:

using System;
using System.Linq;

namespace ProblematicWebApplication
{
    public partial class Index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.intDropDownList.DataSource = Enumerable.Range(0, 11).ToArray();
                this.intDropDownList.DataBind();
            }

            if (this.Request["remove-dummy"] != null)
                this.Controls.Remove(this.dummyLabel);
        }

        protected void OnSubmitClicked(object sender, EventArgs e)
        {
            this.selectedValueLabel.Text = this.intDropDownList.SelectedValue;
        }
    }
}

当我运行我的查询字符串中没有remove-虚拟参数的应用程序,然后从intDropDownList一些值,然后单击提交按钮,选择的值是相应的美元selectedValueLabel psented p $。

When I run my application with no remove-dummy parameter in query string and select some value from intDropDownList and click Submit button, selected value is accordingly presented in selectedValueLabel.

但是,如果我跑我的查询字符串中删除哑= 1参数应用,dummyLabel被删除。现在,当我选择intDropDownList一些值,然后单击提交按钮,选定值不正确写入selectedValueLabel和intDropDownList所有项目都被删除。

But if I run my application with remove-dummy=1 parameter in query string, dummyLabel gets removed. Now when I select some value from intDropDownList and click Submit button, selected value is not correctly written to selectedValueLabel and all items from intDropDownList are removed.

有人能向我解释为什么发生这种情况?
为什么删除无关dummyLabel控制对intDropDownList控制的影响?

Can someone explain it to me why this is happening? Why removing unrelated dummyLabel control has influence on intDropDownList control?

推荐答案

似乎加载的ViewState在回传dummyLabel在previous页面加载后取出失败。

It seems that ViewState loading fails in postback after dummyLabel is removed in previous page load.

详细内容:

下面的文章第一项研究:

first study following articles:

  • TRULY Understanding ViewState - Infinities Loop
  • ASP.NET Page Life Cycle Overview

下图显示了重要的ASP.NET页面事件和处理的ViewState之间在哪里发生。

Following image shows important ASP.NET page events and where in between ViewState handling takes place.

所以,当声明控制dummyLabel被删除会发生什么?这里是一个过程:

So what happens when declarative Control dummyLabel is removed? Here is a process:


  1. 页面被请求首次与查询字符串参数中删除哑= 1。

  2. 在Page_Load事件的声明性控制dummyLabel被删除。

  3. SaveStateComplete事件之前,ViewState是保存。有控制树没有控制dummyLabel,所以它的ViewState不会被保存。

  4. 提交按钮被点击。

  5. InitComplete和preLOAD事件之间,ViewState获取加载。这是它打破了,因为控件树现在包含dummyLabel(dummyLabel被删除后,在Load事件)和 ASP.NET无法在递归加载的ViewState到页面控件树。我的假设是,ViewState的页面和控件树是紧耦合和递归的ViewState加载失败,因为这紧密耦合的结果。

  1. Page is requested for the first time with query string parameter remove-dummy=1.
  2. In Page_Load event declarative Control dummyLabel is removed.
  3. Before SaveStateComplete event, ViewState is saved. There is no control dummyLabel in control tree, so its ViewState won't be saved.
  4. Submit button is clicked.
  5. Between InitComplete and PreLoad events, ViewState gets loaded. This is where it breaks because control tree now contains dummyLabel (dummyLabel gets removed after, in Load event) and ASP.NET fails in recursively loading ViewState into Page control tree. My assumption is that ViewState and Page control tree are tightly coupled and recursive ViewState loading fails as a consequence of this tight coupling.

这是备份,这个理论还有一个情况:如果你把dummyLabel在页面的最末端,问题不会再发生,因为在页面控件树dummyLabel之前出现的所有其他控件已经回升,从ViewState中正确的价值观(ViewState的结构和页面控件树是紧密耦合)。如果dummyLabel后更多的控制有,他们的ViewState加载会失败。

One more situation that backs-up this theory: if you place dummyLabel at the very end of the page, issue doesn't happen anymore, because all other controls in Page control tree that come before dummyLabel already picked correct values from ViewState (ViewState structure and Page control tree are tightly coupled). If there were more controls after dummyLabel, their ViewState loading would fail.

要解决此问题,ViewState中加载发生之前所有声明的控制(在ASPX文件中定义)应删除,必须拆除。

To resolve this issue, all declarative Controls (defined in ASPX file) that should be removed, must be removed before ViewState loading takes place - in InitComplete event or any other event before it.

这篇关于取消对页面加载控件时ASP.NET问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆