2 // System.Web.UI.WebControls.BaseDataList.cs
5 // Gaurav Vaish (gvaish@iitk.ac.in)
6 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
7 // Sanjay Gupta (gsanjay@novell.com)
9 // (C) Gaurav Vaish (2001)
10 // (C) 2003 Andreas Nahr
11 // (C) 2004 Novell, Inc. (http://www.novell.com)
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 using System.ComponentModel;
37 using System.ComponentModel.Design;
38 using System.Collections;
41 using System.Web.Util;
43 namespace System.Web.UI.WebControls
45 [DefaultEvent("SelectedIndexChanged")]
46 [DefaultProperty("DataSource")]
47 [Designer("System.Web.UI.Design.WebControls.BaseDataListDesigner, " + Consts.AssemblySystem_Design, typeof (IDesigner))]
48 public abstract class BaseDataList: WebControl
50 private static readonly object SelectedIndexChangedEvent = new object();
51 internal static string ItemCountViewStateKey = "_!ItemCount";
53 private DataKeyCollection dataKeys;
54 private object dataSource;
56 public BaseDataList() : base()
60 public static bool IsBindableType(Type type)
62 if(type.IsPrimitive || type == typeof(string) || type == typeof(DateTime) || type == typeof(Decimal))
67 public override ControlCollection Controls
71 EnsureChildControls();
76 public override void DataBind()
79 RequiresDataBinding = false;
81 OnDataBinding(EventArgs.Empty);
84 [WebCategory("Action")]
85 [WebSysDescription("BaseDataList_OnSelectedIndexChanged")]
86 public event EventHandler SelectedIndexChanged
90 Events.AddHandler(SelectedIndexChangedEvent, value);
94 Events.RemoveHandler(SelectedIndexChangedEvent, value);
100 [WebCategory("Layout")]
101 [WebSysDescription("BaseDataList_CellPadding")]
102 public virtual int CellPadding
106 if(!ControlStyleCreated)
108 return ((TableStyle)ControlStyle).CellPadding;
112 ((TableStyle)ControlStyle).CellPadding = value;
118 [WebCategory("Layout")]
119 [WebSysDescription("BaseDataList_CellSpacing")]
120 public virtual int CellSpacing
124 if(!ControlStyleCreated)
126 return ((TableStyle)ControlStyle).CellSpacing;
130 ((TableStyle)ControlStyle).CellSpacing = value;
135 [WebCategory("Data")]
136 [WebSysDescription("BaseDataList_DataKeyField")]
137 public virtual string DataKeyField
141 object o = ViewState["DataKeyField"];
148 ViewState["DataKeyField"] = value;
153 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
154 [WebSysDescription("BaseDataList_DataKeys")]
155 public DataKeyCollection DataKeys
160 dataKeys = new DataKeyCollection(DataKeysArray);
167 [WebCategory("Data")]
168 [WebSysDescription("BaseDataList_DataMember")]
169 public string DataMember
173 object o = ViewState["DataMember"];
180 ViewState["DataMember"] = value;
186 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
187 [WebCategory("Data")]
188 [WebSysDescription("BaseDataList_DataSource")]
189 public virtual object DataSource {
194 if (value == null || value is IListSource || value is IEnumerable) {
197 throw new ArgumentException (HttpRuntime.FormatResourceString (
198 "Invalid_DataSource_Type", ID));
204 [DefaultValue(GridLines.Both)]
205 [WebCategory("Appearance")]
206 [WebSysDescription("BaseDataList_GridLines")]
207 public virtual GridLines GridLines
211 if(ControlStyleCreated)
212 return ((TableStyle)ControlStyle).GridLines;
213 return GridLines.Both;
217 ((TableStyle)ControlStyle).GridLines = value;
221 // LAMESPEC HorizontalAlign has a Category attribute, this should obviously be a WebCategory attribute
222 // but is defined incorrectly in the MS framework
225 [DefaultValue(HorizontalAlign.NotSet)]
227 [WebSysDescription("BaseDataList_HorizontalAlign")]
228 public virtual HorizontalAlign HorizontalAlign
232 if(ControlStyleCreated)
233 return ((TableStyle)ControlStyle).HorizontalAlign;
234 return HorizontalAlign.NotSet;
238 ((TableStyle)ControlStyle).HorizontalAlign = value;
242 protected ArrayList DataKeysArray
246 object o = ViewState["DataKeys"];
250 ViewState["DataKeys"] = o;
256 protected override void AddParsedSubObject(object o)
258 // Preventing literal controls from being added as children.
261 protected override void CreateChildControls()
264 if(ViewState[ItemCountViewStateKey]!=null)
266 CreateControlHierarchy(false);
267 ClearChildViewState();
271 protected override void OnDataBinding(EventArgs e)
273 base.OnDataBinding(e);
275 ClearChildViewState();
276 CreateControlHierarchy(true);
277 ChildControlsCreated = true;
281 protected virtual void OnSelectedIndexChanged(EventArgs e)
285 EventHandler eh = (EventHandler)(Events[SelectedIndexChangedEvent]);
291 protected override void Render(HtmlTextWriter writer)
293 PrepareControlHierarchy();
294 RenderContents(writer);
297 protected abstract void PrepareControlHierarchy();
298 protected abstract void CreateControlHierarchy(bool useDataSource);
303 // should be `internal protected' (why, oh WHY did they do that !?!)
304 protected override void OnInit (EventArgs e)
308 if (!Page.IsPostBack)
309 RequiresDataBinding = true;
312 // should be `internal protected' (why, oh WHY did they do that !?!)
313 protected override void OnLoad (EventArgs e)
315 IDataSource ds = GetDataSourceObject () as IDataSource;
316 if (ds != null && DataSourceID != "")
317 ds.DataSourceChanged += new EventHandler (OnDataSourceChanged);
322 // should be `internal protected' (why, oh WHY did they do that !?!)
323 protected override void OnPreRender (EventArgs e)
326 base.OnPreRender (e);
329 protected void EnsureDataBound ()
331 if (RequiresDataBinding && DataSourceID != "")
335 protected virtual object GetDataSourceObject ()
337 if (DataSourceID != "")
338 return (IDataSource) NamingContainer.FindControl (DataSourceID);
343 protected virtual IEnumerable GetResolvedDataSource ()
345 if (DataSource != null && DataSourceID != "")
346 throw new HttpException ();
348 IDataSource ds = this.GetDataSourceObject () as IDataSource;
349 if (ds != null && DataSourceID != "") {
350 return ds.GetView (DataMember).ExecuteSelect (selectArguments);
352 else if (DataSource != null)
353 return DataSourceHelper.GetResolvedDataSource (DataSource, DataMember);
358 protected void OnDataSourceChanged (object sender, EventArgs e)
360 RequiresDataBinding = true;
363 public virtual string DataSourceID {
365 object o = ViewState ["DataSourceID"];
373 RequiresDataBinding = true;
375 ViewState ["DataSourceID"] = value;
379 bool requiresDataBinding;
380 protected bool RequiresDataBinding {
381 get { return requiresDataBinding; }
382 set { requiresDataBinding = value; }
385 protected bool inited;
387 DataSourceSelectArguments selectArguments = null;
389 protected DataSourceSelectArguments SelectArguments {
390 get { return selectArguments; }
393 internal IEnumerable GetResolvedDataSource ()
395 if (DataSource != null)
396 return DataSourceHelper.GetResolvedDataSource (DataSource, DataMember);