GridView.cs 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916
  1. //
  2. // System.Web.UI.WebControls.GridView.cs
  3. //
  4. // Authors:
  5. // Lluis Sanchez Gual ([email protected])
  6. //
  7. // (C) 2005 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. #if NET_2_0
  29. using System;
  30. using System.Collections;
  31. using System.Collections.Specialized;
  32. using System.ComponentModel;
  33. using System.Web.UI;
  34. using System.Security.Permissions;
  35. using System.Text;
  36. using System.IO;
  37. using System.Reflection;
  38. namespace System.Web.UI.WebControls
  39. {
  40. [SupportsEventValidation]
  41. [DesignerAttribute ("System.Web.UI.Design.WebControls.GridViewDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
  42. [ControlValuePropertyAttribute ("SelectedValue")]
  43. [DefaultEventAttribute ("SelectedIndexChanged")]
  44. [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  45. [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  46. public class GridView: CompositeDataBoundControl, ICallbackEventHandler, ICallbackContainer, IPostBackEventHandler, IPostBackContainer
  47. {
  48. Table table;
  49. GridViewRowCollection rows;
  50. GridViewRow bottomPagerRow;
  51. GridViewRow topPagerRow;
  52. IOrderedDictionary currentEditRowKeys;
  53. IOrderedDictionary currentEditNewValues;
  54. IOrderedDictionary currentEditOldValues;
  55. ITemplate pagerTemplate;
  56. ITemplate emptyDataTemplate;
  57. PropertyDescriptor[] cachedKeyProperties;
  58. // View state
  59. DataControlFieldCollection columns;
  60. PagerSettings pagerSettings;
  61. TableItemStyle alternatingRowStyle;
  62. TableItemStyle editRowStyle;
  63. TableItemStyle emptyDataRowStyle;
  64. TableItemStyle footerStyle;
  65. TableItemStyle headerStyle;
  66. TableItemStyle pagerStyle;
  67. TableItemStyle rowStyle;
  68. TableItemStyle selectedRowStyle;
  69. DataKeyArray keys;
  70. DataKey oldEditValues;
  71. AutoGeneratedFieldProperties[] autoFieldProperties;
  72. readonly string[] emptyKeys = new string[0];
  73. private static readonly object PageIndexChangedEvent = new object();
  74. private static readonly object PageIndexChangingEvent = new object();
  75. private static readonly object RowCancelingEditEvent = new object();
  76. private static readonly object RowCommandEvent = new object();
  77. private static readonly object RowCreatedEvent = new object();
  78. private static readonly object RowDataBoundEvent = new object();
  79. private static readonly object RowDeletedEvent = new object();
  80. private static readonly object RowDeletingEvent = new object();
  81. private static readonly object RowEditingEvent = new object();
  82. private static readonly object RowUpdatedEvent = new object();
  83. private static readonly object RowUpdatingEvent = new object();
  84. private static readonly object SelectedIndexChangedEvent = new object();
  85. private static readonly object SelectedIndexChangingEvent = new object();
  86. private static readonly object SortedEvent = new object();
  87. private static readonly object SortingEvent = new object();
  88. // Control state
  89. int pageIndex;
  90. int pageCount = -1;
  91. int selectedIndex = -1;
  92. int editIndex = -1;
  93. SortDirection sortDirection = SortDirection.Ascending;
  94. string sortExpression;
  95. public GridView ()
  96. {
  97. }
  98. public event EventHandler PageIndexChanged {
  99. add { Events.AddHandler (PageIndexChangedEvent, value); }
  100. remove { Events.RemoveHandler (PageIndexChangedEvent, value); }
  101. }
  102. public event GridViewPageEventHandler PageIndexChanging {
  103. add { Events.AddHandler (PageIndexChangingEvent, value); }
  104. remove { Events.RemoveHandler (PageIndexChangingEvent, value); }
  105. }
  106. public event GridViewCancelEditEventHandler RowCancelingEdit {
  107. add { Events.AddHandler (RowCancelingEditEvent, value); }
  108. remove { Events.RemoveHandler (RowCancelingEditEvent, value); }
  109. }
  110. public event GridViewCommandEventHandler RowCommand {
  111. add { Events.AddHandler (RowCommandEvent, value); }
  112. remove { Events.RemoveHandler (RowCommandEvent, value); }
  113. }
  114. public event GridViewRowEventHandler RowCreated {
  115. add { Events.AddHandler (RowCreatedEvent, value); }
  116. remove { Events.RemoveHandler (RowCreatedEvent, value); }
  117. }
  118. public event GridViewRowEventHandler RowDataBound {
  119. add { Events.AddHandler (RowDataBoundEvent, value); }
  120. remove { Events.RemoveHandler (RowDataBoundEvent, value); }
  121. }
  122. public event GridViewDeletedEventHandler RowDeleted {
  123. add { Events.AddHandler (RowDeletedEvent, value); }
  124. remove { Events.RemoveHandler (RowDeletedEvent, value); }
  125. }
  126. public event GridViewDeleteEventHandler RowDeleting {
  127. add { Events.AddHandler (RowDeletingEvent, value); }
  128. remove { Events.RemoveHandler (RowDeletingEvent, value); }
  129. }
  130. public event GridViewEditEventHandler RowEditing {
  131. add { Events.AddHandler (RowEditingEvent, value); }
  132. remove { Events.RemoveHandler (RowEditingEvent, value); }
  133. }
  134. public event GridViewUpdatedEventHandler RowUpdated {
  135. add { Events.AddHandler (RowUpdatedEvent, value); }
  136. remove { Events.RemoveHandler (RowUpdatedEvent, value); }
  137. }
  138. public event GridViewUpdateEventHandler RowUpdating {
  139. add { Events.AddHandler (RowUpdatingEvent, value); }
  140. remove { Events.RemoveHandler (RowUpdatingEvent, value); }
  141. }
  142. public event EventHandler SelectedIndexChanged {
  143. add { Events.AddHandler (SelectedIndexChangedEvent, value); }
  144. remove { Events.RemoveHandler (SelectedIndexChangedEvent, value); }
  145. }
  146. public event GridViewSelectEventHandler SelectedIndexChanging {
  147. add { Events.AddHandler (SelectedIndexChangingEvent, value); }
  148. remove { Events.RemoveHandler (SelectedIndexChangingEvent, value); }
  149. }
  150. public event EventHandler Sorted {
  151. add { Events.AddHandler (SortedEvent, value); }
  152. remove { Events.RemoveHandler (SortedEvent, value); }
  153. }
  154. public event GridViewSortEventHandler Sorting {
  155. add { Events.AddHandler (SortingEvent, value); }
  156. remove { Events.RemoveHandler (SortingEvent, value); }
  157. }
  158. protected virtual void OnPageIndexChanged (EventArgs e)
  159. {
  160. if (Events != null) {
  161. EventHandler eh = (EventHandler) Events [PageIndexChangedEvent];
  162. if (eh != null) eh (this, e);
  163. }
  164. }
  165. protected virtual void OnPageIndexChanging (GridViewPageEventArgs e)
  166. {
  167. if (Events != null) {
  168. GridViewPageEventHandler eh = (GridViewPageEventHandler) Events [PageIndexChangingEvent];
  169. if (eh != null) eh (this, e);
  170. }
  171. }
  172. protected virtual void OnRowCancelingEdit (GridViewCancelEditEventArgs e)
  173. {
  174. if (Events != null) {
  175. GridViewCancelEditEventHandler eh = (GridViewCancelEditEventHandler) Events [RowCancelingEditEvent];
  176. if (eh != null) eh (this, e);
  177. }
  178. }
  179. protected virtual void OnRowCommand (GridViewCommandEventArgs e)
  180. {
  181. if (Events != null) {
  182. GridViewCommandEventHandler eh = (GridViewCommandEventHandler) Events [RowCommandEvent];
  183. if (eh != null) eh (this, e);
  184. }
  185. }
  186. protected virtual void OnRowCreated (GridViewRowEventArgs e)
  187. {
  188. if (Events != null) {
  189. GridViewRowEventHandler eh = (GridViewRowEventHandler) Events [RowCreatedEvent];
  190. if (eh != null) eh (this, e);
  191. }
  192. }
  193. protected virtual void OnRowDataBound (GridViewRowEventArgs e)
  194. {
  195. if (Events != null) {
  196. GridViewRowEventHandler eh = (GridViewRowEventHandler) Events [RowDataBoundEvent];
  197. if (eh != null) eh (this, e);
  198. }
  199. }
  200. protected virtual void OnRowDeleted (GridViewDeletedEventArgs e)
  201. {
  202. if (Events != null) {
  203. GridViewDeletedEventHandler eh = (GridViewDeletedEventHandler) Events [RowDeletedEvent];
  204. if (eh != null) eh (this, e);
  205. }
  206. }
  207. protected virtual void OnRowDeleting (GridViewDeleteEventArgs e)
  208. {
  209. if (Events != null) {
  210. GridViewDeleteEventHandler eh = (GridViewDeleteEventHandler) Events [RowDeletingEvent];
  211. if (eh != null) eh (this, e);
  212. }
  213. }
  214. protected virtual void OnRowEditing (GridViewEditEventArgs e)
  215. {
  216. if (Events != null) {
  217. GridViewEditEventHandler eh = (GridViewEditEventHandler) Events [RowEditingEvent];
  218. if (eh != null) eh (this, e);
  219. }
  220. }
  221. protected virtual void OnRowUpdated (GridViewUpdatedEventArgs e)
  222. {
  223. if (Events != null) {
  224. GridViewUpdatedEventHandler eh = (GridViewUpdatedEventHandler) Events [RowUpdatedEvent];
  225. if (eh != null) eh (this, e);
  226. }
  227. }
  228. protected virtual void OnRowUpdating (GridViewUpdateEventArgs e)
  229. {
  230. if (Events != null) {
  231. GridViewUpdateEventHandler eh = (GridViewUpdateEventHandler) Events [RowUpdatingEvent];
  232. if (eh != null) eh (this, e);
  233. }
  234. }
  235. protected virtual void OnSelectedIndexChanged (EventArgs e)
  236. {
  237. if (Events != null) {
  238. EventHandler eh = (EventHandler) Events [SelectedIndexChangedEvent];
  239. if (eh != null) eh (this, e);
  240. }
  241. }
  242. protected virtual void OnSelectedIndexChanging (GridViewSelectEventArgs e)
  243. {
  244. if (Events != null) {
  245. GridViewSelectEventHandler eh = (GridViewSelectEventHandler) Events [SelectedIndexChangingEvent];
  246. if (eh != null) eh (this, e);
  247. }
  248. }
  249. protected virtual void OnSorted (EventArgs e)
  250. {
  251. if (Events != null) {
  252. EventHandler eh = (EventHandler) Events [SortedEvent];
  253. if (eh != null) eh (this, e);
  254. }
  255. }
  256. protected virtual void OnSorting (GridViewSortEventArgs e)
  257. {
  258. if (Events != null) {
  259. GridViewSortEventHandler eh = (GridViewSortEventHandler) Events [SortingEvent];
  260. if (eh != null) eh (this, e);
  261. }
  262. }
  263. [WebCategoryAttribute ("Paging")]
  264. [DefaultValueAttribute (false)]
  265. public virtual bool AllowPaging {
  266. get {
  267. object ob = ViewState ["AllowPaging"];
  268. if (ob != null) return (bool) ob;
  269. return false;
  270. }
  271. set {
  272. ViewState ["AllowPaging"] = value;
  273. RequireBinding ();
  274. }
  275. }
  276. [WebCategoryAttribute ("Behavior")]
  277. [DefaultValueAttribute (false)]
  278. public virtual bool AllowSorting {
  279. get {
  280. object ob = ViewState ["AllowSorting"];
  281. if (ob != null) return (bool) ob;
  282. return false;
  283. }
  284. set {
  285. ViewState ["AllowSorting"] = value;
  286. RequireBinding ();
  287. }
  288. }
  289. [WebCategoryAttribute ("Styles")]
  290. [PersistenceMode (PersistenceMode.InnerProperty)]
  291. [NotifyParentProperty (true)]
  292. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  293. public TableItemStyle AlternatingRowStyle {
  294. get {
  295. if (alternatingRowStyle == null) {
  296. alternatingRowStyle = new TableItemStyle ();
  297. if (IsTrackingViewState)
  298. alternatingRowStyle.TrackViewState();
  299. }
  300. return alternatingRowStyle;
  301. }
  302. }
  303. [WebCategoryAttribute ("Behavior")]
  304. [DefaultValueAttribute (false)]
  305. public virtual bool AutoGenerateEditButton {
  306. get {
  307. object ob = ViewState ["AutoGenerateEditButton"];
  308. if (ob != null) return (bool) ob;
  309. return false;
  310. }
  311. set {
  312. ViewState ["AutoGenerateEditButton"] = value;
  313. RequireBinding ();
  314. }
  315. }
  316. [WebCategoryAttribute ("Behavior")]
  317. [DefaultValueAttribute (false)]
  318. public virtual bool AutoGenerateDeleteButton {
  319. get {
  320. object ob = ViewState ["AutoGenerateDeleteButton"];
  321. if (ob != null) return (bool) ob;
  322. return false;
  323. }
  324. set {
  325. ViewState ["AutoGenerateDeleteButton"] = value;
  326. RequireBinding ();
  327. }
  328. }
  329. [WebCategoryAttribute ("Behavior")]
  330. [DefaultValueAttribute (false)]
  331. public virtual bool AutoGenerateSelectButton {
  332. get {
  333. object ob = ViewState ["AutoGenerateSelectButton"];
  334. if (ob != null) return (bool) ob;
  335. return false;
  336. }
  337. set {
  338. ViewState ["AutoGenerateSelectButton"] = value;
  339. RequireBinding ();
  340. }
  341. }
  342. [WebCategoryAttribute ("Behavior")]
  343. [DefaultValueAttribute (true)]
  344. public virtual bool AutoGenerateColumns {
  345. get {
  346. object ob = ViewState ["AutoGenerateColumns"];
  347. if (ob != null) return (bool) ob;
  348. return true;
  349. }
  350. set {
  351. ViewState ["AutoGenerateColumns"] = value;
  352. RequireBinding ();
  353. }
  354. }
  355. [UrlPropertyAttribute]
  356. [WebCategoryAttribute ("Appearance")]
  357. [DefaultValueAttribute ("")]
  358. [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  359. public virtual string BackImageUrl {
  360. get {
  361. object ob = ViewState ["BackImageUrl"];
  362. if (ob != null) return (string) ob;
  363. return string.Empty;
  364. }
  365. set {
  366. ViewState ["BackImageUrl"] = value;
  367. RequireBinding ();
  368. }
  369. }
  370. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  371. [BrowsableAttribute (false)]
  372. public virtual GridViewRow BottomPagerRow {
  373. get {
  374. EnsureDataBound ();
  375. return bottomPagerRow;
  376. }
  377. }
  378. [WebCategoryAttribute ("Accessibility")]
  379. [DefaultValueAttribute ("")]
  380. [LocalizableAttribute (true)]
  381. public virtual string Caption {
  382. get {
  383. object ob = ViewState ["Caption"];
  384. if (ob != null) return (string) ob;
  385. return string.Empty;
  386. }
  387. set {
  388. ViewState ["Caption"] = value;
  389. RequireBinding ();
  390. }
  391. }
  392. [WebCategoryAttribute ("Accessibility")]
  393. [DefaultValueAttribute (TableCaptionAlign.NotSet)]
  394. public virtual TableCaptionAlign CaptionAlign
  395. {
  396. get {
  397. object o = ViewState ["CaptionAlign"];
  398. if(o != null) return (TableCaptionAlign) o;
  399. return TableCaptionAlign.NotSet;
  400. }
  401. set {
  402. ViewState ["CaptionAlign"] = value;
  403. RequireBinding ();
  404. }
  405. }
  406. [WebCategoryAttribute ("Layout")]
  407. [DefaultValueAttribute (-1)]
  408. public virtual int CellPadding
  409. {
  410. get {
  411. object o = ViewState ["CellPadding"];
  412. if (o != null) return (int) o;
  413. return -1;
  414. }
  415. set {
  416. ViewState ["CellPadding"] = value;
  417. RequireBinding ();
  418. }
  419. }
  420. [WebCategoryAttribute ("Layout")]
  421. [DefaultValueAttribute (0)]
  422. public virtual int CellSpacing
  423. {
  424. get {
  425. object o = ViewState ["CellSpacing"];
  426. if (o != null) return (int) o;
  427. return 0;
  428. }
  429. set {
  430. ViewState ["CellSpacing"] = value;
  431. RequireBinding ();
  432. }
  433. }
  434. [EditorAttribute ("System.Web.UI.Design.WebControls.DataControlFieldTypeEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  435. [MergablePropertyAttribute (false)]
  436. [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
  437. [DefaultValueAttribute (null)]
  438. [WebCategoryAttribute ("Misc")]
  439. public virtual DataControlFieldCollection Columns {
  440. get {
  441. if (columns == null) {
  442. columns = new DataControlFieldCollection ();
  443. columns.FieldsChanged += new EventHandler (OnFieldsChanged);
  444. if (IsTrackingViewState)
  445. ((IStateManager)columns).TrackViewState ();
  446. }
  447. return columns;
  448. }
  449. }
  450. [DefaultValueAttribute (null)]
  451. [WebCategoryAttribute ("Data")]
  452. [TypeConverter (typeof(StringArrayConverter))]
  453. [EditorAttribute ("System.Web.UI.Design.WebControls.DataFieldEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  454. public virtual string[] DataKeyNames
  455. {
  456. get {
  457. object o = ViewState ["DataKeyNames"];
  458. if (o != null) return (string[]) o;
  459. return emptyKeys;
  460. }
  461. set {
  462. ViewState ["DataKeyNames"] = value;
  463. RequireBinding ();
  464. }
  465. }
  466. [BrowsableAttribute (false)]
  467. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  468. public virtual DataKeyArray DataKeys {
  469. get {
  470. EnsureDataBound ();
  471. return keys;
  472. }
  473. }
  474. [WebCategoryAttribute ("Misc")]
  475. [DefaultValueAttribute (-1)]
  476. public virtual int EditIndex {
  477. get {
  478. return editIndex;
  479. }
  480. set {
  481. editIndex = value;
  482. RequireBinding ();
  483. }
  484. }
  485. [WebCategoryAttribute ("Styles")]
  486. [PersistenceMode (PersistenceMode.InnerProperty)]
  487. [NotifyParentProperty (true)]
  488. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  489. public TableItemStyle EditRowStyle {
  490. get {
  491. if (editRowStyle == null) {
  492. editRowStyle = new TableItemStyle ();
  493. if (IsTrackingViewState)
  494. editRowStyle.TrackViewState();
  495. }
  496. return editRowStyle;
  497. }
  498. }
  499. [WebCategoryAttribute ("Styles")]
  500. [PersistenceMode (PersistenceMode.InnerProperty)]
  501. [NotifyParentProperty (true)]
  502. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  503. public TableItemStyle EmptyDataRowStyle {
  504. get {
  505. if (emptyDataRowStyle == null) {
  506. emptyDataRowStyle = new TableItemStyle ();
  507. if (IsTrackingViewState)
  508. emptyDataRowStyle.TrackViewState();
  509. }
  510. return emptyDataRowStyle;
  511. }
  512. }
  513. [DefaultValue (null)]
  514. [TemplateContainer (typeof(GridViewRow), BindingDirection.OneWay)]
  515. [PersistenceMode (PersistenceMode.InnerProperty)]
  516. [Browsable (false)]
  517. public virtual ITemplate EmptyDataTemplate {
  518. get { return emptyDataTemplate; }
  519. set { emptyDataTemplate = value; RequireBinding (); }
  520. }
  521. [LocalizableAttribute (true)]
  522. [WebCategoryAttribute ("Appearance")]
  523. [DefaultValueAttribute ("")]
  524. public virtual string EmptyDataText {
  525. get {
  526. object ob = ViewState ["EmptyDataText"];
  527. if (ob != null) return (string) ob;
  528. return string.Empty;
  529. }
  530. set {
  531. ViewState ["EmptyDataText"] = value;
  532. RequireBinding ();
  533. }
  534. }
  535. [WebCategoryAttribute ("Behavior")]
  536. [DefaultValueAttribute (false)]
  537. public virtual bool EnableSortingAndPagingCallbacks {
  538. get {
  539. object ob = ViewState ["EnableSortingAndPagingCallbacks"];
  540. if (ob != null) return (bool) ob;
  541. return false;
  542. }
  543. set {
  544. ViewState ["EnableSortingAndPagingCallbacks"] = value;
  545. RequireBinding ();
  546. }
  547. }
  548. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  549. [BrowsableAttribute (false)]
  550. public virtual GridViewRow FooterRow {
  551. get {
  552. if (table != null) {
  553. for (int index = table.Rows.Count - 1; index >= 0; index--) {
  554. GridViewRow row = (GridViewRow) table.Rows [index];
  555. switch (row.RowType) {
  556. case DataControlRowType.Separator:
  557. case DataControlRowType.Pager:
  558. continue;
  559. case DataControlRowType.Footer:
  560. return row;
  561. default:
  562. break;
  563. }
  564. }
  565. }
  566. return null;
  567. }
  568. }
  569. [WebCategoryAttribute ("Styles")]
  570. [PersistenceMode (PersistenceMode.InnerProperty)]
  571. [NotifyParentProperty (true)]
  572. [DefaultValue (null)]
  573. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  574. public TableItemStyle FooterStyle {
  575. get {
  576. if (footerStyle == null) {
  577. footerStyle = new TableItemStyle ();
  578. if (IsTrackingViewState)
  579. footerStyle.TrackViewState();
  580. }
  581. return footerStyle;
  582. }
  583. }
  584. [WebCategoryAttribute ("Appearance")]
  585. [DefaultValueAttribute (GridLines.Both)]
  586. public virtual GridLines GridLines {
  587. get {
  588. object ob = ViewState ["GridLines"];
  589. if (ob != null) return (GridLines) ob;
  590. return GridLines.Both;
  591. }
  592. set {
  593. ViewState ["GridLines"] = value;
  594. }
  595. }
  596. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  597. [BrowsableAttribute (false)]
  598. public virtual GridViewRow HeaderRow {
  599. get {
  600. if (table != null) {
  601. for (int index = 0, total = table.Rows.Count; index < total; index++) {
  602. GridViewRow row = (GridViewRow) table.Rows [index];
  603. switch (row.RowType) {
  604. case DataControlRowType.Separator:
  605. case DataControlRowType.Pager:
  606. continue;
  607. case DataControlRowType.Header:
  608. return row;
  609. default:
  610. break;
  611. }
  612. }
  613. }
  614. return null;
  615. }
  616. }
  617. [WebCategoryAttribute ("Styles")]
  618. [PersistenceMode (PersistenceMode.InnerProperty)]
  619. [NotifyParentProperty (true)]
  620. [DefaultValue (null)]
  621. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  622. public TableItemStyle HeaderStyle {
  623. get {
  624. if (headerStyle == null) {
  625. headerStyle = new TableItemStyle ();
  626. if (IsTrackingViewState)
  627. headerStyle.TrackViewState();
  628. }
  629. return headerStyle;
  630. }
  631. }
  632. [Category ("Layout")]
  633. [DefaultValueAttribute (HorizontalAlign.NotSet)]
  634. public virtual HorizontalAlign HorizontalAlign {
  635. get {
  636. object ob = ViewState ["HorizontalAlign"];
  637. if (ob != null) return (HorizontalAlign) ob;
  638. return HorizontalAlign.NotSet;
  639. }
  640. set {
  641. ViewState ["HorizontalAlign"] = value;
  642. RequireBinding ();
  643. }
  644. }
  645. [BrowsableAttribute (false)]
  646. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  647. public virtual int PageCount {
  648. get {
  649. if (pageCount != -1) return pageCount;
  650. EnsureDataBound ();
  651. return pageCount;
  652. }
  653. }
  654. [WebCategoryAttribute ("Paging")]
  655. [BrowsableAttribute (true)]
  656. [DefaultValueAttribute (0)]
  657. public virtual int PageIndex {
  658. get {
  659. return pageIndex;
  660. }
  661. set {
  662. pageIndex = value;
  663. RequireBinding ();
  664. }
  665. }
  666. [DefaultValueAttribute (10)]
  667. [WebCategoryAttribute ("Paging")]
  668. public virtual int PageSize {
  669. get {
  670. object ob = ViewState ["PageSize"];
  671. if (ob != null) return (int) ob;
  672. return 10;
  673. }
  674. set {
  675. ViewState ["PageSize"] = value;
  676. RequireBinding ();
  677. }
  678. }
  679. [WebCategoryAttribute ("Paging")]
  680. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Content)]
  681. [NotifyParentPropertyAttribute (true)]
  682. [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
  683. public virtual PagerSettings PagerSettings {
  684. get {
  685. if (pagerSettings == null) {
  686. pagerSettings = new PagerSettings (this);
  687. if (IsTrackingViewState)
  688. ((IStateManager)pagerSettings).TrackViewState ();
  689. }
  690. return pagerSettings;
  691. }
  692. }
  693. [WebCategoryAttribute ("Styles")]
  694. [PersistenceMode (PersistenceMode.InnerProperty)]
  695. [NotifyParentProperty (true)]
  696. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  697. public TableItemStyle PagerStyle {
  698. get {
  699. if (pagerStyle == null) {
  700. pagerStyle = new TableItemStyle ();
  701. if (IsTrackingViewState)
  702. pagerStyle.TrackViewState();
  703. }
  704. return pagerStyle;
  705. }
  706. }
  707. [DefaultValue (null)]
  708. /* DataControlPagerCell isnt specified in the docs */
  709. //[TemplateContainer (typeof(DataControlPagerCell), BindingDirection.OneWay)]
  710. [PersistenceMode (PersistenceMode.InnerProperty)]
  711. [Browsable (false)]
  712. public virtual ITemplate PagerTemplate {
  713. get { return pagerTemplate; }
  714. set { pagerTemplate = value; RequireBinding (); }
  715. }
  716. [DefaultValueAttribute ("")]
  717. [WebCategoryAttribute ("Accessibility")]
  718. // [TypeConverterAttribute (typeof(System.Web.UI.Design.DataColumnSelectionConverter)]
  719. public virtual string RowHeaderColumn {
  720. get {
  721. object ob = ViewState ["RowHeaderColumn"];
  722. if (ob != null) return (string) ob;
  723. return string.Empty;
  724. }
  725. set {
  726. ViewState ["RowHeaderColumn"] = value;
  727. RequireBinding ();
  728. }
  729. }
  730. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  731. [BrowsableAttribute (false)]
  732. public virtual GridViewRowCollection Rows {
  733. get {
  734. EnsureDataBound ();
  735. return rows;
  736. }
  737. }
  738. [WebCategoryAttribute ("Styles")]
  739. [PersistenceMode (PersistenceMode.InnerProperty)]
  740. [NotifyParentProperty (true)]
  741. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  742. public TableItemStyle RowStyle {
  743. get {
  744. if (rowStyle == null) {
  745. rowStyle = new TableItemStyle ();
  746. if (IsTrackingViewState)
  747. rowStyle.TrackViewState();
  748. }
  749. return rowStyle;
  750. }
  751. }
  752. [BrowsableAttribute (false)]
  753. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  754. public virtual DataKey SelectedDataKey {
  755. get {
  756. if (selectedIndex >= 0 && selectedIndex < DataKeys.Count) {
  757. return DataKeys [selectedIndex];
  758. } else
  759. return null;
  760. }
  761. }
  762. [BindableAttribute (true)]
  763. [DefaultValueAttribute (-1)]
  764. public virtual int SelectedIndex {
  765. get {
  766. return selectedIndex;
  767. }
  768. set {
  769. if (Rows != null && selectedIndex >= 0 && selectedIndex < Rows.Count) {
  770. int oldIndex = selectedIndex;
  771. selectedIndex = -1;
  772. Rows [oldIndex].RowState = GetRowState (oldIndex);
  773. }
  774. selectedIndex = value;
  775. if (Rows != null && selectedIndex >= 0 && selectedIndex < Rows.Count) {
  776. Rows [selectedIndex].RowState = GetRowState (selectedIndex);
  777. }
  778. }
  779. }
  780. [BrowsableAttribute (false)]
  781. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  782. public virtual GridViewRow SelectedRow {
  783. get {
  784. if (selectedIndex >= 0 && selectedIndex < Rows.Count) {
  785. return Rows [selectedIndex];
  786. } else
  787. return null;
  788. }
  789. }
  790. [WebCategoryAttribute ("Styles")]
  791. [PersistenceMode (PersistenceMode.InnerProperty)]
  792. [NotifyParentProperty (true)]
  793. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  794. public TableItemStyle SelectedRowStyle {
  795. get {
  796. if (selectedRowStyle == null) {
  797. selectedRowStyle = new TableItemStyle ();
  798. if (IsTrackingViewState)
  799. selectedRowStyle.TrackViewState();
  800. }
  801. return selectedRowStyle;
  802. }
  803. }
  804. [BrowsableAttribute (false)]
  805. public object SelectedValue {
  806. get {
  807. if (SelectedDataKey != null)
  808. return SelectedDataKey.Value;
  809. else
  810. return null;
  811. }
  812. }
  813. [WebCategoryAttribute ("Appearance")]
  814. [DefaultValueAttribute (false)]
  815. public virtual bool ShowFooter {
  816. get {
  817. object ob = ViewState ["ShowFooter"];
  818. if (ob != null) return (bool) ob;
  819. return false;
  820. }
  821. set {
  822. ViewState ["ShowFooter"] = value;
  823. RequireBinding ();
  824. }
  825. }
  826. [WebCategoryAttribute ("Appearance")]
  827. [DefaultValueAttribute (true)]
  828. public virtual bool ShowHeader {
  829. get {
  830. object ob = ViewState ["ShowHeader"];
  831. if (ob != null) return (bool) ob;
  832. return true;
  833. }
  834. set {
  835. ViewState ["ShowHeader"] = value;
  836. RequireBinding ();
  837. }
  838. }
  839. [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
  840. [BrowsableAttribute (false)]
  841. [DefaultValueAttribute (SortDirection.Ascending)]
  842. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  843. public virtual SortDirection SortDirection {
  844. get { return sortDirection; }
  845. }
  846. [BrowsableAttribute (false)]
  847. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  848. public virtual string SortExpression {
  849. get { return sortExpression; }
  850. }
  851. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  852. [BrowsableAttribute (false)]
  853. public virtual GridViewRow TopPagerRow {
  854. get {
  855. EnsureDataBound ();
  856. return topPagerRow;
  857. }
  858. }
  859. [WebCategoryAttribute ("Accessibility")]
  860. [DefaultValueAttribute (true)]
  861. public virtual bool UseAccessibleHeader {
  862. get {
  863. object ob = ViewState ["UseAccessibleHeader"];
  864. if (ob != null) return (bool) ob;
  865. return true;
  866. }
  867. set {
  868. ViewState ["UseAccessibleHeader"] = value;
  869. RequireBinding ();
  870. }
  871. }
  872. public virtual bool IsBindableType (Type type)
  873. {
  874. return type.IsPrimitive || type == typeof(string) || type == typeof(DateTime) || type == typeof(Guid);
  875. }
  876. protected override DataSourceSelectArguments CreateDataSourceSelectArguments ()
  877. {
  878. return base.CreateDataSourceSelectArguments ();
  879. }
  880. protected virtual ICollection CreateColumns (PagedDataSource dataSource, bool useDataSource)
  881. {
  882. ArrayList fields = new ArrayList ();
  883. if (AutoGenerateEditButton || AutoGenerateDeleteButton || AutoGenerateSelectButton) {
  884. CommandField field = new CommandField ();
  885. field.ShowEditButton = AutoGenerateEditButton;
  886. field.ShowDeleteButton = AutoGenerateDeleteButton;
  887. field.ShowSelectButton = AutoGenerateSelectButton;
  888. fields.Add (field);
  889. }
  890. if (AutoGenerateColumns) {
  891. if (useDataSource)
  892. autoFieldProperties = CreateAutoFieldProperties (dataSource);
  893. if (autoFieldProperties != null) {
  894. foreach (AutoGeneratedFieldProperties props in autoFieldProperties)
  895. fields.Add (CreateAutoGeneratedColumn (props));
  896. }
  897. }
  898. fields.AddRange (Columns);
  899. return fields;
  900. }
  901. protected virtual AutoGeneratedField CreateAutoGeneratedColumn (AutoGeneratedFieldProperties fieldProperties)
  902. {
  903. return new AutoGeneratedField (fieldProperties);
  904. }
  905. AutoGeneratedFieldProperties[] CreateAutoFieldProperties (PagedDataSource source)
  906. {
  907. if(source == null) return null;
  908. PropertyDescriptorCollection props = source.GetItemProperties (new PropertyDescriptor[0]);
  909. Type prop_type;
  910. ArrayList retVal = new ArrayList();
  911. if (props == null)
  912. {
  913. object fitem = null;
  914. prop_type = null;
  915. PropertyInfo prop_item = source.DataSource.GetType().GetProperty("Item",
  916. BindingFlags.Instance | BindingFlags.Static |
  917. BindingFlags.Public, null, null,
  918. new Type[] { typeof(int) }, null);
  919. if (prop_item != null) {
  920. prop_type = prop_item.PropertyType;
  921. }
  922. if (prop_type == null || prop_type == typeof(object)) {
  923. IEnumerator en = source.GetEnumerator();
  924. if (en.MoveNext())
  925. fitem = en.Current;
  926. if (fitem != null)
  927. prop_type = fitem.GetType();
  928. }
  929. if (fitem != null && fitem is ICustomTypeDescriptor) {
  930. props = TypeDescriptor.GetProperties(fitem);
  931. } else if (prop_type != null) {
  932. if (IsBindableType (prop_type)) {
  933. AutoGeneratedFieldProperties field = new AutoGeneratedFieldProperties ();
  934. ((IStateManager)field).TrackViewState();
  935. field.Name = "Item";
  936. field.DataField = BoundField.ThisExpression;
  937. field.Type = prop_type;
  938. retVal.Add (field);
  939. } else {
  940. props = TypeDescriptor.GetProperties (prop_type);
  941. }
  942. }
  943. }
  944. if (props != null && props.Count > 0)
  945. {
  946. foreach (PropertyDescriptor current in props) {
  947. if (IsBindableType (current.PropertyType)) {
  948. AutoGeneratedFieldProperties field = new AutoGeneratedFieldProperties ();
  949. ((IStateManager)field).TrackViewState();
  950. field.Name = current.Name;
  951. field.DataField = current.Name;
  952. field.IsReadOnly = current.IsReadOnly;
  953. field.Type = current.PropertyType;
  954. retVal.Add (field);
  955. }
  956. }
  957. }
  958. if (retVal.Count > 0)
  959. return (AutoGeneratedFieldProperties[]) retVal.ToArray (typeof(AutoGeneratedFieldProperties));
  960. else
  961. return new AutoGeneratedFieldProperties [0];
  962. }
  963. protected virtual GridViewRow CreateRow (int rowIndex, int dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState)
  964. {
  965. GridViewRow row = new GridViewRow (rowIndex, dataSourceIndex, rowType, rowState);
  966. OnRowCreated (new GridViewRowEventArgs (row));
  967. return row;
  968. }
  969. void RequireBinding ()
  970. {
  971. if (Initialized) {
  972. RequiresDataBinding = true;
  973. pageCount = -1;
  974. }
  975. }
  976. protected virtual Table CreateChildTable ()
  977. {
  978. Table table = new Table ();
  979. table.Caption = Caption;
  980. table.CaptionAlign = CaptionAlign;
  981. table.CellPadding = CellPadding;
  982. table.CellSpacing = CellSpacing;
  983. table.HorizontalAlign = HorizontalAlign;
  984. table.BackImageUrl = BackImageUrl;
  985. return table;
  986. }
  987. protected override int CreateChildControls (IEnumerable data, bool dataBinding)
  988. {
  989. PagedDataSource dataSource;
  990. if (dataBinding) {
  991. DataSourceView view = GetData ();
  992. dataSource = new PagedDataSource ();
  993. dataSource.DataSource = data;
  994. if (AllowPaging) {
  995. dataSource.AllowPaging = true;
  996. dataSource.PageSize = PageSize;
  997. dataSource.CurrentPageIndex = PageIndex;
  998. if (view.CanPage) {
  999. dataSource.AllowServerPaging = true;
  1000. if (view.CanRetrieveTotalRowCount)
  1001. dataSource.VirtualCount = SelectArguments.TotalRowCount;
  1002. else {
  1003. dataSource.DataSourceView = view;
  1004. dataSource.DataSourceSelectArguments = SelectArguments;
  1005. dataSource.SetItemCountFromPageIndex (PageIndex + PagerSettings.PageButtonCount);
  1006. }
  1007. }
  1008. }
  1009. pageCount = dataSource.PageCount;
  1010. }
  1011. else
  1012. {
  1013. dataSource = new PagedDataSource ();
  1014. dataSource.DataSource = data;
  1015. if (AllowPaging) {
  1016. dataSource.AllowPaging = true;
  1017. dataSource.PageSize = PageSize;
  1018. dataSource.CurrentPageIndex = PageIndex;
  1019. }
  1020. }
  1021. bool showPager = AllowPaging && (PageCount > 1);
  1022. Controls.Clear ();
  1023. table = CreateChildTable ();
  1024. Controls.Add (table);
  1025. ArrayList list = new ArrayList ();
  1026. ArrayList keyList = new ArrayList ();
  1027. // Creates the set of fields to show
  1028. ICollection fieldCollection = CreateColumns (dataSource, dataBinding);
  1029. DataControlField[] fields = new DataControlField [fieldCollection.Count];
  1030. fieldCollection.CopyTo (fields, 0);
  1031. foreach (DataControlField field in fields) {
  1032. field.Initialize (AllowSorting, this);
  1033. if (EnableSortingAndPagingCallbacks)
  1034. field.ValidateSupportsCallback ();
  1035. }
  1036. // Main table creation
  1037. if (showPager && PagerSettings.Position == PagerPosition.Top || PagerSettings.Position == PagerPosition.TopAndBottom) {
  1038. topPagerRow = CreatePagerRow (fields.Length, dataSource);
  1039. table.Rows.Add (topPagerRow);
  1040. }
  1041. GridViewRow headerRow = CreateRow (0, 0, DataControlRowType.Header, DataControlRowState.Normal);
  1042. table.Rows.Add (headerRow);
  1043. InitializeRow (headerRow, fields);
  1044. foreach (object obj in dataSource) {
  1045. DataControlRowState rstate = GetRowState (list.Count);
  1046. GridViewRow row = CreateRow (list.Count, list.Count, DataControlRowType.DataRow, rstate);
  1047. row.DataItem = obj;
  1048. list.Add (row);
  1049. table.Rows.Add (row);
  1050. InitializeRow (row, fields);
  1051. if (dataBinding) {
  1052. row.DataBind ();
  1053. OnRowDataBound (new GridViewRowEventArgs (row));
  1054. if (EditIndex == row.RowIndex)
  1055. oldEditValues = new DataKey (GetRowValues (row, false, true));
  1056. keyList.Add (new DataKey (CreateRowDataKey (row), DataKeyNames));
  1057. } else {
  1058. if (EditIndex == row.RowIndex)
  1059. oldEditValues = new DataKey (new OrderedDictionary ());
  1060. keyList.Add (new DataKey (new OrderedDictionary (), DataKeyNames));
  1061. }
  1062. if (list.Count >= PageSize)
  1063. break;
  1064. }
  1065. if (list.Count == 0)
  1066. table.Rows.Add (CreateEmptyrRow (fields.Length));
  1067. GridViewRow footerRow = CreateRow (0, 0, DataControlRowType.Footer, DataControlRowState.Normal);
  1068. table.Rows.Add (footerRow);
  1069. InitializeRow (footerRow, fields);
  1070. if (showPager && PagerSettings.Position == PagerPosition.Bottom || PagerSettings.Position == PagerPosition.TopAndBottom) {
  1071. bottomPagerRow = CreatePagerRow (fields.Length, dataSource);
  1072. table.Rows.Add (bottomPagerRow);
  1073. }
  1074. rows = new GridViewRowCollection (list);
  1075. keys = new DataKeyArray (keyList);
  1076. return dataSource.DataSourceCount;
  1077. }
  1078. [MonoTODO]
  1079. protected override Style CreateControlStyle ()
  1080. {
  1081. return base.CreateControlStyle ();
  1082. }
  1083. DataControlRowState GetRowState (int index)
  1084. {
  1085. DataControlRowState rstate = (index % 2) == 0 ? DataControlRowState.Normal : DataControlRowState.Alternate;
  1086. if (index == SelectedIndex) rstate |= DataControlRowState.Selected;
  1087. if (index == EditIndex) rstate |= DataControlRowState.Edit;
  1088. return rstate;
  1089. }
  1090. GridViewRow CreatePagerRow (int fieldCount, PagedDataSource dataSource)
  1091. {
  1092. GridViewRow row = CreateRow (-1, -1, DataControlRowType.Pager, DataControlRowState.Normal);
  1093. InitializePager (row, fieldCount, dataSource);
  1094. return row;
  1095. }
  1096. protected virtual void InitializePager (GridViewRow row, int columnSpan, PagedDataSource dataSource)
  1097. {
  1098. TableCell cell = new TableCell ();
  1099. cell.ColumnSpan = columnSpan;
  1100. if (pagerTemplate != null)
  1101. pagerTemplate.InstantiateIn (cell);
  1102. else
  1103. cell.Controls.Add (PagerSettings.CreatePagerControl (dataSource.CurrentPageIndex, dataSource.PageCount));
  1104. row.Cells.Add (cell);
  1105. }
  1106. GridViewRow CreateEmptyrRow (int fieldCount)
  1107. {
  1108. GridViewRow row = CreateRow (-1, -1, DataControlRowType.EmptyDataRow, DataControlRowState.Normal);
  1109. TableCell cell = new TableCell ();
  1110. cell.ColumnSpan = fieldCount;
  1111. if (emptyDataTemplate != null)
  1112. emptyDataTemplate.InstantiateIn (cell);
  1113. else
  1114. cell.Text = EmptyDataText;
  1115. row.Cells.Add (cell);
  1116. return row;
  1117. }
  1118. protected virtual void InitializeRow (GridViewRow row, DataControlField[] fields)
  1119. {
  1120. DataControlCellType ctype;
  1121. bool accessibleHeader = false;
  1122. switch (row.RowType) {
  1123. case DataControlRowType.Header:
  1124. ctype = DataControlCellType.Header;
  1125. accessibleHeader = UseAccessibleHeader;
  1126. break;
  1127. case DataControlRowType.Footer:
  1128. ctype = DataControlCellType.Footer;
  1129. break;
  1130. default:
  1131. ctype = DataControlCellType.DataCell;
  1132. break;
  1133. }
  1134. for (int n=0; n<fields.Length; n++) {
  1135. DataControlField field = fields [n];
  1136. DataControlFieldCell cell;
  1137. if (((field is BoundField) && ((BoundField)field).DataField == RowHeaderColumn) || accessibleHeader)
  1138. cell = new DataControlFieldHeaderCell (field, accessibleHeader ? TableHeaderScope.Column : TableHeaderScope.Row);
  1139. else
  1140. cell = new DataControlFieldCell (field);
  1141. row.Cells.Add (cell);
  1142. field.InitializeCell (cell, ctype, row.RowState, row.RowIndex);
  1143. }
  1144. }
  1145. IOrderedDictionary CreateRowDataKey (GridViewRow row)
  1146. {
  1147. if (cachedKeyProperties == null) {
  1148. PropertyDescriptorCollection props = TypeDescriptor.GetProperties (row.DataItem);
  1149. cachedKeyProperties = new PropertyDescriptor [DataKeyNames.Length];
  1150. for (int n=0; n<DataKeyNames.Length; n++) {
  1151. PropertyDescriptor p = props [DataKeyNames[n]];
  1152. if (p == null)
  1153. new InvalidOperationException ("Property '" + DataKeyNames[n] + "' not found in object of type " + row.DataItem.GetType());
  1154. cachedKeyProperties [n] = p;
  1155. }
  1156. }
  1157. OrderedDictionary dic = new OrderedDictionary ();
  1158. foreach (PropertyDescriptor p in cachedKeyProperties)
  1159. dic [p.Name] = p.GetValue (row.DataItem);
  1160. return dic;
  1161. }
  1162. IOrderedDictionary GetRowValues (GridViewRow row, bool includeReadOnlyFields, bool includePrimaryKey)
  1163. {
  1164. OrderedDictionary dic = new OrderedDictionary ();
  1165. ExtractRowValues (dic, row, includeReadOnlyFields, includePrimaryKey);
  1166. return dic;
  1167. }
  1168. protected virtual void ExtractRowValues (IOrderedDictionary fieldValues, GridViewRow row, bool includeReadOnlyFields, bool includePrimaryKey)
  1169. {
  1170. foreach (TableCell cell in row.Cells) {
  1171. DataControlFieldCell c = cell as DataControlFieldCell;
  1172. if (c != null)
  1173. c.ContainingField.ExtractValuesFromCell (fieldValues, c, row.RowState, includeReadOnlyFields);
  1174. }
  1175. if (!includePrimaryKey && DataKeyNames != null)
  1176. foreach (string key in DataKeyNames)
  1177. fieldValues.Remove (key);
  1178. }
  1179. protected override HtmlTextWriterTag TagKey {
  1180. get {
  1181. if (EnableSortingAndPagingCallbacks)
  1182. return HtmlTextWriterTag.Div;
  1183. else
  1184. return HtmlTextWriterTag.Table;
  1185. }
  1186. }
  1187. public sealed override void DataBind ()
  1188. {
  1189. DataSourceView view = GetData ();
  1190. if (AllowPaging && view.CanPage) {
  1191. SelectArguments.StartRowIndex = PageIndex * PageSize;
  1192. SelectArguments.MaximumRows = PageSize;
  1193. if (view.CanRetrieveTotalRowCount)
  1194. SelectArguments.RetrieveTotalRowCount = true;
  1195. }
  1196. if (sortExpression != "") {
  1197. if (sortDirection == SortDirection.Ascending)
  1198. SelectArguments.SortExpression = sortExpression;
  1199. else
  1200. SelectArguments.SortExpression = sortExpression + " DESC";
  1201. }
  1202. cachedKeyProperties = null;
  1203. base.DataBind ();
  1204. }
  1205. protected internal override void PerformDataBinding (IEnumerable data)
  1206. {
  1207. base.PerformDataBinding (data);
  1208. }
  1209. [MonoTODO]
  1210. protected internal virtual void PrepareControlHierarchy ()
  1211. {
  1212. throw new NotImplementedException ();
  1213. }
  1214. protected internal override void OnInit (EventArgs e)
  1215. {
  1216. Page.RegisterRequiresControlState (this);
  1217. base.OnInit (e);
  1218. }
  1219. void OnFieldsChanged (object sender, EventArgs args)
  1220. {
  1221. RequireBinding ();
  1222. }
  1223. protected override void OnDataPropertyChanged ()
  1224. {
  1225. base.OnDataPropertyChanged ();
  1226. RequireBinding ();
  1227. }
  1228. protected override void OnDataSourceViewChanged (object sender, EventArgs e)
  1229. {
  1230. base.OnDataSourceViewChanged (sender, e);
  1231. RequireBinding ();
  1232. }
  1233. protected override bool OnBubbleEvent (object source, EventArgs e)
  1234. {
  1235. GridViewCommandEventArgs args = e as GridViewCommandEventArgs;
  1236. if (args != null) {
  1237. OnRowCommand (args);
  1238. string param = args.CommandArgument as string;
  1239. if (param == null || param.Length == 0) {
  1240. GridViewRow row = args.Row;
  1241. if (row != null)
  1242. param = row.RowIndex.ToString();
  1243. }
  1244. ProcessEvent (args.CommandName, param);
  1245. }
  1246. return base.OnBubbleEvent (source, e);
  1247. }
  1248. void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
  1249. {
  1250. RaisePostBackEvent (eventArgument);
  1251. }
  1252. // This is prolly obsolete
  1253. protected virtual void RaisePostBackEvent (string eventArgument)
  1254. {
  1255. int i = eventArgument.IndexOf ('$');
  1256. if (i != -1)
  1257. ProcessEvent (eventArgument.Substring (0, i), eventArgument.Substring (i + 1));
  1258. else
  1259. ProcessEvent (eventArgument, null);
  1260. }
  1261. void ProcessEvent (string eventName, string param)
  1262. {
  1263. switch (eventName)
  1264. {
  1265. case DataControlCommands.PageCommandName:
  1266. int newIndex = -1;
  1267. switch (param) {
  1268. case DataControlCommands.FirstPageCommandArgument:
  1269. newIndex = 0;
  1270. break;
  1271. case DataControlCommands.LastPageCommandArgument:
  1272. newIndex = PageCount - 1;
  1273. break;
  1274. case DataControlCommands.NextPageCommandArgument:
  1275. if (PageIndex < PageCount - 1) newIndex = PageIndex + 1;
  1276. break;
  1277. case DataControlCommands.PreviousPageCommandArgument:
  1278. if (PageIndex > 0) newIndex = PageIndex - 1;
  1279. break;
  1280. default:
  1281. newIndex = int.Parse (param) - 1;
  1282. break;
  1283. }
  1284. ShowPage (newIndex);
  1285. break;
  1286. case DataControlCommands.FirstPageCommandArgument:
  1287. ShowPage (0);
  1288. break;
  1289. case DataControlCommands.LastPageCommandArgument:
  1290. ShowPage (PageCount - 1);
  1291. break;
  1292. case DataControlCommands.NextPageCommandArgument:
  1293. if (PageIndex < PageCount - 1)
  1294. ShowPage (PageIndex + 1);
  1295. break;
  1296. case DataControlCommands.PreviousPageCommandArgument:
  1297. if (PageIndex > 0)
  1298. ShowPage (PageIndex - 1);
  1299. break;
  1300. case DataControlCommands.SelectCommandName:
  1301. SelectRow (int.Parse (param));
  1302. break;
  1303. case DataControlCommands.EditCommandName:
  1304. EditRow (int.Parse (param));
  1305. break;
  1306. case DataControlCommands.UpdateCommandName:
  1307. UpdateRow (EditIndex, true);
  1308. break;
  1309. case DataControlCommands.CancelCommandName:
  1310. CancelEdit ();
  1311. break;
  1312. case DataControlCommands.DeleteCommandName:
  1313. DeleteRow (int.Parse (param));
  1314. break;
  1315. case DataControlCommands.SortCommandName:
  1316. Sort (param);
  1317. break;
  1318. }
  1319. }
  1320. void Sort (string newSortExpression)
  1321. {
  1322. SortDirection newDirection;
  1323. if (sortExpression == newSortExpression) {
  1324. if (sortDirection == SortDirection.Ascending)
  1325. newDirection = SortDirection.Descending;
  1326. else
  1327. newDirection = SortDirection.Ascending;
  1328. } else
  1329. newDirection = sortDirection;
  1330. Sort (newSortExpression, newDirection);
  1331. }
  1332. public virtual void Sort (string newSortExpression, SortDirection newSortDirection)
  1333. {
  1334. GridViewSortEventArgs args = new GridViewSortEventArgs (newSortExpression, newSortDirection);
  1335. OnSorting (args);
  1336. if (args.Cancel) return;
  1337. sortExpression = args.SortExpression;
  1338. sortDirection = args.SortDirection;
  1339. RequireBinding ();
  1340. OnSorted (EventArgs.Empty);
  1341. }
  1342. void SelectRow (int index)
  1343. {
  1344. GridViewSelectEventArgs args = new GridViewSelectEventArgs (index);
  1345. OnSelectedIndexChanging (args);
  1346. if (!args.Cancel) {
  1347. SelectedIndex = args.NewSelectedIndex;
  1348. OnSelectedIndexChanged (EventArgs.Empty);
  1349. }
  1350. }
  1351. void ShowPage (int newIndex)
  1352. {
  1353. GridViewPageEventArgs args = new GridViewPageEventArgs (newIndex);
  1354. OnPageIndexChanging (args);
  1355. if (!args.Cancel) {
  1356. EndRowEdit ();
  1357. PageIndex = args.NewPageIndex;
  1358. OnPageIndexChanged (EventArgs.Empty);
  1359. }
  1360. }
  1361. void EditRow (int index)
  1362. {
  1363. GridViewEditEventArgs args = new GridViewEditEventArgs (index);
  1364. OnRowEditing (args);
  1365. if (!args.Cancel) {
  1366. EditIndex = args.NewEditIndex;
  1367. }
  1368. }
  1369. void CancelEdit ()
  1370. {
  1371. GridViewCancelEditEventArgs args = new GridViewCancelEditEventArgs (EditIndex);
  1372. OnRowCancelingEdit (args);
  1373. if (!args.Cancel) {
  1374. EndRowEdit ();
  1375. }
  1376. }
  1377. [MonoTODO ("Support two-way binding expressions")]
  1378. public virtual void UpdateRow (int rowIndex, bool causesValidation)
  1379. {
  1380. if (causesValidation)
  1381. Page.Validate ();
  1382. if (rowIndex != EditIndex) throw new NotSupportedException ();
  1383. currentEditOldValues = oldEditValues.Values;
  1384. GridViewRow row = Rows [rowIndex];
  1385. currentEditRowKeys = DataKeys [rowIndex].Values;
  1386. currentEditNewValues = GetRowValues (row, false, false);
  1387. GridViewUpdateEventArgs args = new GridViewUpdateEventArgs (EditIndex, currentEditRowKeys, currentEditOldValues, currentEditNewValues);
  1388. OnRowUpdating (args);
  1389. if (!args.Cancel) {
  1390. DataSourceView view = GetData ();
  1391. if (view == null) throw new HttpException ("The DataSourceView associated to data bound control was null");
  1392. view.Update (currentEditRowKeys, currentEditNewValues, currentEditOldValues, new DataSourceViewOperationCallback (UpdateCallback));
  1393. } else
  1394. EndRowEdit ();
  1395. }
  1396. bool UpdateCallback (int recordsAffected, Exception exception)
  1397. {
  1398. GridViewUpdatedEventArgs dargs = new GridViewUpdatedEventArgs (recordsAffected, exception, currentEditRowKeys, currentEditOldValues, currentEditNewValues);
  1399. OnRowUpdated (dargs);
  1400. if (!dargs.KeepInEditMode)
  1401. EndRowEdit ();
  1402. return dargs.ExceptionHandled;
  1403. }
  1404. public virtual void DeleteRow (int rowIndex)
  1405. {
  1406. GridViewRow row = Rows [rowIndex];
  1407. currentEditRowKeys = DataKeys [rowIndex].Values;
  1408. currentEditNewValues = GetRowValues (row, true, true);
  1409. GridViewDeleteEventArgs args = new GridViewDeleteEventArgs (rowIndex, currentEditRowKeys, currentEditNewValues);
  1410. OnRowDeleting (args);
  1411. if (!args.Cancel) {
  1412. RequireBinding ();
  1413. DataSourceView view = GetData ();
  1414. if (view != null)
  1415. view.Delete (currentEditRowKeys, currentEditNewValues, new DataSourceViewOperationCallback (DeleteCallback));
  1416. else {
  1417. GridViewDeletedEventArgs dargs = new GridViewDeletedEventArgs (0, null, currentEditRowKeys, currentEditNewValues);
  1418. OnRowDeleted (dargs);
  1419. }
  1420. }
  1421. }
  1422. bool DeleteCallback (int recordsAffected, Exception exception)
  1423. {
  1424. GridViewDeletedEventArgs dargs = new GridViewDeletedEventArgs (recordsAffected, exception, currentEditRowKeys, currentEditNewValues);
  1425. OnRowDeleted (dargs);
  1426. return dargs.ExceptionHandled;
  1427. }
  1428. void EndRowEdit ()
  1429. {
  1430. EditIndex = -1;
  1431. oldEditValues = new DataKey (new OrderedDictionary ());
  1432. currentEditRowKeys = null;
  1433. currentEditOldValues = null;
  1434. currentEditNewValues = null;
  1435. }
  1436. protected internal override void LoadControlState (object ob)
  1437. {
  1438. if (ob == null) return;
  1439. object[] state = (object[]) ob;
  1440. base.LoadControlState (state[0]);
  1441. pageIndex = (int) state[1];
  1442. pageCount = (int) state[2];
  1443. selectedIndex = (int) state[3];
  1444. editIndex = (int) state[4];
  1445. sortExpression = (string) state[5];
  1446. sortDirection = (SortDirection) state[6];
  1447. }
  1448. protected internal override object SaveControlState ()
  1449. {
  1450. object bstate = base.SaveControlState ();
  1451. return new object[] {
  1452. bstate, pageIndex, pageCount, selectedIndex, editIndex, sortExpression, sortDirection
  1453. };
  1454. }
  1455. protected override void TrackViewState()
  1456. {
  1457. base.TrackViewState();
  1458. if (columns != null) ((IStateManager)columns).TrackViewState();
  1459. if (pagerSettings != null) ((IStateManager)pagerSettings).TrackViewState();
  1460. if (alternatingRowStyle != null) ((IStateManager)alternatingRowStyle).TrackViewState();
  1461. if (footerStyle != null) ((IStateManager)footerStyle).TrackViewState();
  1462. if (headerStyle != null) ((IStateManager)headerStyle).TrackViewState();
  1463. if (pagerStyle != null) ((IStateManager)pagerStyle).TrackViewState();
  1464. if (rowStyle != null) ((IStateManager)rowStyle).TrackViewState();
  1465. if (selectedRowStyle != null) ((IStateManager)selectedRowStyle).TrackViewState();
  1466. if (editRowStyle != null) ((IStateManager)editRowStyle).TrackViewState();
  1467. if (emptyDataRowStyle != null) ((IStateManager)emptyDataRowStyle).TrackViewState();
  1468. if (keys != null) ((IStateManager)keys).TrackViewState();
  1469. if (autoFieldProperties != null) {
  1470. foreach (IStateManager sm in autoFieldProperties)
  1471. sm.TrackViewState ();
  1472. }
  1473. }
  1474. protected override object SaveViewState()
  1475. {
  1476. object[] states = new object [14];
  1477. states[0] = base.SaveViewState();
  1478. states[1] = (columns == null ? null : ((IStateManager)columns).SaveViewState());
  1479. states[2] = (pagerSettings == null ? null : ((IStateManager)pagerSettings).SaveViewState());
  1480. states[3] = (alternatingRowStyle == null ? null : ((IStateManager)alternatingRowStyle).SaveViewState());
  1481. states[4] = (footerStyle == null ? null : ((IStateManager)footerStyle).SaveViewState());
  1482. states[5] = (headerStyle == null ? null : ((IStateManager)headerStyle).SaveViewState());
  1483. states[6] = (pagerStyle == null ? null : ((IStateManager)pagerStyle).SaveViewState());
  1484. states[7] = (rowStyle == null ? null : ((IStateManager)rowStyle).SaveViewState());
  1485. states[8] = (selectedRowStyle == null ? null : ((IStateManager)selectedRowStyle).SaveViewState());
  1486. states[9] = (editRowStyle == null ? null : ((IStateManager)editRowStyle).SaveViewState());
  1487. states[10] = (emptyDataRowStyle == null ? null : ((IStateManager)emptyDataRowStyle).SaveViewState());
  1488. states[11] = (keys == null ? null : ((IStateManager)keys).SaveViewState());
  1489. states[12] = (oldEditValues == null ? null : ((IStateManager)oldEditValues).SaveViewState());
  1490. if (autoFieldProperties != null) {
  1491. object[] data = new object [autoFieldProperties.Length];
  1492. bool allNull = true;
  1493. for (int n=0; n<data.Length; n++) {
  1494. data [n] = ((IStateManager)autoFieldProperties [n]).SaveViewState ();
  1495. if (data [n] != null) allNull = false;
  1496. }
  1497. if (!allNull) states [13] = data;
  1498. }
  1499. for (int i = states.Length - 1; i >= 0; i--) {
  1500. if (states [i] != null)
  1501. return states;
  1502. }
  1503. return null;
  1504. }
  1505. protected override void LoadViewState (object savedState)
  1506. {
  1507. if (savedState == null) {
  1508. base.LoadViewState (null);
  1509. return;
  1510. }
  1511. object [] states = (object []) savedState;
  1512. if (states[13] != null) {
  1513. object[] data = (object[]) states [13];
  1514. autoFieldProperties = new AutoGeneratedFieldProperties [data.Length];
  1515. for (int n=0; n<data.Length; n++) {
  1516. IStateManager p = new AutoGeneratedFieldProperties ();
  1517. p.TrackViewState ();
  1518. p.LoadViewState (data [n]);
  1519. autoFieldProperties [n] = (AutoGeneratedFieldProperties) p;
  1520. }
  1521. }
  1522. base.LoadViewState (states[0]);
  1523. EnsureChildControls ();
  1524. if (states[1] != null) ((IStateManager)Columns).LoadViewState (states[1]);
  1525. if (states[2] != null) ((IStateManager)PagerSettings).LoadViewState (states[2]);
  1526. if (states[3] != null) ((IStateManager)AlternatingRowStyle).LoadViewState (states[3]);
  1527. if (states[4] != null) ((IStateManager)FooterStyle).LoadViewState (states[4]);
  1528. if (states[5] != null) ((IStateManager)HeaderStyle).LoadViewState (states[5]);
  1529. if (states[6] != null) ((IStateManager)PagerStyle).LoadViewState (states[6]);
  1530. if (states[7] != null) ((IStateManager)RowStyle).LoadViewState (states[7]);
  1531. if (states[8] != null) ((IStateManager)SelectedRowStyle).LoadViewState (states[8]);
  1532. if (states[9] != null) ((IStateManager)EditRowStyle).LoadViewState (states[9]);
  1533. if (states[10] != null) ((IStateManager)EmptyDataRowStyle).LoadViewState (states[10]);
  1534. if (states[11] != null) ((IStateManager)DataKeys).LoadViewState (states[11]);
  1535. if (states[12] != null && oldEditValues != null) ((IStateManager)oldEditValues).LoadViewState (states[12]);
  1536. }
  1537. void ICallbackEventHandler.RaiseCallbackEvent (string eventArgs)
  1538. {
  1539. RaiseCallbackEvent (eventArgs);
  1540. }
  1541. string callbackResult;
  1542. protected virtual void RaiseCallbackEvent (string eventArgs)
  1543. {
  1544. string[] clientData = eventArgs.Split ('|');
  1545. pageIndex = int.Parse (clientData[0]);
  1546. sortExpression = HttpUtility.UrlDecode (clientData[1]);
  1547. if (sortExpression == "") sortExpression = null;
  1548. RequireBinding ();
  1549. RaisePostBackEvent (clientData[2]);
  1550. EnsureDataBound ();
  1551. StringWriter sw = new StringWriter ();
  1552. sw.Write (PageIndex.ToString() + '|' + SortExpression + '|');
  1553. HtmlTextWriter writer = new HtmlTextWriter (sw);
  1554. RenderGrid (writer);
  1555. callbackResult = sw.ToString ();
  1556. }
  1557. string ICallbackEventHandler.GetCallbackResult ()
  1558. {
  1559. return GetCallbackResult ();
  1560. }
  1561. protected virtual string GetCallbackResult ()
  1562. {
  1563. return callbackResult;
  1564. }
  1565. string ICallbackContainer.GetCallbackScript (IButtonControl control, string argument)
  1566. {
  1567. return GetCallbackScript (control, argument);
  1568. }
  1569. protected virtual string GetCallbackScript (IButtonControl control, string argument)
  1570. {
  1571. if (EnableSortingAndPagingCallbacks)
  1572. return "javascript:GridView_ClientEvent (\"" + ClientID + "\",\"" + control.CommandName + "$" + control.CommandArgument + "\"); return false;";
  1573. else
  1574. return null;
  1575. }
  1576. [MonoTODO]
  1577. protected override void OnPagePreLoad (object sender, EventArgs e)
  1578. {
  1579. base.OnPagePreLoad (sender, e);
  1580. }
  1581. protected internal override void OnPreRender (EventArgs e)
  1582. {
  1583. base.OnPreRender (e);
  1584. if (EnableSortingAndPagingCallbacks)
  1585. {
  1586. if (!Page.ClientScript.IsClientScriptIncludeRegistered (typeof(GridView), "GridView.js")) {
  1587. string url = Page.ClientScript.GetWebResourceUrl (typeof(GridView), "GridView.js");
  1588. Page.ClientScript.RegisterClientScriptInclude (typeof(GridView), "GridView.js", url);
  1589. }
  1590. string cgrid = ClientID + "_data";
  1591. string script = string.Format ("var {0} = new Object ();\n", cgrid);
  1592. script += string.Format ("{0}.pageIndex = {1};\n", cgrid, ClientScriptManager.GetScriptLiteral (PageIndex));
  1593. script += string.Format ("{0}.sortExp = {1};\n", cgrid, ClientScriptManager.GetScriptLiteral (SortExpression == null ? "" : SortExpression));
  1594. script += string.Format ("{0}.uid = {1};\n", cgrid, ClientScriptManager.GetScriptLiteral (UniqueID));
  1595. Page.ClientScript.RegisterStartupScript (typeof(TreeView), this.UniqueID, script, true);
  1596. // Make sure the basic script infrastructure is rendered
  1597. Page.ClientScript.GetCallbackEventReference (this, "null", "", "null");
  1598. Page.ClientScript.GetPostBackClientHyperlink (this, "");
  1599. }
  1600. }
  1601. protected internal override void Render (HtmlTextWriter writer)
  1602. {
  1603. if (EnableSortingAndPagingCallbacks)
  1604. base.RenderBeginTag (writer);
  1605. RenderGrid (writer);
  1606. if (EnableSortingAndPagingCallbacks)
  1607. base.RenderEndTag (writer);
  1608. }
  1609. void RenderGrid (HtmlTextWriter writer)
  1610. {
  1611. switch (GridLines) {
  1612. case GridLines.Horizontal:
  1613. writer.AddAttribute (HtmlTextWriterAttribute.Rules, "rows");
  1614. writer.AddAttribute (HtmlTextWriterAttribute.Border, "1");
  1615. break;
  1616. case GridLines.Vertical:
  1617. writer.AddAttribute (HtmlTextWriterAttribute.Rules, "cols");
  1618. writer.AddAttribute (HtmlTextWriterAttribute.Border, "1");
  1619. break;
  1620. case GridLines.Both:
  1621. writer.AddAttribute (HtmlTextWriterAttribute.Rules, "all");
  1622. writer.AddAttribute (HtmlTextWriterAttribute.Border, "1");
  1623. break;
  1624. default:
  1625. writer.AddAttribute (HtmlTextWriterAttribute.Border, "0");
  1626. break;
  1627. }
  1628. writer.AddAttribute (HtmlTextWriterAttribute.Cellspacing, "0");
  1629. writer.AddStyleAttribute (HtmlTextWriterStyle.BorderCollapse, "collapse");
  1630. table.RenderBeginTag (writer);
  1631. foreach (GridViewRow row in table.Rows)
  1632. {
  1633. switch (row.RowType) {
  1634. case DataControlRowType.Header:
  1635. if (!ShowHeader) continue;
  1636. if (headerStyle != null)headerStyle.AddAttributesToRender (writer, row);
  1637. break;
  1638. case DataControlRowType.Footer:
  1639. if (!ShowFooter) continue;
  1640. if (footerStyle != null) footerStyle.AddAttributesToRender (writer, row);
  1641. break;
  1642. case DataControlRowType.Pager:
  1643. if (pagerStyle != null) pagerStyle.AddAttributesToRender (writer, row);
  1644. break;
  1645. case DataControlRowType.EmptyDataRow:
  1646. if (emptyDataRowStyle != null) emptyDataRowStyle.AddAttributesToRender (writer, row);
  1647. break;
  1648. default:
  1649. break;
  1650. }
  1651. if ((row.RowState & DataControlRowState.Normal) != 0 && rowStyle != null)
  1652. rowStyle.AddAttributesToRender (writer, row);
  1653. if ((row.RowState & DataControlRowState.Alternate) != 0 && alternatingRowStyle != null)
  1654. alternatingRowStyle.AddAttributesToRender (writer, row);
  1655. if ((row.RowState & DataControlRowState.Edit) != 0 && editRowStyle != null)
  1656. editRowStyle.AddAttributesToRender (writer, row);
  1657. if ((row.RowState & DataControlRowState.Selected) != 0 && selectedRowStyle != null)
  1658. selectedRowStyle.AddAttributesToRender (writer, row);
  1659. row.RenderBeginTag (writer);
  1660. foreach (TableCell cell in row.Cells) {
  1661. DataControlFieldCell fcell = cell as DataControlFieldCell;
  1662. if (fcell != null) {
  1663. Style cellStyle = null;
  1664. switch (row.RowType) {
  1665. case DataControlRowType.Header: cellStyle = fcell.ContainingField.HeaderStyle; break;
  1666. case DataControlRowType.Footer: cellStyle = fcell.ContainingField.FooterStyle; break;
  1667. default: cellStyle = fcell.ContainingField.ItemStyle; break;
  1668. }
  1669. if (cellStyle != null)
  1670. cellStyle.AddAttributesToRender (writer, cell);
  1671. }
  1672. cell.Render (writer);
  1673. }
  1674. row.RenderEndTag (writer);
  1675. }
  1676. table.RenderEndTag (writer);
  1677. }
  1678. [MonoTODO]
  1679. PostBackOptions IPostBackContainer.GetPostBackOptions (IButtonControl control)
  1680. {
  1681. throw new NotImplementedException ();
  1682. }
  1683. }
  1684. }
  1685. #endif