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 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System.ComponentModel;
35 using System.ComponentModel.Design;
\r
36 using System.Collections;
\r
39 using System.Web.Util;
\r
41 namespace System.Web.UI.WebControls
\r
43 [DefaultEvent("SelectedIndexChanged")]
\r
44 [DefaultProperty("DataSource")]
\r
45 [Designer("System.Web.UI.Design.WebControls.BaseDataListDesigner, " + Consts.AssemblySystem_Design, typeof (IDesigner))]
\r
46 public abstract class BaseDataList: WebControl
\r
48 private static readonly object SelectedIndexChangedEvent = new object();
\r
49 internal static string ItemCountViewStateKey = "_!ItemCount";
\r
51 private DataKeyCollection dataKeys;
\r
52 private object dataSource;
\r
54 public BaseDataList() : base()
\r
58 public static bool IsBindableType(Type type)
\r
60 if(type.IsPrimitive || type == typeof(string) || type == typeof(DateTime) || type == typeof(Decimal))
\r
65 public override ControlCollection Controls
\r
69 EnsureChildControls();
\r
70 return base.Controls;
\r
74 public override void DataBind()
\r
77 RequiresDataBinding = false;
79 OnDataBinding(EventArgs.Empty);
\r
82 [WebCategory("Action")]
\r
83 [WebSysDescription("BaseDataList_OnSelectedIndexChanged")]
\r
84 public event EventHandler SelectedIndexChanged
\r
88 Events.AddHandler(SelectedIndexChangedEvent, value);
\r
92 Events.RemoveHandler(SelectedIndexChangedEvent, value);
\r
98 [WebCategory("Layout")]
\r
99 [WebSysDescription("BaseDataList_CellPadding")]
\r
100 public virtual int CellPadding
\r
104 if(!ControlStyleCreated)
\r
106 return ((TableStyle)ControlStyle).CellPadding;
\r
110 ((TableStyle)ControlStyle).CellPadding = value;
\r
116 [WebCategory("Layout")]
\r
117 [WebSysDescription("BaseDataList_CellSpacing")]
\r
118 public virtual int CellSpacing
\r
122 if(!ControlStyleCreated)
\r
124 return ((TableStyle)ControlStyle).CellSpacing;
\r
128 ((TableStyle)ControlStyle).CellSpacing = value;
\r
133 [WebCategory("Data")]
\r
134 [WebSysDescription("BaseDataList_DataKeyField")]
\r
135 public virtual string DataKeyField
\r
139 object o = ViewState["DataKeyField"];
\r
142 return String.Empty;
\r
146 ViewState["DataKeyField"] = value;
\r
151 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
\r
152 [WebSysDescription("BaseDataList_DataKeys")]
\r
153 public DataKeyCollection DataKeys
\r
157 if( dataKeys==null )
\r
158 dataKeys = new DataKeyCollection(DataKeysArray);
\r
165 [WebCategory("Data")]
\r
166 [WebSysDescription("BaseDataList_DataMember")]
\r
167 public string DataMember
\r
171 object o = ViewState["DataMember"];
\r
174 return String.Empty;
\r
178 ViewState["DataMember"] = value;
\r
183 [DefaultValue(null)]
\r
184 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
\r
185 [WebCategory("Data")]
\r
186 [WebSysDescription("BaseDataList_DataSource")]
\r
187 public virtual object DataSource {
192 if (value == null || value is IListSource || value is IEnumerable) {
195 throw new ArgumentException (HttpRuntime.FormatResourceString (
196 "Invalid_DataSource_Type", ID));
202 [DefaultValue(GridLines.Both)]
\r
203 [WebCategory("Appearance")]
\r
204 [WebSysDescription("BaseDataList_GridLines")]
\r
205 public virtual GridLines GridLines
\r
209 if(ControlStyleCreated)
\r
210 return ((TableStyle)ControlStyle).GridLines;
\r
211 return GridLines.Both;
\r
215 ((TableStyle)ControlStyle).GridLines = value;
\r
219 // LAMESPEC HorizontalAlign has a Category attribute, this should obviously be a WebCategory attribute
220 // but is defined incorrectly in the MS framework
\r
223 [DefaultValue(HorizontalAlign.NotSet)]
\r
224 [Category("Layout")]
\r
225 [WebSysDescription("BaseDataList_HorizontalAlign")]
\r
226 public virtual HorizontalAlign HorizontalAlign
\r
230 if(ControlStyleCreated)
\r
231 return ((TableStyle)ControlStyle).HorizontalAlign;
\r
232 return HorizontalAlign.NotSet;
\r
236 ((TableStyle)ControlStyle).HorizontalAlign = value;
\r
240 protected ArrayList DataKeysArray
\r
244 object o = ViewState["DataKeys"];
\r
247 o = new ArrayList();
\r
248 ViewState["DataKeys"] = o;
\r
250 return (ArrayList)o;
\r
254 protected override void AddParsedSubObject(object o)
\r
256 // Preventing literal controls from being added as children.
\r
259 protected override void CreateChildControls()
\r
262 if(ViewState[ItemCountViewStateKey]!=null)
\r
264 CreateControlHierarchy(false);
\r
265 ClearChildViewState();
\r
269 protected override void OnDataBinding(EventArgs e)
\r
271 base.OnDataBinding(e);
\r
273 ClearChildViewState();
\r
274 CreateControlHierarchy(true);
\r
275 ChildControlsCreated = true;
\r
279 protected virtual void OnSelectedIndexChanged(EventArgs e)
\r
283 EventHandler eh = (EventHandler)(Events[SelectedIndexChangedEvent]);
\r
289 protected override void Render(HtmlTextWriter writer)
\r
291 PrepareControlHierarchy();
\r
292 RenderContents(writer);
\r
295 protected abstract void PrepareControlHierarchy();
\r
296 protected abstract void CreateControlHierarchy(bool useDataSource);
301 // should be `internal protected' (why, oh WHY did they do that !?!)
302 protected override void OnInit (EventArgs e)
306 if (!Page.IsPostBack)
307 RequiresDataBinding = true;
310 // should be `internal protected' (why, oh WHY did they do that !?!)
311 protected override void OnLoad (EventArgs e)
313 IDataSource ds = GetDataSourceObject () as IDataSource;
314 if (ds != null && DataSourceID != "")
315 ds.DataSourceChanged += new EventHandler (OnDataSourceChanged);
320 // should be `internal protected' (why, oh WHY did they do that !?!)
321 protected override void OnPreRender (EventArgs e)
324 base.OnPreRender (e);
327 protected void EnsureDataBound ()
329 if (RequiresDataBinding && DataSourceID != "")
333 protected virtual object GetDataSourceObject ()
335 if (DataSourceID != "")
336 return (IDataSource) NamingContainer.FindControl (DataSourceID);
341 protected virtual IEnumerable GetResolvedDataSource ()
343 if (DataSource != null && DataSourceID != "")
344 throw new HttpException ();
346 IDataSource ds = this.GetDataSourceObject () as IDataSource;
347 if (ds != null && DataSourceID != "")
348 return ds.GetView (DataMember).Select ();
349 else if (DataSource != null)
350 return DataSourceHelper.GetResolvedDataSource (DataSource, DataMember);
355 protected void OnDataSourceChanged (object sender, EventArgs e)
357 RequiresDataBinding = true;
360 public virtual string DataSourceID {
362 object o = ViewState ["DataSourceID"];
370 RequiresDataBinding = true;
372 ViewState ["DataSourceID"] = value;
376 bool requiresDataBinding;
377 protected bool RequiresDataBinding {
378 get { return requiresDataBinding; }
379 set { requiresDataBinding = value; }
382 protected bool inited;
385 internal IEnumerable GetResolvedDataSource ()
387 if (DataSource != null)
388 return DataSourceHelper.GetResolvedDataSource (DataSource, DataMember);