更改ActiveViewIndex后ASP按钮不起作用

时间:2012-04-05 14:38:55

标签: c# asp.net multiview

我正在使用MultiView构建多步骤表单。下一个按钮适用于第一步(ViewOne),但是下一步和后退按钮都不会执行下一步(ViewTwo)。

我不知道下面的代码中是否有任何线索,或者您需要查看更多?...

 <asp:MultiView ID="MultiView2" runat="server" ActiveViewIndex="0">
 <asp:View ID="ViewOne" runat="server">
 </asp:View>
 <asp:View ID="ViewTwo" runat="server">
 </asp:View>
 </asp:MultiView>
 <asp:ImageButton ID="btnBack" runat="server" ImageUrl="/assets/back_button.jpg"  OnClick="btnBack_Click" /> 
 <asp:ImageButton ID="btnNext" runat="server" ImageUrl="/assets/next_button.gif" OnClick="btnNext_Click" />
 <asp:ImageButton ID="btnSubmit" runat="server" ImageUrl="/assets/submit_button.gif" OnClick="btnSend_Click" />

代码背后......

protected void btnBack_Click(object sender, EventArgs e)
    {
        MultiView2.ActiveViewIndex--;
    }

    protected void btnNext_Click(object sender, EventArgs e)
    {
        MultiView2.ActiveViewIndex++;
    }

    protected void btnSend_Click(object sender, EventArgs e)
    {
        OnSubmitClicked(e);
    }

更多代码......

protected override void OnLoad(EventArgs e)
{
        base.OnLoad(e);
     PopulateSomeField();
    }

protected override void OnPreRender(EventArgs e)
    {
        btnBack.Visible = MultiView2.ActiveViewIndex > 0;
        btnNext.Visible = MultiView2.ActiveViewIndex < MultiView2.Views.Count - 1;
        btnSubmit.Visible = MultiView2.ActiveViewIndex == MultiView2.Views.Count - 1;
        base.OnPreRender(e);
    }

1 个答案:

答案 0 :(得分:2)

那里没有错误。它对我来说很好。

<强> Test2.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test2.aspx.cs" Inherits="test2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:MultiView ID="MultiView2" runat="server" ActiveViewIndex="0">
         <asp:View ID="ViewOne" runat="server">
            One
         </asp:View>
         <asp:View ID="ViewTwo" runat="server">
            Two
         </asp:View>
         </asp:MultiView>
         <asp:ImageButton ID="btnBack" runat="server" ImageUrl="/assets/back_button.jpg"  OnClick="btnBack_Click" /> 
         <asp:ImageButton ID="btnNext" runat="server" ImageUrl="/assets/next_button.gif" OnClick="btnNext_Click" />
         <asp:ImageButton ID="btnSubmit" runat="server" ImageUrl="/assets/submit_button.gif" OnClick="btnSend_Click" />
    </div>
    </form>
</body>
</html>

<强> Test2.aspx.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class test2 : System.Web.UI.Page
{
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        //PopulateSomeField();
    }

    protected override void OnPreRender(EventArgs e)
    {
        btnBack.Visible = MultiView2.ActiveViewIndex > 0;
        btnNext.Visible = MultiView2.ActiveViewIndex < MultiView2.Views.Count - 1;
        btnSubmit.Visible = MultiView2.ActiveViewIndex == MultiView2.Views.Count - 1;
        base.OnPreRender(e);
    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        MultiView2.ActiveViewIndex--;
    }

    protected void btnNext_Click(object sender, EventArgs e)
    {
        MultiView2.ActiveViewIndex++;
    }

    protected void btnSend_Click(object sender, EventArgs e)
    {
        Response.Write("submitted");
    }

}