简体   繁体   中英

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

and when am i try to do push export_ServerClick() property GvControls equals null but before whould be not null. Why is it happening.

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. Then the user clicks another button that triggers the export_ServerClick -event handler. At this point the field GvControls has gain it's default value which is null .

You have to persist this value somewhere, for example in the Session .

Nine Options for Managing Persistent User State in Your ASP.NET Application

However, i think that you should not need to persist the contol itself. Perhaps it is sufficient to store the ID of the control. 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

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