user2043533
user2043533

Reputation: 751

Event when HttpRequest Ends?

I am using asp.net forms. There is a Page_Load event,but is there an end event?

I have a linq datacontext created on pageload and I would like to dispose it when I am done.

Upvotes: 0

Views: 109

Answers (3)

Win
Win

Reputation: 62290

As Emmanuel N stated, there is Page_Unload event. However, if you use using, you do not need to worry about disposing DataContext.

Here is an example.

protected void buttonSearch_Click(object sender, EventArgs e)
{
  using (var context = new NorthwindDataContext())
  {
    var customers =
      from c in context.Customers
      select c;

    gridViewCustomers.DataSource = customers;
    gridViewCustomers.DataBind();
  }
}

Using is better than Dispose.

Upvotes: 1

Alex Tsvetkov
Alex Tsvetkov

Reputation: 1659

By the way if you are using Entity Framework, you don't have to dispose DbContext: the default behaviour is to open connection when needed and close it when done (more details).

Upvotes: 0

Emmanuel N
Emmanuel N

Reputation: 7449

You should probably do it on Page_Unload Event is the last event in page life cycle. For more on page events check out this.

Upvotes: 5

Related Questions