自定义控件开发3---集合数据绑定控件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.ObjectModel;
using System.Globalization;
using System.ComponentModel;
using System.Collections;
namespace MyTestProj
{
    [ParseChildren(
true"Items")]
    
public class HyperListControls : DataBoundControl, INamingContainer, IRepeatInfoUser
    {
        
public string DataUrlField
        {
            
get
            {
                
return ViewState["DataUrlField"== null ? "" : Convert.ToString(ViewState["DataUrlField"], CultureInfo.CurrentCulture);
            }
            
set { ViewState["DataUrlField"= value; }
        }
        
public string DataTextField
        {
            
get
            {
                
return ViewState["DataTextField"== null ? "" : Convert.ToString(ViewState["DataTextField"], CultureInfo.CurrentCulture);
            }
            
set { ViewState["DataTextField"= value; }
        }
        
public string DataTipField
        {
            
get
            {
                
return ViewState["DataTipField"== null ? "" : Convert.ToString(ViewState["DataTipField"], CultureInfo.CurrentCulture);
            }
            
set { ViewState["DataTipField"= value; }
        }
        
private MyHyperCollection _items;
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        
public MyHyperCollection Items
        {
            
get
            {
                
if (_items == null)
                {
                    _items 
= new MyHyperCollection();
                    
if (base.IsTrackingViewState)
                        _items.TrackViewState();
                }
                
return _items;
            }
        }

        
protected override void Render(HtmlTextWriter writer)
        {
            
if (Items.Count > 0)
            {
                RepeatInfo ri 
= new RepeatInfo();
                Style controlStyle 
= (base.ControlStyleCreated ? base.ControlStyle : null);
                ri.RepeatColumns 
= RepeatColumns;
                ri.RepeatDirection 
= RepeatDirection;
                ri.RepeatLayout 
= RepeatLayout;
                ri.RenderRepeater(writer, 
this, controlStyle, this);
            }
        }

        
// ***************************************************************************************************
        
// METHOD CreateControlStyle 
        
// Create the style object to apply to the control
        protected override Style CreateControlStyle()
        {
            
return new TableStyle(this.ViewState);
        }


        
#region PerformDataBinding override
        
protected override void PerformDataBinding(IEnumerable dataSource)
        {
            
base.PerformDataBinding(dataSource);

            
string urlField = DataUrlField;
            
string textField = DataTextField;
            
string tooltipField = DataTipField;

            
if (dataSource != null)
            {
                
// Fill Items
                Items.Clear();
                
foreach (object o in dataSource)
                {
                    MyHyperItem item 
= new MyHyperItem();
                    
if (DesignMode)
                    {
                        item.Src 
= "DataBound";
                        item.Text 
= "DataBound";
                    }
                    
else
                    {
                        item.Src 
= DataBinder.GetPropertyValue(o, urlField, null);
                        item.Text 
= DataBinder.GetPropertyValue(o, textField, null);
                        item.Tip 
= DataBinder.GetPropertyValue(o, tooltipField, null);
                    }
                    Items.Add(item);
                }
            }
        }

        
protected override void LoadViewState(object savedState)
        {
            
if (savedState != null)
            {
                Pair p 
= savedState as Pair;
                
base.LoadViewState(p.First);
                Items.LoadViewState(p.Second);
            }
            
else
            {
                
base.LoadViewState(null);
            }
        }
        
protected override object  SaveViewState()
        {
            
object baseObj = base.SaveViewState();
            
object itemObj = Items.SaveViewState();
            
if ((baseObj == null&& (itemObj == null))
            {
                
return null;
            }
            
return new Pair(baseObj, itemObj);
        }
        
private HyperLink _controlToRepeat;
        
private HyperLink ControlToRepeat
        {
            
get
            {
                
if (_controlToRepeat == null)
                {
                    _controlToRepeat 
= new HyperLink();
                    Controls.Add(_controlToRepeat);
                }
                
return _controlToRepeat;
            }
        }
        
public virtual RepeatDirection RepeatDirection
        {
            
get
            {
                
object o = ViewState["RepeatDirection"];
                
if (o != null)
                    
return (RepeatDirection)o;
                
return RepeatDirection.Vertical;
            }
            
set
            {
                ViewState[
"RepeatDirection"= value;
            }
        }

        
// ***************************************************************************************************
        
// PROPERTY RepeatColumns 
        
// Gets and sets the number of columns to render out 
        public virtual int RepeatColumns
        {
            
get
            {
                
object o = ViewState["RepeatColumns"];
                
if (o != null)
                    
return (int)o;
                
return 0;
            }
            
set
            {
                ViewState[
"RepeatColumns"= value;
            }
        }

        
// ***************************************************************************************************
        
// PROPERTY RepeatLayout 
        
// Gets and sets the expected layout for the control's output (flow or table) 
        public virtual RepeatLayout RepeatLayout
        {
            
get
            {
                
object o = ViewState["RepeatLayout"];
                
if (o != null)
                    
return (RepeatLayout)o;
                
return RepeatLayout.Table;
            }
            
set
            {
                ViewState[
"RepeatLayout"= value;
            }
        }

        
public Style GetItemStyle(ListItemType itemType, int repeatIndex)
        {
            
return null;
        }

        
public bool HasFooter
        {
            
get { return false; }
        }

        
public bool HasHeader
        {
            
get { return false; }
        }

        
public bool HasSeparators
        {
            
get { return false; }
        }

        
public void RenderItem(ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)
        {
            HyperLink ctl 
= ControlToRepeat;
            
int i = repeatIndex;
            ctl.ID 
= i.ToString();
            ctl.Text 
= Items[i].Text;
            ctl.NavigateUrl 
= Items[i].Src;
            ctl.ToolTip 
= Items[i].Tip;
            ctl.RenderControl(writer);
        }

        
public int RepeatedItemCount
        {
            
get { return Items.Count; }
        }

        
#endregion
    }
    
public class MyHyperItem
    {
        
private string _text;
        
private string _src;
        
private string _tip;
        
public MyHyperItem() { }
        
public MyHyperItem(string Text, string Src, string Tip)
        {
            _text 
= Text;
            _src 
= Src;
            _tip 
= Tip;
        }
        
public string Text
        {
            
get { return _text; }
            
set { _text = value; }
        }
        
public string Src
        {
            
get { return _src; }
            
set { _src = value; }
        }
        
public string Tip
        {
            
get { return _tip; }
            
set { _tip = value; }
        }
    }
    
public class MyHyperCollection : Collection<MyHyperItem>,IStateManager
    {
        
private bool _mark;
        
#region IStateManager Members

        
public bool IsTrackingViewState
        {
            
get { return _mark; }
        }

        
public void LoadViewState(object state)
        {
            
//if (state == null)
            
//    return;
            if (state != null)
            {
                Clear();
                Triplet t 
= state as Triplet;
                
string[] rgTip = t.First as string[];
                
string[] rgText = t.Second as string[];
                
string[] rgSrc = t.Third as string[];
                
for (int i = 0; i < rgSrc.Length; i++)
                {
                    Add(
new MyHyperItem(rgText[i], rgSrc[i], rgTip[i]));
                }
            }
        }

        
public object SaveViewState()
        {
            
int nums = Count;
            
object[] rgTip = new string[nums];
            
object[] rgText = new string[nums];
            
object[] rgSrc = new string[nums];
            
for (int i = 0; i < nums; i++)
            {
                rgTip[i] 
= this[i].Tip;
                rgText[i] 
= this[i].Text;
                rgSrc[i] 
= this[i].Src;
            }
            
return new Triplet(rgSrc, rgText, rgTip);
        }

        
public void TrackViewState()
        {
            _mark 
= true;
        }

        
#endregion
    }
}
DownLoad

posted on 2009-11-22 17:34  博览潇湘  阅读(444)  评论(0)    收藏  举报

导航