Can't get multiview control to change views

2019-07-20 12:42发布

问题:

I have a multiview control, that has two views. View1 is the default view, and View2 is the new view. When the end user click a button, I want to change the view to View2. I can't seem to achieve this anyway I go.

ASP.NET Code:

<asp:MultiView ID="MVOrder" runat="server">
    <asp:View ID="VOrderNow" runat="server">
        <asp:UpdatePanel ID="UpdatePanel2" runat="server">
            <ContentTemplate>
                <table>
                    <tr>
                        <td>
                        <asp:Label ID="LblInfo" runat="server"></asp:Label>
                        </td>
                        <td>
                        &nbsp;</td>
                        <td>&nbsp;</td>
                    </tr>
                    <tr>
                        <td>
                        <asp:Label ID="LblDrink" runat="server"></asp:Label>
                        </td>
                        <td></td>
                        <td></td>
                    </tr>
                    <tr>
                        <td>
                        <asp:Label ID="LblItemInfo" runat="server"></asp:Label>
                        </td>
                        <td>
                        &nbsp;</td>
                        <td>&nbsp;</td>
                    </tr>
                </table>
                <br />
                <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
            </ContentTemplate>
        </asp:UpdatePanel>
    </asp:View>
    <asp:View runat="server" ID="VOrderComplete">

    <table class="auto-style1">
        <tr>
            <td>&nbsp;</td>
            <td>
            <asp:Label ID="LblOrderComplete" runat="server"></asp:Label>
            </td>
            <td>&nbsp;</td>
        </tr>
        <tr>
            <td>&nbsp;</td>
            <td>
            <asp:Label ID="LblOrderNumberAgain" runat="server"></asp:Label>
            </td>
            <td>&nbsp;</td>
        </tr>
        <tr>
            <td>&nbsp;</td>
            <td>
            <asp:Label ID="LblOrderTimeAgain" runat="server"></asp:Label>
            </td>
            <td>&nbsp;</td>
        </tr>
    </table>

    </asp:View>
<br/>
</asp:MultiView>

Button Click Event Code:

protected void Button1_Click(object sender, EventArgs e)
        {
            //MVOrder.ActiveViewIndex = 1;
            MVOrder.SetActiveView(VOrderComplete);
        } 

Page Load Code:

if (!IsPostBack)
{
    MVOrder.SetActiveView(VOrderNow);
}

Can someone please tell me what I am doing wrong?

回答1:

I think you might have some additional code that might make this not work properly. But based on what you've shown, this will post and update your view:

<asp:UpdatePanel ID="UpdatePanel2" runat="server">
    <ContentTemplate>
        <asp:MultiView ID="MVOrder" runat="server">
            <asp:View ID="VOrderNow" runat="server">
                <table>
                    <tr>
                        <td><asp:Label ID="LblInfo" runat="server" Text="asdfasdfasdf"></asp:Label></td>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                    </tr>
                    <tr>
                        <td><asp:Label ID="LblDrink" runat="server" Text="lmnoplmnop"></asp:Label></td>
                        <td></td>
                        <td></td>
                    </tr>
                    <tr>
                        <td><asp:Label ID="LblItemInfo" runat="server" Text="iteminfo"></asp:Label></td>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                    </tr>
                </table>
                <br />
                <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
            </asp:View>
            <asp:View runat="server" ID="VOrderComplete">
                <table class="auto-style1">
                    <tr>
                        <td>&nbsp;</td>
                        <td><asp:Label ID="LblOrderComplete" runat="server" Text="ordercomplete"></asp:Label></td>
                        <td>&nbsp;</td>
                    </tr>
                    <tr>
                        <td>&nbsp;</td>
                        <td><asp:Label ID="LblOrderNumberAgain" runat="server" Text="ordernumberagain"></asp:Label></td>
                        <td>&nbsp;</td>
                    </tr>
                    <tr>
                        <td>&nbsp;</td>
                        <td><asp:Label ID="LblOrderTimeAgain" runat="server" Text="ordertimeagain"></asp:Label></td>
                        <td>&nbsp;</td>
                    </tr>
                </table>
            </asp:View>
            <br />
        </asp:MultiView>
    </ContentTemplate>
</asp:UpdatePanel>