Search code examples
c#asp.netmultiview

Multiview controls


I wrote the following code for multiview where I am using gridview and datalist:

<ContentPlaceHolderID="ContentPlaceHolder1">
    <div class="alert alert-success" >
      <div class="divbtn" style="text-align:right">
            <asp:LinkButton ID="gridbtn" runat="server" CssClass="glyphicon glyphicon-th" OnClick="gridbtn_Click"></asp:LinkButton>
            <asp:LinkButton ID="listbtn" runat="server" CssClass="glyphicon glyphicon-th-list" OnClick="listbtn_Click"></asp:LinkButton>
      </div>
    </div>

    <asp:MultiView runat="server" ID="multiview" ActiveViewIndex="0"> 
        <asp:View runat="server" ID="gridview">   
             <uc1:GridControl runat="server" ID="GridControl"/>
        </asp:View>

        <asp:View runat="server" ID="listview">
            <uc1:Listing runat="server" ID="Listing" />
        </asp:View>
    </asp:MultiView>

</asp:Content>

I am using two link buttons to call their respective views by firing two separate events as follows.

protected void listbtn_Click(object sender, EventArgs e)
    {
        multiview.ActiveViewIndex = 1;
    }

    protected void gridbtn_Click(object sender, EventArgs e)
    { 
        multiview.ActiveViewIndex = 0;
    }

Suppose, my datalist (Index=1) is active on my page and if there is a post back it should still be showing the datalist but on postback it automatically switches back to grid view (Index=0). I really need help with this!


Solution

  • You can save the index to a session variable and then read it back on post back like this:

    To save:

    Session["index"] = index.ToString();
    

    Read it on page load like this:

    Index = Session["index"];
    

    You will need the session variable to maintain state per user session. If you want to maintain state for the application then you have to use the application variable.