基于ASP.NET的JQueryUI控件开发(4) - JQTab和JQAccordion

来源:互联网 发布:android电商项目源码 编辑:程序博客网 时间:2024/06/05 10:48

先上图:

设计思路:

参照ASP.NET控件MultiView的使用方法,在MultiView中可以放多个View

Code:
  1.  [Designer("System.Web.UI.Design.WebControls.MultiViewDesigner, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]   
  2.  public class JQTabPanel : Panel   
  3.  {   
  4.      string headerTitle;   
  5.      //标题   
  6.      public string HeaderTitle   
  7.      {   
  8.          get  
  9.          {   
  10.              if (string.IsNullOrEmpty(headerTitle))   
  11.              {   
  12.                  if (!string.IsNullOrEmpty(ToolTip))   
  13.                      return ToolTip;   
  14.                  return this.ClientID;   
  15.              }   
  16.              return headerTitle;   
  17.          }   
  18.          set { headerTitle = value; }   
  19.      }   
  20.  }   
  21.   
  22.     
  23.  [ParseChildren(typeof(JQTabPanel)),   
  24.  Designer("System.Web.UI.Design.WebControls.MultiViewDesigner, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"),   
  25. AspNetHostingPermission(SecurityAction.LinkDemand,   
  26. Level = AspNetHostingPermissionLevel.Minimal),   
  27. AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]   
  28.  public class JQTab:Panel   
  29.  {      
  30.      //增加子选项卡   
  31.      protected override void AddParsedSubObject(object obj)   
  32.      {   
  33.          if (obj is JQTabPanel)   
  34.          {   
  35.              this.Controls.Add(obj as Control);   
  36.          }   
  37.          else  
  38.          {   
  39.              if (!(obj is LiteralControl))   
  40.                  throw new Exception("只能使用JQTabPanel控件!");   
  41.          }   
  42.      }   
  43.      //呈现之前处理   
  44.      protected override void OnPreRender(EventArgs e)   
  45.      {   
  46.          base.OnPreRender(e);   
  47.          StringBuilder sb = new StringBuilder();   
  48.          sb.Append("<ul>");   
  49.          for (int i = 0; i < this.Controls.Count; i++)   
  50.          {   
  51.              if (this.Controls[i] is JQTabPanel)   
  52.              {   
  53.                  var tp = this.Controls[i] as JQTabPanel;   
  54.                  sb.AppendFormat("<li><a href='#{0}'>{1}</a></li>",tp.ClientID,tp.HeaderTitle);   
  55.              }   
  56.          }   
  57.          sb.Append("</ul>");   
  58.          Literal lit = new Literal();   
  59.          lit.Text = sb.ToString();   
  60.          this.Controls.AddAt(0, lit);   
  61.      }   
  62.      public override void RenderEndTag(HtmlTextWriter writer)   
  63.      {   
  64.          base.RenderEndTag(writer);   
  65.   
  66.          string config = "";              
  67.          if (config.Length > 0)   
  68.          {   
  69.              config = config.Substring(0, config.Length - 1);   
  70.              config = "{" + config + "}";   
  71.          }   
  72.          writer.Write("/n<script>$('#" + this.ClientID + "').tabs(" + config + ");</script>/n");   
  73.      }   
  74.  }  

两个类,一个是Tab和一个TabPanel

使用时ASP.NET的标记如下:

Code:
  1. <cc1:JQTab ID="JQTab1" runat="server">  
  2.     <cc1:JQTabPanel ID="JQTabPanel1" runat="server" HeaderTitle="选项卡一">  
  3.         选项卡一<br />  
  4.         选项卡一<br />  
  5.         选项卡一<br />  
  6.         选项卡一<br />  
  7.         选项卡一<br />  
  8.         选项卡一   
  9.     </cc1:JQTabPanel>  
  10.     <cc1:JQTabPanel ID="JQTabPanel2" runat="server" HeaderTitle="选项卡二">  
  11.         选项卡二<br />  
  12.         选项卡二<br />  
  13.         选项卡二<br />  
  14.         选项卡二<br />  
  15.         选项卡二<br />  
  16.         选项卡二   
  17.     </cc1:JQTabPanel>  
  18.     <cc1:JQTabPanel ID="JQTabPanel3" runat="server" HeaderTitle="选项卡三">  
  19.         选项卡三<br />  
  20.         选项卡三<br />  
  21.         选项卡三<br />  
  22.         选项卡三<br />  
  23.         选项卡三<br />  
  24.         选项卡三   
  25.     </cc1:JQTabPanel>  
  26. </cc1:JQTab>  

生成的客户端HTML如下:

Code:
  1.         <div id="JQTab1">  
  2.     <ul><li><a href='#JQTabPanel1'>选项卡一</a></li><li><a href='#JQTabPanel2'>选项卡二</a></li><li><a href='#JQTabPanel3'>选项卡三</a></li></ul><div id="JQTabPanel1">  
  3.            
  4.                 选项卡一<br />  
  5.                 选项卡一<br />  
  6.                 选项卡一<br />  
  7.                 选项卡一<br />  
  8.                 选项卡一<br />  
  9.                 选项卡一   
  10.                
  11.     </div><div id="JQTabPanel2">  
  12.            
  13.                 选项卡二<br />  
  14.                 选项卡二<br />  
  15.                 选项卡二<br />  
  16.                 选项卡二<br />  
  17.                 选项卡二<br />  
  18.                 选项卡二   
  19.                
  20.     </div><div id="JQTabPanel3">  
  21.            
  22.                 选项卡三<br />  
  23.                 选项卡三<br />  
  24.                 选项卡三<br />  
  25.                 选项卡三<br />  
  26.                 选项卡三<br />  
  27.                 选项卡三   
  28.                
  29.     </div>  
  30. </div>  
  31. <script>$('#JQTab1').tabs();</script>  

即可实现!

相同道理即可以实现JQAccordion功能

效果图:

ASP.NET界面效果:

 源代码和Tab类似,如下:有两个类一个是JQAccordion一个是JQAccordionPanel

Code:
  1. [Designer("System.Web.UI.Design.WebControls.MultiViewDesigner, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]   
  2. public class JQAccordionPanel : Panel   
  3. {   
  4.     private string title;   
  5.   
  6.     public string Title   
  7.     {   
  8.         get { return title; }   
  9.         set { title = value; }   
  10.     }   
  11.     public override void RenderBeginTag(HtmlTextWriter writer)   
  12.     {   
  13.         if (string.IsNullOrEmpty(title)) title = this.ClientID;   
  14.         writer.Write("/n<h3><a href=/"#/">" + title + "</a></h3>/n");   
  15.         base.RenderBeginTag(writer);   
  16.     }   
  17.     protected override void OnPreRender(EventArgs e)   
  18.     {   
  19.         base.OnPreRender(e);   
  20.     }   
  21. }   
  22.   
  23. [ParseChildren(typeof(JQAccordionPanel)),   
  24.  Designer("System.Web.UI.Design.WebControls.MultiViewDesigner, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"),    
  25. AspNetHostingPermission(SecurityAction.LinkDemand,    
  26. Level = AspNetHostingPermissionLevel.Minimal),    
  27. AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]   
  28. public class JQAccordion:Panel   
  29. {   
  30.   
  31.     private int activeIndex = 0;   
  32.   
  33.     public int ActiveIndex   
  34.     {   
  35.         get { return activeIndex; }   
  36.         set { activeIndex = value; }   
  37.     }   
  38.   
  39.     protected override void AddParsedSubObject(object obj)   
  40.     {   
  41.         if (obj is JQAccordionPanel)   
  42.         {   
  43.             this.Controls.Add(obj as Control);   
  44.         }   
  45.         else  
  46.         {   
  47.             if(!(obj is LiteralControl))   
  48.                 throw new Exception("只能使用JQAccordionPanel控件!");   
  49.         }   
  50.     }   
  51.     private Effect animated=Effect.None;   
  52.   
  53.     public Effect Animated   
  54.     {   
  55.         get { return animated; }   
  56.         set { animated = value; }   
  57.     }   
  58.   
  59.     private bool collapsible;   
  60.   
  61.   
  62.     public bool Collapsible   
  63.     {   
  64.         get { return collapsible; }   
  65.         set { collapsible = value; }   
  66.     }   
  67.   
  68.     public override void RenderEndTag(HtmlTextWriter writer)   
  69.     {   
  70.         base.RenderEndTag(writer);   
  71.   
  72.         string config = "";   
  73.         if (animated != Effect.None)   
  74.         {   
  75.             config += "animated:'" + animated.ToString().ToLower() + "',";   
  76.         }   
  77.         config += "active:" + activeIndex+",";   
  78.         config += "collapsible:" + collapsible.ToString().ToLower() + ",";   
  79.           
  80.         if (config.Length > 0)   
  81.         {   
  82.             config = config.Substring(0, config.Length - 1);   
  83.             config = "{" + config + "}";   
  84.         }   
  85.   
  86.         writer.Write("/n<script>$('#"+this.ClientID+"').accordion("+config+");</script>/n");   
  87.     }   
  88. }   

ASP.NET使用代码:

Code:
  1. <cc1:JQAccordion ID="JQAccordion1" runat="server">  
  2.     <cc1:JQAccordionPanel ID="JQAccordionPanel1" runat="server" Title="测试一">  
  3.         测试一<br />  
  4.         测试一<br />  
  5.         测试一   
  6.     </cc1:JQAccordionPanel>  
  7.     <cc1:JQAccordionPanel ID="JQAccordionPanel2" runat="server" Title="测试二">  
  8.         测试二<br />  
  9.         测试二<br />  
  10.         测试二   
  11.     </cc1:JQAccordionPanel>  
  12.     <cc1:JQAccordionPanel ID="JQAccordionPanel3" runat="server" Title="测试三">  
  13.         测试三<br />  
  14.         测试三<br />  
  15.         测试三   
  16.     </cc1:JQAccordionPanel>  
  17. </cc1:JQAccordion>  

生成客户端HTML代码:

Code:
  1.         <div id="JQAccordion1">  
  2.        
  3. <h3><a href="#">测试一</a></h3>  
  4. <div id="JQAccordionPanel1">  
  5.            
  6.                 测试一<br />  
  7.                 测试一<br />  
  8.                 测试一   
  9.                
  10.     </div>  
  11. <h3><a href="#">测试二</a></h3>  
  12. <div id="JQAccordionPanel2">  
  13.            
  14.                 测试二<br />  
  15.                 测试二<br />  
  16.                 测试二   
  17.                
  18.     </div>  
  19. <h3><a href="#">测试三</a></h3>  
  20. <div id="JQAccordionPanel3">  
  21.            
  22.                 测试三<br />  
  23.                 测试三<br />  
  24.                 测试三   
  25.                
  26.     </div>  
  27. </div>  
  28. <script>$('#JQAccordion1').accordion({active:0,collapsible:false});</script>  
  29.     
  30.     

完工,待续... 

原创粉丝点击