2 // System.Web.UI.WebControls.BaseDataList.cs
5 // Gaurav Vaish (gvaish@iitk.ac.in)
6 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
8 // (C) Gaurav Vaish (2001)
9 // (C) 2003 Andreas Nahr
13 using System.ComponentModel;
14 using System.ComponentModel.Design;
\r
15 using System.Collections;
\r
18 using System.Web.Util;
\r
20 namespace System.Web.UI.WebControls
\r
22 [DefaultEvent("SelectedIndexChanged")]
\r
23 [DefaultProperty("DataSource")]
\r
24 [Designer("System.Web.UI.Design.WebControls.BaseDataListDesigner, " + Consts.AssemblySystem_Design, typeof (IDesigner))]
\r
25 public abstract class BaseDataList: WebControl
\r
27 private static readonly object SelectedIndexChangedEvent = new object();
\r
28 internal static string ItemCountViewStateKey = "_!ItemCount";
\r
30 private DataKeyCollection dataKeys;
\r
31 private object dataSource;
\r
33 public BaseDataList() : base()
\r
37 public static bool IsBindableType(Type type)
\r
39 if(type.IsPrimitive || type == typeof(string) || type == typeof(DateTime) || type == typeof(Decimal))
\r
44 public override ControlCollection Controls
\r
48 EnsureChildControls();
\r
49 return base.Controls;
\r
53 public override void DataBind()
\r
56 RequiresDataBinding = false;
58 OnDataBinding(EventArgs.Empty);
\r
61 [WebCategory("Action")]
\r
62 [WebSysDescription("BaseDataList_OnSelectedIndexChanged")]
\r
63 public event EventHandler SelectedIndexChanged
\r
67 Events.AddHandler(SelectedIndexChangedEvent, value);
\r
71 Events.RemoveHandler(SelectedIndexChangedEvent, value);
\r
77 [WebCategory("Layout")]
\r
78 [WebSysDescription("BaseDataList_CellPadding")]
\r
79 public virtual int CellPadding
\r
83 if(!ControlStyleCreated)
\r
85 return ((TableStyle)ControlStyle).CellPadding;
\r
89 ((TableStyle)ControlStyle).CellPadding = value;
\r
95 [WebCategory("Layout")]
\r
96 [WebSysDescription("BaseDataList_CellSpacing")]
\r
97 public virtual int CellSpacing
\r
101 if(!ControlStyleCreated)
\r
103 return ((TableStyle)ControlStyle).CellSpacing;
\r
107 ((TableStyle)ControlStyle).CellSpacing = value;
\r
112 [WebCategory("Data")]
\r
113 [WebSysDescription("BaseDataList_DataKeyField")]
\r
114 public virtual string DataKeyField
\r
118 object o = ViewState["DataKeyField"];
\r
121 return String.Empty;
\r
125 ViewState["DataKeyField"] = value;
\r
130 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
\r
131 [WebSysDescription("BaseDataList_DataKeys")]
\r
132 public DataKeyCollection DataKeys
\r
136 if( dataKeys==null )
\r
137 dataKeys = new DataKeyCollection(DataKeysArray);
\r
144 [WebCategory("Data")]
\r
145 [WebSysDescription("BaseDataList_DataMember")]
\r
146 public string DataMember
\r
150 object o = ViewState["DataMember"];
\r
153 return String.Empty;
\r
157 ViewState["DataMember"] = value;
\r
162 [DefaultValue(null)]
\r
163 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
\r
164 [WebCategory("Data")]
\r
165 [WebSysDescription("BaseDataList_DataSource")]
\r
166 public virtual object DataSource {
171 if (value == null || value is IListSource || value is IEnumerable) {
174 throw new ArgumentException (HttpRuntime.FormatResourceString (
175 "Invalid_DataSource_Type", ID));
181 [DefaultValue(GridLines.Both)]
\r
182 [WebCategory("Appearance")]
\r
183 [WebSysDescription("BaseDataList_GridLines")]
\r
184 public virtual GridLines GridLines
\r
188 if(ControlStyleCreated)
\r
189 return ((TableStyle)ControlStyle).GridLines;
\r
190 return GridLines.Both;
\r
194 ((TableStyle)ControlStyle).GridLines = value;
\r
198 // LAMESPEC HorizontalAlign has a Category attribute, this should obviously be a WebCategory attribute
199 // but is defined incorrectly in the MS framework
\r
202 [DefaultValue(HorizontalAlign.NotSet)]
\r
203 [Category("Layout")]
\r
204 [WebSysDescription("BaseDataList_HorizontalAlign")]
\r
205 public virtual HorizontalAlign HorizontalAlign
\r
209 if(ControlStyleCreated)
\r
210 return ((TableStyle)ControlStyle).HorizontalAlign;
\r
211 return HorizontalAlign.NotSet;
\r
215 ((TableStyle)ControlStyle).HorizontalAlign = value;
\r
219 protected ArrayList DataKeysArray
\r
223 object o = ViewState["DataKeys"];
\r
226 o = new ArrayList();
\r
227 ViewState["DataKeys"] = o;
\r
229 return (ArrayList)o;
\r
233 protected override void AddParsedSubObject(object o)
\r
235 // Preventing literal controls from being added as children.
\r
238 protected override void CreateChildControls()
\r
241 if(ViewState[ItemCountViewStateKey]!=null)
\r
243 CreateControlHierarchy(false);
\r
244 ClearChildViewState();
\r
248 protected override void OnDataBinding(EventArgs e)
\r
250 base.OnDataBinding(e);
\r
252 ClearChildViewState();
\r
253 CreateControlHierarchy(true);
\r
254 ChildControlsCreated = true;
\r
258 protected virtual void OnSelectedIndexChanged(EventArgs e)
\r
262 EventHandler eh = (EventHandler)(Events[SelectedIndexChangedEvent]);
\r
268 protected override void Render(HtmlTextWriter writer)
\r
270 PrepareControlHierarchy();
\r
271 RenderContents(writer);
\r
274 protected abstract void PrepareControlHierarchy();
\r
275 protected abstract void CreateControlHierarchy(bool useDataSource);
280 // should be `internal protected' (why, oh WHY did they do that !?!)
281 protected override void OnInit (EventArgs e)
285 if (!Page.IsPostBack)
286 RequiresDataBinding = true;
289 // should be `internal protected' (why, oh WHY did they do that !?!)
290 protected override void OnLoad (EventArgs e)
292 IDataSource ds = GetDataSourceObject () as IDataSource;
293 if (ds != null && DataSourceID != "")
294 ds.DataSourceChanged += new EventHandler (OnDataSourceChanged);
299 // should be `internal protected' (why, oh WHY did they do that !?!)
300 protected override void OnPreRender (EventArgs e)
303 base.OnPreRender (e);
306 protected void EnsureDataBound ()
308 if (RequiresDataBinding && DataSourceID != "")
312 protected virtual object GetDataSourceObject ()
314 if (DataSourceID != "")
315 return (IDataSource) NamingContainer.FindControl (DataSourceID);
320 protected virtual IEnumerable GetResolvedDataSource ()
322 if (DataSource != null && DataSourceID != "")
323 throw new HttpException ();
325 IDataSource ds = this.GetDataSourceObject () as IDataSource;
326 if (ds != null && DataSourceID != "")
327 return ds.GetView (DataMember).Select ();
328 else if (DataSource != null)
329 return DataSourceHelper.GetResolvedDataSource (DataSource, DataMember);
334 protected void OnDataSourceChanged (object sender, EventArgs e)
336 RequiresDataBinding = true;
339 public virtual string DataSourceID {
341 object o = ViewState ["DataSourceID"];
349 RequiresDataBinding = true;
351 ViewState ["DataSourceID"] = value;
355 bool requiresDataBinding;
356 protected bool RequiresDataBinding {
357 get { return requiresDataBinding; }
358 set { requiresDataBinding = value; }
361 protected bool inited;
364 internal IEnumerable GetResolvedDataSource ()
366 if (DataSource != null)
367 return DataSourceHelper.GetResolvedDataSource (DataSource, DataMember);