Search code examples
c#asp.netupdatepanelmultiview

Trying to set view inside UpdatePanel in PageLoad


I'm having trouble with a combination of an UpdatePanel and MultiView.

I have an UpdatePanel at my top level, inside I have a bunch of imagebuttons - their click events set the view on the Multiview, and each View has an UpdatePanel inside of it with my binding.

Everything works great - but I am trying to set the View via the Querystring, so I can send a user to a particular view.

When I try and set the View from PageLoad - it says 'object does not exist'. So I figured I'd try it in Page_LoadComplete, which works great - but then my imagebuttons don't work to switch the view like they originally did.

What am I missing! Thanks!

  void Page_LoadComplete()
    {
        tabSelect= Request.QueryString["tab"];

            if (tabSelect.Contains("Community"))
            {
                MultiView1.SetActiveView(Community);

                btnCommunity.ImageUrl = "images/tabCommunity_on.png";

            }
     }




<asp:ScriptManager id="ScriptManager1" runat="server"/>
<asp:UpdatePanel id="UpdatePanel1" childrenastriggers="true" updatemode="Always" runat="server">
  <ContentTemplate>
    <asp:ImageButton id="btnCommunity" onclick="" runat="server">

    <asp:MultiView ID="MultiView1" ActiveViewIndex="0" runat="server">
        <asp:View ID="Community" runat="server">
            <asp:UpdatePanel id="UpdatePanel1" childrenastriggers="true" updatemode="Always" runat="server">
             //data controls in here
           </asp:UpdatePanel>
        </asp:View>
        <asp:View id="tabFriends" runat="server">
           <asp:UpdatePanel id="UpdatePanel2" childrenastriggers="true" updatemode="Always" runat="server">
             //data controls in here
           </asp:UpdatePanel>
         </asp:View>
     </asp:MultiView>
  </ContentTemplate>
</asp:UpdatePanel>

Solution

  • UPDATE: After reviewing your code in further detail, I believe figured out the problem.

    I made the following adjustments to the code:

    1. If the querystring is not passed, set tabSelect to empty string and thus avoiding a null object reference exception on the next line.

    2. Set the ImageUrl path to include ~ (for root).

    Please try the code below:

    void Page_LoadComplete()
    {
        string tabSelect = Request.QueryString["tab"] ?? string.Empty;
    
        if (tabSelect.Contains("Community"))
        {
            MultiView1.SetActiveView(Community);
            btnCommunity.ImageUrl = "~/images/tabCommunity_on.png";
        }
    }