Vishwanath Mishra
Vishwanath Mishra

Reputation: 445

How to handle PostBack inside ASP.net Custom Control

May be I am missing something important in order to implement CustomControl properly as per requirement or having a lack of knowledge. Actually I have Created CustomControl (.dll)for searching purpose having 3 Dropdown boxes(populating from database), 1 textbox and search button, after choosing appropriate filter criteria user clicks on Search button and simple gridview appears. My problem is I dont want to fill dropdown boxes in CustomControl on every post back(fired from page).

Upvotes: 3

Views: 1778

Answers (1)

Aristos
Aristos

Reputation: 66649

When you are in a place that can not spot the Page you can use this global parameter:

System.Web.HttpContext.Current

Now from that you can get the Page if its available,

Page page = HttpContext.Current.Handler as Page;

if (page != null && page.IsPostBack)
{

}

or you can get the Form and the post back values using

System.Web.HttpContext.Current.Request.Form

and check if the Form have values, then is probably post back.

Just a note, always check if the System.Web.HttpContext.Current is not null before using it, because if you call it from a thread and there is no page available, then is null.

Upvotes: 2

Related Questions