原创  MasterPage的BUG 收藏

最近需要一个像TabPanel一样的Web控件,上网找了许久还是没找到合适,绝大多数都是使用AJAX实现的,AJAX虽酷,但对搜索引擎的支持并不太友好。
于是最后只得自己动手写个服务器控件。
对于写控件,感觉还是比较陌生,最多的记忆也只是一两年前写过个半成品的分页控件。之后便一直没怎么研究。。。
匆匆复习了下,写了个简陋的TabPanel,还是参考了几个门户网站上的HTML代码写的。
using System;
using System.Collections;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
namespace MyControlLibrary
{
    [ControlBuilder(
typeof(ServerTabsBuilder))]
    [ParseChildren(
false)]
    
public class ServerTabs : WebControl, INamingContainer
    
{
        
private String menuactivecss;
        
private String menunormalcss;
        
private String tabcontaincss;
        
private String menucontaincss;
        [Category(
"Style")]
        [DefaultValue(
"")]
        [Description(
"当前显示菜单样式")]
        
public String MenuActiveCss
        
{
            
get
            
{
                
               
                    
return menuactivecss;
                
            }

            
set
            
{
                menuactivecss 
= value;
            }


            
           
        }

        [Category(
"Style")]
        [DefaultValue(
"")]
        [Description(
"未激活的菜单样式")]
        
public String MenuNormalCss
        
{
            
get
            
{
                
                
                    
return menunormalcss;
               
            }

            
set
            
{
                menunormalcss 
= value;
            }

            
          
        }

        [Category(
"Style")]
        [DefaultValue(
"")]
        [Description(
"容器样式")]
        
public String TabContainCss
        
{
            
get
            
{
                
return tabcontaincss;
            }

            
set
            
{
                tabcontaincss 
= value;
            }

        }

        [Category(
"Style")]
        [DefaultValue(
"")]
        [Description(
"菜单容器样式")]
        
public String MenuContainCss
        
{
            
get
            
{
                
                    
return menucontaincss;
                
            }

            
set
            
{
                menucontaincss 
= value;
            }

           
           
        }

        
protected override void OnPreRender(EventArgs e)
        
{
            String script 
= "<script>";
            script 
+= ("var count=" + this.Controls.Count.ToString() + ";");
            script 
+= "function swichtab(tab,index){";
            script 
+= "for(i=0;i<count;i++){";
            script 
+= ("var viewtab=document.getElementById('"+this.ClientID + "panel'" +"+i);");
            script 
+= ("var viewtitle=document.getElementById('" + this.ClientID + "menu'" + "+i);");
            script 
+= "if(i==index){";
            script 
+= ("viewtitle.className='"+MenuActiveCss+"';");
            script 
+= "viewtab.style.display = 'block';";
            script 
+= "}else{";
            script 
+= "viewtitle.className='"+MenuNormalCss+"';";
            script 
+= "viewtab.style.display='none';}";
            script 
+= "};";
            script 
+= "}";
            script 
+= "</script>";
            Page.ClientScript.RegisterClientScriptBlock(
this.GetType(),"MyScript", script);
            
            
base.OnPreRender(e);
        }

        
protected override void RenderContents(HtmlTextWriter writer)
        
{
   
            writer.AddAttribute(HtmlTextWriterAttribute.Class,MenuContainCss);
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
            writer.RenderBeginTag(HtmlTextWriterTag.Ul); 
            
for (int i = 0; i < this.Controls.Count; i++)
            
{
                ServerTab tab 
= (ServerTab)this.Controls[i];
                writer.AddStyleAttribute(HtmlTextWriterStyle.Display,
"block");
                writer.AddStyleAttribute(
"float","left");
                writer.AddStyleAttribute(HtmlTextWriterStyle.Cursor, 
"pointer");
                
if (0== i)
                
{
                    writer.AddAttribute(HtmlTextWriterAttribute.Class, MenuActiveCss);
                   
                }

                
else
                
{
                writer.AddAttribute(HtmlTextWriterAttribute.Class, MenuNormalCss);
                    
                }

                writer.AddAttribute(HtmlTextWriterAttribute.Id,
this.ClientID+"menu"+i.ToString());
                writer.AddAttribute(
"onmouseover""swichtab(this,"+i.ToString()+")");
                writer.RenderBeginTag(HtmlTextWriterTag.Li);
                writer.RenderBeginTag(HtmlTextWriterTag.Span);
                writer.Write(tab.Text);
                writer.RenderEndTag();
                writer.RenderEndTag();
            }

            writer.RenderEndTag();
            writer.RenderEndTag();
            
for (int i = 0; i < this.Controls.Count; i++)
            
{
                
                writer.AddAttribute(HtmlTextWriterAttribute.Class, TabContainCss);
                
if (0 != i)
                
{
                    writer.AddStyleAttribute(HtmlTextWriterStyle.Display,
"none") ;
                }

                writer.AddAttribute(HtmlTextWriterAttribute.Id,
this.ClientID+"panel"+i.ToString() );
                writer.RenderBeginTag(HtmlTextWriterTag.Div);
                
this.Controls[i].RenderControl(writer);
                writer.RenderEndTag();
            }


   

            

        }




        
protected override HtmlTextWriterTag TagKey
        
{
            
get
            
{
                
return HtmlTextWriterTag.Div;
            }

        }

        
protected override void AddParsedSubObject(object obj)
        
{
            
if (obj is ServerTab)
                
base.AddParsedSubObject(obj);
        }

    }




    
//自定义页分析器
    public class ServerTabsBuilder : ControlBuilder
    
{
        
public override Type GetChildControlType(string tagName, IDictionary attribs)
        
{
            
if (String.Compare(tagName, "tab"true== 0)
                
return typeof(ServerTab);
            
else
                
return null;
        }

    }


    [ToolboxItem(
false)]
    
public class ServerTab : Control
    
{
        
private string _Text;

        
public string Text
        
{
            
get return _Text; }
            
set { _Text = value; }
        }

    }

}
测试页面:
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ Register Assembly="MyControlLibrary" Namespace="MyControlLibrary" TagPrefix="cc1" %>

<!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>
    
<style type="text/css">
    div
{
    line
-height: 150%;
    margin: 
0 auto;
    text
-align: left;
}


ul, li
{
    list
-style-type: none;
    margin: 0px;
    padding: 0px;
}

    .tabbox
{
        HEIGHT:205px; 
        width:480px;
        border:1px;
        }

.menu
-1
  
{
    border
-right: 1px solid #DCE4ED;
    height: 29px;
    width: 480px;
    
float: left;
}

.menu
-1 li
{
    display: block;
    text
-align: center;
    height: 30px 
!important;
    height: 27px;
    line
-height: 30px !important;
    line
-height: 27px;
    padding
-top: 3px;
    width: 160px;
    
float: left;
    cursor: pointer;
}

.menu
-1-active
{
    
float:left;
    text
-align: center;
    height: 30px 
!important;
    height: 27px;
    line
-height: 30px !important;
    line
-height: 27px;
    padding
-top: 3px;
    width: 160px;
    background: url(http:
//www.blogcn.com/images/door-bg-1.gif) no-repeat left top;
    color:#ff00ff;
}

.menu
-1-normal
{
    
float: left;
    text
-align: center;
    height: 30px 
!important;
    height: 27px;
    line
-height: 30px !important;
    line
-height: 27px;
    padding
-top: 3px;
    width: 160px;
    background: url(http:
//www.blogcn.com/images/door-bg-2.gif) no-repeat left top;
}

.menu
-2
{
    height: 170px;
    width: 479px;
    
float: left;
    border
-right-width: 1px;
    border
-bottom-width: 1px;
    border
-left-width: 1px;
    border
-right-style: solid;
    border
-bottom-style: solid;
    border
-left-style: solid;
    border
-right-color: #DCE4ED;
    border
-bottom-color: #DCE4ED;
    border
-left-color: #DCE4ED;
}

        
</style>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
    
<cc1:ServerTabs ID="ServerTabs1" runat="server"  
        Height
="205px" Width="480px" 
        TabContainCss
="menu-2" MenuActiveCss="menu-1-active" MenuNormalCss="menu-1-normal" MenuContainCss="menu-1" 
        
><cc1:ServerTab runat="server" Text="First Tab">
        
<asp:Label runat="server" Text="Label" ID="Label2"></asp:Label>
          Contents of the first tab
          
<asp:TextBox runat="server" ID="TextBox1"></asp:TextBox>
        
</cc1:ServerTab>
        
<cc1:ServerTab runat="server" Text="Second Tab">
          Contents of the second tab
        
</cc1:ServerTab>
        
<cc1:ServerTab runat="server" Text="Third Tab">
          Contents of the third tab
        
</cc1:ServerTab>
        
</cc1:ServerTabs>
        
</div>
    
</form>
</body>
</html>
样式用的是BLGCN门户上的样式,呵呵,不要告我盗版,只为学习。
效果图:

接着问题就来了,当在MasterPage中使用该控件的时候JS无法输出,控件ID也会被自动填上一个前缀。
一句话就是几个小时的心血白费了
MasterPage我所了解的BUG有:有回调事件的控件将无法使用。
还有个很不爽的BUG,ASP.net的menu控件在MasterPage中使用的时候在IE6下浏览会错位,上次也因为这个问题导致页面重构。不知道这该算IE6的BUG还是MasterPage的BUG。
两个字:不爽。
网上有人说可以使用一个叫WilsonMasterPages的控件代替Maserpage,没用过,也不知道怎么样。

发表于 @ 2007年05月20日 02:26:00 | 评论( loading... ) | 编辑| 举报| 收藏

旧一篇:关于NHibernate1.2延迟加载及InvalidProxyTypeException异常 | 新一篇:Click FrameWork框架初体验

  • 发表评论
  • 评论内容:
  •  
Copyright © lulustray
Powered by CSDN Blog