views:

526

answers:

2

Hi all,

I'm doing an asp.net application with one page. In this page, I have one usercontrol defined. This usercontrol has a menu (three buttons) and 3 usercontrols defined too. Depending on the clicked button one of the three usercontrols turn to visible true or false.

In these three usercontrols I have a button and a message, and I want to show the message "It's NOT postback" when the button of the menu is clicked, and when the button of the usercontrol is clicked the message will be "YES, it's postback!!!".

The question is that using property "IsPostBack" of the usercontrol or the page the message will never be "It's NOT postback" because of the clicked button of the menu to show the nested usercontrol.

This is the structure of the page:

page
  parent usercontrol
    menu
    nested usercontrol 1
      message
      button
    nested usercontrol 2
    nested usercontrol 3

I know it can be done using ViewState but, there is a way to simulate IsPostBack property or know when is the true usercontrol postback?

Thanks.

A: 

I have the solution for this problem. When an element fires an event is sent to the server in the Request.Form collection, so I created a property in nested usercontrols that checks if there is a child control (defined in the usercontrol itself) in Request.Form collection:

public bool IsUserControlPostBack
    {
        get 
        { 
            foreach (Control c in Controls)
                foreach(string key in Page.Request.Form.AllKeys)
                    if( c.ClientID == key.Replace('$','_'))
                        return true;

            return false;
        }
    }

This code can be a property of the usercontrol or, if it's called a lot of times, a variable that it's set on the OnInit event of the usercontrol.

jaloplo
A: 

Dear jaloplo,

you have saved me! Thank you very much!!!!!

Sliv3r