views:

184

answers:

2

Hi,

I want pass values between web user controls without writing any code to the main page which user controls are put on. I do something like that but after doing that I need to click double to pass the value.

The example of what I've done :

Department User Control (Code-Behind)

        protected void Page_Load(object sender, EventArgs e)
        {
            int productId = ProductUserControl.selectedProductId;
            ... doing some bind work with productId


        }

Product User Control

        public static int selectedProductId;
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void lvDepartments_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
        if (e.CommandName == "selectDepartment")
           {
              selectedProductId = int.Parse(e.CommandArgument);
           }
        }

Thanks in advance...

+1  A: 

In your Department User Control you are trying to get the value of selectedProductId before it is set in the Product User Control. That's why you don't get the value you expect until you postback twice.

You'll need to get it after the Product User Control sets it in the ItemCommand event. Perhaps placing the Department User Control code in the Page_LoadCompleted... though I'm not sure if that will work either.

Another way to do it is to have Product User Control set a public property in Department User Control instead of having Department User Control try to read a property in Product User Control.

The issue seems to be a Page Lifecycle issue. http://www.robincurry.org/blog/content/binary/o_aspNet_Page_LifeCycle.jpg

I'm sure there's a better way than that as well.

metanaito
Page_LoadCompleted doesn't work :/
Braveyard
yeah, I noticed it doesn't work after trying it out. Unfortunately the only other solution I know of requires that you do something with the main page...
metanaito
then How can I do that in the main page ?
Braveyard
A: 

Try using delegates to achieve this more cleanly, example here

MOZILLA