ASP.NET 用MultiView和View实现选项卡效果

           ASP.NET中的MultiView和View可以作为其他控件的容器,提供了一种可方便地显示信息的替换视图方式。通常情况下, MultiView和View搭配使用。我一般很少使用这两控件,本文讲讲用MultiView和View实现选项卡效果。贴上前台代码:

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication3._Default" %>  
  2.   
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4. <html xmlns="http://www.w3.org/1999/xhtml">  
  5. <head runat="server">  
  6.     <title>用MultiView和View实现选项卡效果</title>  
  7. </head>  
  8. <body>  
  9.     <form id="form1" runat="server">  
  10.     <div>  
  11.         <asp:Button ID="btnIntroduction" runat="server" BorderWidth="0" Text="公司简介" OnClick="btnIntroduction_Click" />  
  12.         <asp:Button ID="btnWelcome" runat="server" BorderWidth="0" Text="欢迎加盟" OnClick="btnIntroduction_Click" />  
  13.         <table style="border: 1px ridge #0000FF; width: 100%;">  
  14.             <tr valign="top">  
  15.                 <td style="width: 300; height: 250">  
  16.                     <asp:MultiView ID="MultiView1" runat="server" ActiveViewIndex="0">  
  17.                         <asp:View ID="View1" runat="server">  
  18.                             <asp:Label ID="Label1" runat="server"></asp:Label>  
  19.                         </asp:View>  
  20.                         <asp:View ID="View2" runat="server">  
  21.                             <asp:Label ID="Label2" runat="server"></asp:Label>  
  22.                         </asp:View>  
  23.                     </asp:MultiView>  
  24.                 </td>  
  25.             </tr>  
  26.         </table>  
  27.     </div>  
  28.     </form>  
  29. </body>  
  30. </html>  
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication3._Default" %>

<!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>用MultiView和View实现选项卡效果</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="btnIntroduction" runat="server" BorderWidth="0" Text="公司简介" OnClick="btnIntroduction_Click" />
        <asp:Button ID="btnWelcome" runat="server" BorderWidth="0" Text="欢迎加盟" OnClick="btnIntroduction_Click" />
        <table style="border: 1px ridge #0000FF; width: 100%;">
            <tr valign="top">
                <td style="width: 300; height: 250">
                    <asp:MultiView ID="MultiView1" runat="server" ActiveViewIndex="0">
                        <asp:View ID="View1" runat="server">
                            <asp:Label ID="Label1" runat="server"></asp:Label>
                        </asp:View>
                        <asp:View ID="View2" runat="server">
                            <asp:Label ID="Label2" runat="server"></asp:Label>
                        </asp:View>
                    </asp:MultiView>
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>
设计效果如下:


用两个Button(btnIntroduction、btnWelcome)作为切换Tab按钮,这两个Button按钮关联到同一个事件处理。其实用ImageButton来实现应该会更好看一点。

后台cs实现切换功能代码:

  1. using System;  
  2. using System.Collections;  
  3. using System.Configuration;  
  4. using System.Data;  
  5. using System.Linq;  
  6. using System.Web;  
  7. using System.Web.Security;  
  8. using System.Web.UI;  
  9. using System.Web.UI.HtmlControls;  
  10. using System.Web.UI.WebControls;  
  11. using System.Web.UI.WebControls.WebParts;  
  12. using System.Xml.Linq;  
  13.   
  14. namespace WebApplication3  
  15. {  
  16.     public partial class _Default : System.Web.UI.Page  
  17.     {  
  18.         protected void Page_Load(object sender, EventArgs e)  
  19.         {  
  20.             Label1.Text = @"本公司为一家集软件开发、传统网络开发以及无线网络开发的大型科技公司。公司自成立以来经过长期发展,取得了80多项国家专利!" + "\r\n" +  
  21. @"本公司为一家集软件开发、传统网络开发以及无线网络开发的大型科技公司。公司自成立以来经过长期发展,取得了80多项国家专利!" + "\r\n" +  
  22. @"本公司为一家集软件开发、传统网络开发以及无线网络开发的大型科技公司。公司自成立以来经过长期发展,取得了80多项国家专利!" + "\r\n" +  
  23. @"本公司为一家集软件开发、传统网络开发以及无线网络开发的大型科技公司。公司自成立以来经过长期发展,取得了80多项国家专利!";  
  24.             Label2.Text = @"欢迎有志之士加盟本公司!  
  25.         软件开发工程师  
  26.         要求:   
  27.         1、热爱软件行业;   
  28.         2、熟悉、net框架,能熟练使用C#和JavaScript进行编码;   
  29.         3、熟悉SQL Server等常用的数据库;   
  30.         4、有良好的编码规范习惯;   
  31.         5、做事认真负责,服从公司的工作安排;   
  32.         6、吃苦耐劳,敬业。 ";  
  33.         }  
  34.   
  35.         protected void btnIntroduction_Click(object sender, EventArgs e)  
  36.         {  
  37.             //获取被触发的Button对象  
  38.             Button b = (Button)sender;  
  39.             if (b.ID == "btnIntroduction")  
  40.             {  
  41.                 //激活View1  
  42.                 MultiView1.SetActiveView(View1);  
  43.             }  
  44.             else  
  45.             {  
  46.                 //激活View2  
  47.                 MultiView1.SetActiveView(View2);  
  48.             }  
  49.         }  
  50.     }  
  51. }  
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace WebApplication3
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Label1.Text = @"本公司为一家集软件开发、传统网络开发以及无线网络开发的大型科技公司。公司自成立以来经过长期发展,取得了80多项国家专利!" + "\r\n" +
@"本公司为一家集软件开发、传统网络开发以及无线网络开发的大型科技公司。公司自成立以来经过长期发展,取得了80多项国家专利!" + "\r\n" +
@"本公司为一家集软件开发、传统网络开发以及无线网络开发的大型科技公司。公司自成立以来经过长期发展,取得了80多项国家专利!" + "\r\n" +
@"本公司为一家集软件开发、传统网络开发以及无线网络开发的大型科技公司。公司自成立以来经过长期发展,取得了80多项国家专利!";
            Label2.Text = @"欢迎有志之士加盟本公司!
        软件开发工程师
        要求: 
        1、热爱软件行业; 
        2、熟悉、net框架,能熟练使用C#和JavaScript进行编码; 
        3、熟悉SQL Server等常用的数据库; 
        4、有良好的编码规范习惯; 
        5、做事认真负责,服从公司的工作安排; 
        6、吃苦耐劳,敬业。 ";
        }

        protected void btnIntroduction_Click(object sender, EventArgs e)
        {
            //获取被触发的Button对象
            Button b = (Button)sender;
            if (b.ID == "btnIntroduction")
            {
                //激活View1
                MultiView1.SetActiveView(View1);
            }
            else
            {
                //激活View2
                MultiView1.SetActiveView(View2);
            }
        }
    }
}

浏览效果:

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值