2011年9月20日

ViewStateAutoManager

using System;
using System.Web.UI;
using System.Reflection;
using System.Collections.Generic;
using System.Collections;

namespace My.Common
{
    [AttributeUsage(AttributeTargets.Property)]
    public class ViewStateAttribute : Attribute
    {
    }

    public class BasePage : Page
    {
        protected override object SaveViewState()
        {
            ViewStateAutoManager mgr = new ViewStateAutoManager(this);
            var v = base.SaveViewState();
            return mgr.SaveViewState(v);
        }

        protected override void LoadViewState(object savedState)
        {
            ViewStateAutoManager mgr = new ViewStateAutoManager(this);
            var v = mgr.LoadViewState(savedState);
            base.LoadViewState(v);
        }
    }

    public class BasePart : UserControl
    {
        protected override object SaveViewState()
        {
            ViewStateAutoManager mgr = new ViewStateAutoManager(this);
            var v = base.SaveViewState();
            return mgr.SaveViewState(v);
        }

        protected override void LoadViewState(object savedState)
        {
            ViewStateAutoManager mgr = new ViewStateAutoManager(this);
            var v = mgr.LoadViewState(savedState);
            base.LoadViewState(v);
        }
    }

    public class ViewStateAutoManager
    {
        private static Dictionary<Type, List<PropertyInfo>> _cache = new Dictionary<Type, List<PropertyInfo>>();
        private Control _ctl;

        public ViewStateAutoManager(Control ctl)
        {
            this._ctl = ctl;
        }

        private List<PropertyInfo> GetViewStateProperties(Control ctl)
        {
            var targetType = ctl.GetType();

            if (!_cache.ContainsKey(targetType))
            {
                var pros = targetType.GetProperties(
                    BindingFlags.Public
                    | BindingFlags.NonPublic
                    | BindingFlags.Instance
                    );

                var list = new List<PropertyInfo>();

                foreach (var p in pros)
                {
                    var attr = Attribute.GetCustomAttribute(p, typeof(ViewStateAttribute));
                    if (attr != null)
                    {
                        list.Add(p);
                    }
                }

                _cache[targetType] = list;
            }

            return _cache[targetType];
        }

        private ViewStateList GetViewState()
        {
            var data = new ViewStateList();
            var pros = GetViewStateProperties(_ctl);

            foreach (var p in pros)
            {
                var value = p.GetValue(_ctl, null);
                data.Add(p.Name, value);
            }

            return data;
        }

        public object SaveViewState(object parentViewState)
        {
            var my = GetViewState();
            return new Pair(parentViewState, my);
        }

        private void SetViewState(object viewState)
        {
            var data = viewState as ViewStateList;
            var pros = GetViewStateProperties(_ctl);

            foreach (var p in pros)
            {
                p.SetValue(_ctl, data[p.Name], null);
            }
        }

        public object LoadViewState(object parentViewState)
        {
            var pair = parentViewState as Pair;
            SetViewState(pair.Second);
            return pair.First;
        }
    }

    [Serializable]
    public class ViewStateList : IEnumerable<ViewStateList._Item>
    {
        [Serializable]
        public class _Item
        {
            public string Key;
            public object Value;
            public _Item Next;
        }

        private _Item First;

        public void Add(string key, object value)
        {
            var newItem = new _Item { Key = key, Value = value };

            if (First == null)
            {
                First = newItem;
            }
            else
            {
                _Item last = First;
                while (last.Next != null)
                {
                    last = last.Next;
                }
                last.Next = newItem;
            }
        }

        public object this[string key]
        {
            get
            {
                foreach (var p in this)
                {
                    if (p.Key == key)
                    {
                        return p.Value;
                    }
                }

                return string.Empty;
            }
        }

        #region IEnumerable<_Item> Members

        public IEnumerator<ViewStateList._Item> GetEnumerator()
        {
            for (var p = First; p != null; p = p.Next)
            {
                yield return p;
            }
        }

        #endregion

        #region IEnumerable Members

        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }

        #endregion
    }
}

posted @ 2011-09-20 13:43 shcity 阅读(10) 评论(0) 编辑

2011年5月20日

ReportingService formatting

 

Standard Numeric Format Strings

http://msdn.microsoft.com/en-us/library/dwhawy9k(VS.95).aspx

 

Custom Numeric Format Strings

http://msdn.microsoft.com/en-us/library/0c899ak8(VS.95).aspx

 

 Date Formatting in Reporting Services – list of format codes

http://thavash.wordpress.com/2007/10/02/date-formatting-in-reporting-services-list-of-format-codes/

posted @ 2011-05-20 12:35 shcity 阅读(14) 评论(0) 编辑

2011年3月30日

Build my own DataTable

public class MyTable : List<MyRow>, ITypedList
    {
        
public MyTable()
        {
            
for (int i = 0; i < 5; i++)
            {
                MyRow ht 
= new MyRow();
                ht.Table 
= this;
                ht.Add(
"Name""name" + i);
                ht.Add(
"Age", i + 10);
                ht.Add(
"Address""add:" + i);
                
this.Add(ht);
            }
        }

        
#region ITypedList Members

        
public PropertyDescriptorCollection GetItemProperties(PropertyDescriptor[] listAccessors)
        {
            List
<MyPropertyDescriptor> list = new List<MyPropertyDescriptor>();
            list.Add(
new MyPropertyDescriptor("Name"typeof(string)));
            list.Add(
new MyPropertyDescriptor("Age"typeof(int)));
            list.Add(
new MyPropertyDescriptor("Address"typeof(string)));
            
return new PropertyDescriptorCollection(list.ToArray());
        }

        
public string GetListName(PropertyDescriptor[] listAccessors)
        {
            
return string.Empty;
        }

        
#endregion
    }

    
public class MyRow : Hashtable, ICustomTypeDescriptor
    {
        
public MyTable Table { getset; }

        
#region ICustomTypeDescriptor Members

        
public System.ComponentModel.AttributeCollection GetAttributes()
        {
            
return new System.ComponentModel.AttributeCollection(null);
        }

        
public string GetClassName()
        {
            
return null;
        }

        
public string GetComponentName()
        {
            
return null;
        }

        
public TypeConverter GetConverter()
        {
            
return null;
        }

        
public EventDescriptor GetDefaultEvent()
        {
            
return null;
        }

        
public PropertyDescriptor GetDefaultProperty()
        {
            
return null;
        }

        
public object GetEditor(Type editorBaseType)
        {
            
return null;
        }

        
public EventDescriptorCollection GetEvents(Attribute[] attributes)
        {
            
return new EventDescriptorCollection(null);
        }

        
public EventDescriptorCollection GetEvents()
        {
            
return new EventDescriptorCollection(null);
        }

        
public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
        {
            
return this.Table.GetItemProperties(null);
        }

        
public PropertyDescriptorCollection GetProperties()
        {
            
return ((ICustomTypeDescriptor)this).GetProperties(null);
        }

        
public object GetPropertyOwner(PropertyDescriptor pd)
        {
            
return this;
        }

        
#endregion
    }

    
public class MyPropertyDescriptor : PropertyDescriptor
    {
        
private string _field;
        
private Type _propertyType;

        
public MyPropertyDescriptor(string name, Type propertyType)
            : 
base(name, null)
        {
            _field 
= name;
            _propertyType 
= propertyType;
        }

        
public override bool CanResetValue(object component)
        {
            
return false;
        }

        
public override bool Equals(object other)
        {
            
if (other is MyPropertyDescriptor)
            {
                MyPropertyDescriptor descriptor 
= (MyPropertyDescriptor)other;
                
return (descriptor._field == this._field);
            }
            
return false;
        }

        
public override int GetHashCode()
        {
            
return _field.GetHashCode();
        }

        
public override object GetValue(object component)
        {
            MyRow view 
= (MyRow)component;
            
return view[_field];
        }

        
public override void ResetValue(object component)
        {
            ((MyRow)component)[_field] 
= null;
        }

        
public override void SetValue(object component, object value)
        {
            ((MyRow)component)[_field] 
= value;
            
this.OnValueChanged(component, EventArgs.Empty);
        }

        
public override bool ShouldSerializeValue(object component)
        {
            
return false;
        }

        
public override Type ComponentType
        {
            
get
            {
                
return typeof(MyRow);
            }
        }

        
public override bool IsBrowsable
        {
            
get
            {
                
return base.IsBrowsable;
            }
        }

        
public override bool IsReadOnly
        {
            
get
            {
                
return true;
            }
        }

        
public override Type PropertyType
        {
            
get
            {
                
return _propertyType;
            }
        }
    }

posted @ 2011-03-30 13:24 shcity 阅读(9) 评论(0) 编辑

2011年1月25日

split a string into an array through comma

string input = @"a\,c,ba";
string[] arr = Regex.Split(input, @"(?<!\\),");

 

posted @ 2011-01-25 14:07 shcity 阅读(22) 评论(0) 编辑

2011年1月24日

div with separated html template

代码
<!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>
    
<title></title>
    
<script>
        
function setSize(div, innerDiv) {
            
var doc = document.documentElement;
            
var sw = parseFloat(doc.scrollWidth);
            
var cw = parseFloat(doc.clientWidth);
            
var sh = parseFloat(doc.scrollHeight);
            
var ch = parseFloat(doc.clientHeight);

            div.style.width 
= sw >= cw ? sw : cw;
            div.style.height 
= sh >= ch ? sh : ch;

            innerDiv.style.display 
= '';
            innerDiv.style.left 
= parseFloat(doc.scrollLeft) + cw / 2 - parseFloat(innerDiv.clientWidth) / 2;
            innerDiv.style.top 
= parseFloat(doc.scrollTop) + ch / 2 - parseFloat(innerDiv.clientHeight) / 2;

        }

        
function openNewWindow() {
            
var div = document.getElementById("container");
            
var innerDiv = document.getElementById("content");
            setSize(div, innerDiv);

            window.attachEvent(
"onresize"function() { setSize(div, innerDiv) });
            window.attachEvent(
"onscroll"function() { setSize(div, innerDiv) }); 

        }

        
function closeWindow() {
            
var div = document.getElementById("container");
            
var innerDiv = document.getElementById("content");
            div.style.width 
= 0;
            div.style.height 
= 0;
            innerDiv.style.display 
= 'none';
        }
        
        
function Button1_onclick() {
            openNewWindow();
        }

      

    
</script>
</head>
<body>
    
<div id="container" style="filter: alpha(opacity=40); position: absolute; top: 0px;
        left: 0px; z-index: 9998; background: black"
>
    
</div>
    
<div id="content" style="height: 200px; width: 300px; position: absolute;
        display: none; z-index: 9999;"
>
        
<div style="text-align: center; background-color: Gray;border:solid 1px gray">
            
<iframe style="border-style: none; width: 100%; height: 100%;" frameborder="0">
            
</iframe>            
            
<button onclick="closeWindow();">
                close
</button>
        
</div>
    
</div>
    
<p>
        
<input id="Button1" type="button" value="button" onclick="return Button1_onclick()" /></p>
</body>
</html>

 

posted @ 2011-01-24 17:22 shcity 阅读(21) 评论(0) 编辑

2010年12月13日

正则表达式替换日期

摘要: [代码]阅读全文

posted @ 2010-12-13 13:24 shcity 阅读(26) 评论(0) 编辑

2007年12月18日

半透明的div对话框

posted @ 2007-12-18 19:03 shcity 阅读(1816) 评论(0) 编辑

2007年12月5日

foreach 的自动转化类型

posted @ 2007-12-05 16:55 shcity 阅读(58) 评论(0) 编辑

2007年5月27日

在Ajax1.0中调用页面CS文件中的方法

posted @ 2007-05-27 17:39 shcity 阅读(298) 评论(2) 编辑

导航

<2012年2月>
2930311234
567891011
12131415161718
19202122232425
26272829123
45678910

公告

昵称:shcity
园龄:5年4个月
粉丝:0
关注:1

搜索

 
 

常用链接

随笔分类

随笔档案

文章分类

.net-links

AJAX-links

Asp.net - links

blogs

C - links

download

EF - links

Ent Lib -links

Exchange Server

JAVA-links

Linux-links

moss-links

others

Perl-links

SilverLight-links

SQL - links

SSAS - links

SSRS-links

Threads-links

VSS

WF-links

wpf-links

最新评论

阅读排行榜

评论排行榜

推荐排行榜