更改视图后不显示按钮

时间:2017-02-10 19:06:38

标签: asp.net background-image multiview

我目前遇到的问题是我的按钮和其他控件在更改视图后没有出现在页面上。该程序设置如下:

  1. 用户在文本框中输入一个数字(tbxNumber)
  2. 用户单击按钮(btnNumber),该按钮使用" views"填充下拉列表(ddlViews)。视图的数量取决于输入的数字。
  3. 用户从下拉列表中选择一个视图,然后单击另一个按钮。按钮切换到与多视图(mtvNumber)中的视图对应的视图。
  4. 我让程序切换到正确的视图,但是当它切换按钮时,文本框和下拉列表都会消失。这是在asp.net中更改视图背景的正确方法吗?

    <asp:MultiView ID="mtvNumber" runat="server" ActiveViewIndex ="-1">
        <asp:View ID="View1" runat="server">
            <div style='position:absolute;z-index:0;left:0;top:0;width:100%;height:100%'>
                  <img src='Images/Patriots.jpg' style='width:100%;height:100%' alt='[]' />
            </div>
        <p>Hello from view1</p>
    
        </asp:View>
    

    我不确定这是否是将图像设置为背景的正确方法。如果有其他人有其他方式引入图片,请告诉我!

    ASP.Net代码

    <div>
        <form id="formViews" runat="server">
        <asp:TextBox ID="tbxNumber" runat="server"></asp:TextBox>
        <asp:Button ID="btnNumber" runat="server" Text="Button" OnClick="btnNumber_Click" />
        <asp:DropDownList ID="ddlViews" runat="server" OnSelectedIndexChanged="ddlViews_SelectedIndexChanged"></asp:DropDownList>
            <asp:Button ID="BtnChangeView" runat="server" Text="Change View" OnClick="BtnChangeView_Click" />
        <asp:MultiView ID="mtvNumber" runat="server" ActiveViewIndex ="-1">
        <asp:View ID="View1" runat="server">
            <div style='position:absolute;z-index:0;left:0;top:0;width:100%;height:100%'>
                  <img src='Images/Patriots.jpg' style='width:100%;height:100%' alt='[]' />
            </div>
        <p>Hello from view1</p>
    
        </asp:View>
        <asp:View ID="View2" runat="server">
            <div style='position:absolute;z-index:0;left:0;top:0;width:100%;height:100%'>
                  <img src='Images/patriots2.jpg' style='width:100%;height:100%' alt='[]' />
            </div>
        </asp:View>
    

    C#代码

     protected void BtnChangeView_Click(object sender, EventArgs e)
    {
        // string used to check input.
        string viewChecker = ddlViews.SelectedValue.ToString();
    
        // if view1 is selected, switch to view1
        if (viewChecker == "view1")
        {
            mtvNumber.ActiveViewIndex = 0;
            Response.Write("made it to the first view");
            BtnChangeView.Visible = true;
            btnNumber.Visible = true;
    
            // if view2 is selected, switch to view2
        }
        else if (viewChecker == "view2")
        {
            mtvNumber.ActiveViewIndex = 1;
            Response.Write("made it to the second view");
        }
        // if view3 is selected, switch to view3
        else if (viewChecker == "view3")
        {
            mtvNumber.ActiveViewIndex = 2;
            Response.Write("made it to the third view");
        }
    

1 个答案:

答案 0 :(得分:0)

也许您对图像应用的样式?试试这个,看看视图是否发生变化,其他控件是否仍在那里。

<div>
    <form id="formViews" runat="server">
    <asp:TextBox ID="tbxNumber" runat="server"></asp:TextBox>
    <asp:Button ID="btnNumber" runat="server" Text="Button" OnClick="btnNumber_Click" />
    <asp:DropDownList ID="ddlViews" runat="server" OnSelectedIndexChanged="ddlViews_SelectedIndexChanged"></asp:DropDownList>
        <asp:Button ID="BtnChangeView" runat="server" Text="Change View" OnClick="BtnChangeView_Click" />
    <asp:MultiView ID="mtvNumber" runat="server" ActiveViewIndex ="-1">
    <asp:View ID="View1" runat="server">
        <div>
        <h2>Patriots View 1</h2>
              <img src='Images/Patriots.jpg' style='width:100%;height:100%' alt='[]' />
        </div>
    <p>Hello from view1</p>

    </asp:View>
    <asp:View ID="View2" runat="server">
        <div>
          <h2>Patriots View 2</h2>
              <img src='Images/patriots2.jpg' style='width:100%;height:100%' alt='[]' />
        </div>
    </asp:View>