简体   繁体   中英

How to update UserControl in another UserControl in C#?

I have problem with updating usercontrol in another usercontrol.

Example Code:

UserControl MyCart1 = (UserControl)Page.FindControl("MyCart1");
UpdatePanel up_shoppingcart = (UpdatePanel)MyCart1.FindControl("up_shoppingcart");
                    up_shoppingcart.Update();

This code shows Object reference not set to an instance of an object error

  1. You need to determine which of the three lines of code that you provided, throws the exception. This can easily done using debugger.

  2. FindControl method searches only immediate children controls. You can write a recursive version of it to search deeper.

)

public Control FindControlDeep(Control parent, string id) 
{
    Control result = parent.FindControl(id);
    if (result == null)
    {
        for (int iter = 0; iter < parent.Controls.Count; iter++)
        {
            result = FindControlDeep(parent.Controls[iter], id);
            if (result != null)
                break;
        }
    }
    return result;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM