Passing Value Between Web User Controls - DifferentQuestion

I want to pass values ​​between web user controls without typing code into the master page that the custom controls are overlaid on. I am doing something like this, but after that I need to double click to pass the value.

An example of what I did:

Department user management (by code)

        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


source to share


2 answers


In your custom control, you are trying to get the selectedProductId value before it gets set to User User Control. This is why you don't get the expected value until you return twice.

You will need to get it after the Product Product control sets it in the ItemCommand event. Possibly by putting the department control code of the Department in computer_package_file ... although I'm not sure if that would work.

Another way to do this is to set the User Product Control on the public property on the User Control for the control, instead of using the User Control to read the property on the User User Control.



The problem seems to be related to a page lifecycle issue. http://www.robincurry.org/blog/content/binary/o_aspNet_Page_LifeCycle.jpg

I'm sure there is a better way than to do this.

+1


source


Try using delegates to achieve this more cleanly, like here



0


source







All Articles