Suresh Behera

The Microsoft .Net Junkies

News

Blogroll

Reading

How to makeuse of IsPostPack Method inside UserControl?

How to makeuse of IsPostPack Method inside UserControl? 

IsPostBack method will always be true inside user control. You can use following alternate method to by pass this isse

 protected override void OnLoad(EventArgs e) {
base.OnLoad(e);
if (!IsUserControlPostBack)
{
///Write your code
}
}

//Make ReadOnly property.

protected bool IsUserControlPostBack{
get
{
   return this.ViewState["IsUserControlPostBack"] != null;
}

Hope this help..

Suresh Behera

Comments

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# September 26, 2007 12:35 PM

rams said:

Thanks for the tip.

But where is ViewState["IsUserControlPostBack"] set to some value?

# September 26, 2007 2:25 PM

Richard said:

No, it won't. The IsPostBack property for a UserControl returns the same value as the IsPostBack property for the page. If you're getting true every time, you must be doing something unusual.

# September 26, 2007 2:41 PM

Dan said:

You should do a little testing on your assumption of IsPostBack always equaling true inside of a UserControl.  I'm working on a user control right now and put in Response.Write(Page.IsPostBack) and Response.Write(IsPostBack), and on a get request they both output "False".

# September 26, 2007 3:02 PM

Jason said:

Are you sure that IsPostBack is "always" true inside of a UserControl?

I have a project I'm working on, and the UserControl is called Contracts.ascx...Inside the Page_Load event, I have the following code:

           if (!Page.IsPostBack)

           {

               Response.Write("Can you see me?");

               SetProjectLabels();

           }

I only see the message "Can you see me?" one time...not every time the Page posts back.

# September 26, 2007 3:28 PM

Joe Chung said:

Umm, IsPostBack can be false in a UserControl.  Are you sure you're not doing something else wrong?

# September 26, 2007 8:21 PM

CM said:

Hi there,

Page.IsPostBack is not always true inside a control. What's the catch?

Thanks

# September 26, 2007 8:38 PM

Vikram said:

I could Not follow. Where are you adding the value for the ViewState["IsUserControlPostBack"]

# September 27, 2007 1:49 AM

Joe said:

A couple of remarks.

> IsPostBack method will always be true inside user control.

This is wrong: it will be true the first time you load a UserControl dynamically in response to a page PostBack, but is not true in the general case.

> You can use following alternate method to by pass this isse

The code has a bug - looks like you copied it incorrectly from blogs.clearscreen.com/.../2838.aspx

You omitted the line:

this.ViewState.Add("IsUserControlPostBack", true);

# September 27, 2007 2:42 AM

zameer said:

But wouldn't Page.IsPostBack achieve this?

# September 27, 2007 5:36 AM

Jaap said:

As far as I know, the IsPostback property works the same inside a Web User Control as it would be working inside a page.

Could you provide an example where it doesn't?

# September 27, 2007 11:06 AM

Big D said:

I'm sorry, I don't get this article at all. What do you mean IsPostBack is always true. Under what circumstances? If a Page is postbacked? Yes of course it is.

Could you please clarify things a bit more.

# September 27, 2007 12:28 PM

Clint Simon said:

This is simply false. IsPostBack returns the proper value inside a user control. Maybe you are misguided? Is there some other issue that you or not seeing?

Please don't spread false information.

# September 27, 2007 4:44 PM

Richard said:

The IsPostBack property will *NOT* always return true in a UserControl - it returns the same value as the Page.IsPostBack property.

# September 28, 2007 2:04 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)