使用DataBound控件和状态的子定义控件

 

 1 None.gif using  System;
 2 None.gif using  System.Web.UI;
 3 None.gif using  System.Web.UI.WebControls;
 4 None.gif using  System.ComponentModel;
 5 None.gif using  System.Collections;
 6 None.gif
 7 None.gif namespace  myControls
 8 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 9InBlock.gif
10InBlock.gif    public class DataBoundState : System.Web.UI.WebControls.WebControl
11ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
12InBlock.gif        private IEnumerable _dataSource;
13InBlock.gif        private ITemplate _itemTemplate;
14InBlock.gif
15InBlock.gif        public IEnumerable DataSource
16ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
17ExpandedSubBlockStart.gifContractedSubBlock.gif            getdot.gifreturn _dataSource; }
18ExpandedSubBlockStart.gifContractedSubBlock.gif            setdot.gif{ _dataSource = value; }
19ExpandedSubBlockEnd.gif        }

20InBlock.gif
21InBlock.gif        //如果不加在页面应用此控件在绑定时会提示“System.Web.UI.Control 并不包含对 DataItem”的定义的错误
22InBlock.gif        [TemplateContainer(typeof(DataBoundStateItem))]
23InBlock.gif        public ITemplate ItemTemplate
24ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
25ExpandedSubBlockStart.gifContractedSubBlock.gif            getdot.gifreturn _itemTemplate; }
26ExpandedSubBlockStart.gifContractedSubBlock.gif            setdot.gif{ _itemTemplate = value; }
27ExpandedSubBlockEnd.gif        }

28InBlock.gif
29InBlock.gif        protected override void OnDataBinding(EventArgs e)
30ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
31InBlock.gif            IEnumerator objDataEnum;
32InBlock.gif            DataBoundStateItem objItem;
33InBlock.gif            int intCounter=0;
34InBlock.gif
35InBlock.gif            if( DataSource != null )
36ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
37InBlock.gif                Controls.Clear();
38InBlock.gif                ClearChildViewState();
39InBlock.gif                objDataEnum = DataSource.GetEnumerator();
40InBlock.gif                while( objDataEnum.MoveNext() )
41ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
42InBlock.gif                    objItem = new DataBoundStateItem( objDataEnum.Current );
43InBlock.gif                    ItemTemplate.InstantiateIn( objItem );
44InBlock.gif                    Controls.Add( objItem );
45InBlock.gif                    intCounter ++;
46ExpandedSubBlockEnd.gif                }

47InBlock.gif                ViewState["NumItems"= intCounter;
48InBlock.gif                ChildControlsCreated = true;
49ExpandedSubBlockEnd.gif            }

50ExpandedSubBlockEnd.gif        }

51InBlock.gif
52InBlock.gif        protected override void CreateChildControls()
53ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
54InBlock.gif            object objNumItems;
55InBlock.gif            int intItemCount;
56InBlock.gif//            int intCounter;
57InBlock.gif            DataBoundStateItem objItem;
58InBlock.gif
59InBlock.gif            objNumItems = ViewState["NumItems"];
60InBlock.gif            if( objNumItems != null )
61ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
62InBlock.gif                Controls.Clear();
63InBlock.gif                intItemCount = (int)objNumItems;
64InBlock.gif                for(int i=0; i<intItemCount; i++ )
65ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
66InBlock.gif                    objItem = new DataBoundStateItem( null );
67InBlock.gif                    ItemTemplate.InstantiateIn( objItem );
68InBlock.gif                    Controls.Add( objItem );
69ExpandedSubBlockEnd.gif                }

70ExpandedSubBlockEnd.gif            }

71ExpandedSubBlockEnd.gif        }

72InBlock.gif
73InBlock.gif
74ExpandedSubBlockEnd.gif    }

75InBlock.gif
76ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
77InBlock.gif    /// 模板类
78ExpandedSubBlockEnd.gif    /// </summary>

79InBlock.gif    public class DataBoundStateItem : Control, INamingContainer
80ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
81InBlock.gif        private object _DataItem;
82InBlock.gif
83InBlock.gif        public object DataItem
84ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
85ExpandedSubBlockStart.gifContractedSubBlock.gif            getdot.gifreturn _DataItem; }
86ExpandedSubBlockStart.gifContractedSubBlock.gif            setdot.gif{ _DataItem = value; }
87ExpandedSubBlockEnd.gif        }

88InBlock.gif
89InBlock.gif        public DataBoundStateItem( object DataItem )
90ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
91InBlock.gif            _DataItem = DataItem;
92ExpandedSubBlockEnd.gif        }

93ExpandedSubBlockEnd.gif    }

94InBlock.gif
95ExpandedBlockEnd.gif}

96 None.gif

转载于:https://www.cnblogs.com/zixin/archive/2006/06/06/418882.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值