简体   繁体   English

ASP.NET控件分配给属性null

[英]ASP.NET Control assign to property null

I have piece of code: 我有一段代码:

    protected void Page_Load(object sender, EventArgs e)
    {
        _SearchResult.GridViewChanged += _SearchResult_GridViewChanged;
    }

    public Control GvControls { get; set; }

    protected void _SearchResult_GridViewChanged(object sender, Orders.GridViewChangedEventArgs e)
    {
        this.GvControls = e.GControl;            
    }

    protected void export_ServerClick()
    {
        new ExportGridView(this.GvControls,"report");
    }

One control generate a event with event arg and push a control by arg, then other (this) control do some push-ups with this arg 一个控件使用事件arg生成一个事件,并通过arg推送一个控件,然后其他(此)控件对该arg进行一些上推

and when am i try to do push export_ServerClick() property GvControls equals null but before whould be not null. 当我尝试执行push export_ServerClick()属性时, GvControls等于null,但在GvControls null之前。 Why is it happening. 为什么会这样呢。

ThankX ThankX

All variables (and even controls) are disposed at the end of the page's lifecycle. 所有变量(甚至控件)都位于页面生命周期的末尾。 That's the reason why you have to recreate all dynamically created controls on postback. 这就是为什么您必须在回发时重新创建所有动态创建的控件的原因。 For the same reason you have to re-initialize a variable with the desired value. 出于同样的原因,您必须使用所需的值重新初始化变量。

So i assume that the GridViewChanged -event is triggered first. 所以我假设首先触发GridViewChanged -event。 Then the user clicks another button that triggers the export_ServerClick -event handler. 然后,用户单击另一个按钮,该按钮将触发export_ServerClick事件处理程序。 At this point the field GvControls has gain it's default value which is null . 此时,字段GvControls已获得其默认值null

You have to persist this value somewhere, for example in the Session . 您必须将此值保留在某个地方,例如在Session

Nine Options for Managing Persistent User State in Your ASP.NET Application 在ASP.NET应用程序中管理持久用户状态的九个选项

However, i think that you should not need to persist the contol itself. 但是,我认为您不必坚持控制本身。 Perhaps it is sufficient to store the ID of the control. 也许存储控件的ID就足够了。 Maybe it's even better to load it again from the datasource before you export it for two reasons: 也许最好在导出数据之前从数据源再次加载它,原因有两个:

  1. the shown data could already be outdated because other users have changed it 显示的数据可能已经过时,因为其他用户已对其进行了更改
  2. you you don't need to fiddle around with strings in the control but you have the original values 您不需要在控件中随意使用字符串,但您拥有原始值

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

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