GridView.cs 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546
  1. //
  2. // System.Web.UI.WebControls.GridView.cs
  3. //
  4. // Authors:
  5. // Lluis Sanchez Gual ([email protected])
  6. //
  7. // Copyright (C) 2005-2010 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. using System;
  29. using System.Collections;
  30. using System.Collections.Generic;
  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, IPersistedSelector
  47. #if NET_4_0
  48. , IDataKeysControl, IDataBoundListControl, IDataBoundControl, IFieldControl
  49. #endif
  50. {
  51. Table table;
  52. GridViewRowCollection rows;
  53. GridViewRow bottomPagerRow;
  54. GridViewRow topPagerRow;
  55. IOrderedDictionary currentEditRowKeys;
  56. IOrderedDictionary currentEditNewValues;
  57. IOrderedDictionary currentEditOldValues;
  58. ITemplate pagerTemplate;
  59. ITemplate emptyDataTemplate;
  60. PropertyDescriptor[] cachedKeyProperties;
  61. #if NET_4_0
  62. PropertyDescriptor[] cachedSuffixKeyProperties;
  63. #endif
  64. // View state
  65. DataControlFieldCollection columns;
  66. PagerSettings pagerSettings;
  67. TableItemStyle alternatingRowStyle;
  68. TableItemStyle editRowStyle;
  69. TableItemStyle emptyDataRowStyle;
  70. TableItemStyle footerStyle;
  71. TableItemStyle headerStyle;
  72. TableItemStyle pagerStyle;
  73. TableItemStyle rowStyle;
  74. TableItemStyle selectedRowStyle;
  75. #if NET_4_0
  76. TableItemStyle sortedAscendingCellStyle;
  77. TableItemStyle sortedAscendingHeaderStyle;
  78. TableItemStyle sortedDescendingCellStyle;
  79. TableItemStyle sortedDescendingHeaderStyle;
  80. List <DataKey> _dataKeySuffixList;
  81. DataKeyArray rowSuffixKeys;
  82. #endif
  83. List <DataKey> _dataKeyList;
  84. DataKeyArray keys;
  85. DataKey oldEditValues;
  86. AutoGeneratedFieldProperties[] autoFieldProperties;
  87. string [] dataKeyNames = null;
  88. readonly string[] emptyKeys = new string[0];
  89. IEnumerator _dataEnumerator;
  90. static readonly object PageIndexChangedEvent = new object();
  91. static readonly object PageIndexChangingEvent = new object();
  92. static readonly object RowCancelingEditEvent = new object();
  93. static readonly object RowCommandEvent = new object();
  94. static readonly object RowCreatedEvent = new object();
  95. static readonly object RowDataBoundEvent = new object();
  96. static readonly object RowDeletedEvent = new object();
  97. static readonly object RowDeletingEvent = new object();
  98. static readonly object RowEditingEvent = new object();
  99. static readonly object RowUpdatedEvent = new object();
  100. static readonly object RowUpdatingEvent = new object();
  101. static readonly object SelectedIndexChangedEvent = new object();
  102. static readonly object SelectedIndexChangingEvent = new object();
  103. static readonly object SortedEvent = new object();
  104. static readonly object SortingEvent = new object();
  105. // Control state
  106. int pageIndex;
  107. int selectedIndex = -1;
  108. int editIndex = -1;
  109. int pageCount = 0;
  110. SortDirection sortDirection = SortDirection.Ascending;
  111. string sortExpression;
  112. public GridView ()
  113. {
  114. EnableModelValidation = true;
  115. }
  116. public event EventHandler PageIndexChanged {
  117. add { Events.AddHandler (PageIndexChangedEvent, value); }
  118. remove { Events.RemoveHandler (PageIndexChangedEvent, value); }
  119. }
  120. public event GridViewPageEventHandler PageIndexChanging {
  121. add { Events.AddHandler (PageIndexChangingEvent, value); }
  122. remove { Events.RemoveHandler (PageIndexChangingEvent, value); }
  123. }
  124. public event GridViewCancelEditEventHandler RowCancelingEdit {
  125. add { Events.AddHandler (RowCancelingEditEvent, value); }
  126. remove { Events.RemoveHandler (RowCancelingEditEvent, value); }
  127. }
  128. public event GridViewCommandEventHandler RowCommand {
  129. add { Events.AddHandler (RowCommandEvent, value); }
  130. remove { Events.RemoveHandler (RowCommandEvent, value); }
  131. }
  132. public event GridViewRowEventHandler RowCreated {
  133. add { Events.AddHandler (RowCreatedEvent, value); }
  134. remove { Events.RemoveHandler (RowCreatedEvent, value); }
  135. }
  136. public event GridViewRowEventHandler RowDataBound {
  137. add { Events.AddHandler (RowDataBoundEvent, value); }
  138. remove { Events.RemoveHandler (RowDataBoundEvent, value); }
  139. }
  140. public event GridViewDeletedEventHandler RowDeleted {
  141. add { Events.AddHandler (RowDeletedEvent, value); }
  142. remove { Events.RemoveHandler (RowDeletedEvent, value); }
  143. }
  144. public event GridViewDeleteEventHandler RowDeleting {
  145. add { Events.AddHandler (RowDeletingEvent, value); }
  146. remove { Events.RemoveHandler (RowDeletingEvent, value); }
  147. }
  148. public event GridViewEditEventHandler RowEditing {
  149. add { Events.AddHandler (RowEditingEvent, value); }
  150. remove { Events.RemoveHandler (RowEditingEvent, value); }
  151. }
  152. public event GridViewUpdatedEventHandler RowUpdated {
  153. add { Events.AddHandler (RowUpdatedEvent, value); }
  154. remove { Events.RemoveHandler (RowUpdatedEvent, value); }
  155. }
  156. public event GridViewUpdateEventHandler RowUpdating {
  157. add { Events.AddHandler (RowUpdatingEvent, value); }
  158. remove { Events.RemoveHandler (RowUpdatingEvent, value); }
  159. }
  160. public event EventHandler SelectedIndexChanged {
  161. add { Events.AddHandler (SelectedIndexChangedEvent, value); }
  162. remove { Events.RemoveHandler (SelectedIndexChangedEvent, value); }
  163. }
  164. public event GridViewSelectEventHandler SelectedIndexChanging {
  165. add { Events.AddHandler (SelectedIndexChangingEvent, value); }
  166. remove { Events.RemoveHandler (SelectedIndexChangingEvent, value); }
  167. }
  168. public event EventHandler Sorted {
  169. add { Events.AddHandler (SortedEvent, value); }
  170. remove { Events.RemoveHandler (SortedEvent, value); }
  171. }
  172. public event GridViewSortEventHandler Sorting {
  173. add { Events.AddHandler (SortingEvent, value); }
  174. remove { Events.RemoveHandler (SortingEvent, value); }
  175. }
  176. protected virtual void OnPageIndexChanged (EventArgs e)
  177. {
  178. if (Events != null) {
  179. EventHandler eh = (EventHandler) Events [PageIndexChangedEvent];
  180. if (eh != null) eh (this, e);
  181. }
  182. }
  183. protected virtual void OnPageIndexChanging (GridViewPageEventArgs e)
  184. {
  185. if (Events != null) {
  186. GridViewPageEventHandler eh = (GridViewPageEventHandler) Events [PageIndexChangingEvent];
  187. if (eh != null) {
  188. eh (this, e);
  189. return;
  190. }
  191. }
  192. if (!IsBoundUsingDataSourceID)
  193. throw new HttpException (String.Format ("The GridView '{0}' fired event PageIndexChanging which wasn't handled.", ID));
  194. }
  195. protected virtual void OnRowCancelingEdit (GridViewCancelEditEventArgs e)
  196. {
  197. if (Events != null) {
  198. GridViewCancelEditEventHandler eh = (GridViewCancelEditEventHandler) Events [RowCancelingEditEvent];
  199. if (eh != null) {
  200. eh (this, e);
  201. return;
  202. }
  203. }
  204. if (!IsBoundUsingDataSourceID)
  205. throw new HttpException (String.Format ("The GridView '{0}' fired event RowCancelingEdit which wasn't handled.", ID));
  206. }
  207. protected virtual void OnRowCommand (GridViewCommandEventArgs e)
  208. {
  209. if (Events != null) {
  210. GridViewCommandEventHandler eh = (GridViewCommandEventHandler) Events [RowCommandEvent];
  211. if (eh != null) eh (this, e);
  212. }
  213. }
  214. protected virtual void OnRowCreated (GridViewRowEventArgs e)
  215. {
  216. if (Events != null) {
  217. GridViewRowEventHandler eh = (GridViewRowEventHandler) Events [RowCreatedEvent];
  218. if (eh != null) eh (this, e);
  219. }
  220. }
  221. protected virtual void OnRowDataBound (GridViewRowEventArgs e)
  222. {
  223. if (Events != null) {
  224. GridViewRowEventHandler eh = (GridViewRowEventHandler) Events [RowDataBoundEvent];
  225. if (eh != null) eh (this, e);
  226. }
  227. }
  228. protected virtual void OnRowDeleted (GridViewDeletedEventArgs e)
  229. {
  230. if (Events != null) {
  231. GridViewDeletedEventHandler eh = (GridViewDeletedEventHandler) Events [RowDeletedEvent];
  232. if (eh != null) eh (this, e);
  233. }
  234. }
  235. protected virtual void OnRowDeleting (GridViewDeleteEventArgs e)
  236. {
  237. if (Events != null) {
  238. GridViewDeleteEventHandler eh = (GridViewDeleteEventHandler) Events [RowDeletingEvent];
  239. if (eh != null) {
  240. eh (this, e);
  241. return;
  242. }
  243. }
  244. if (!IsBoundUsingDataSourceID)
  245. throw new HttpException (String.Format ("The GridView '{0}' fired event RowDeleting which wasn't handled.", ID));
  246. }
  247. protected virtual void OnRowEditing (GridViewEditEventArgs e)
  248. {
  249. if (Events != null) {
  250. GridViewEditEventHandler eh = (GridViewEditEventHandler) Events [RowEditingEvent];
  251. if (eh != null) {
  252. eh (this, e);
  253. return;
  254. }
  255. }
  256. if (!IsBoundUsingDataSourceID)
  257. throw new HttpException (String.Format ("The GridView '{0}' fired event RowEditing which wasn't handled.", ID));
  258. }
  259. protected virtual void OnRowUpdated (GridViewUpdatedEventArgs e)
  260. {
  261. if (Events != null) {
  262. GridViewUpdatedEventHandler eh = (GridViewUpdatedEventHandler) Events [RowUpdatedEvent];
  263. if (eh != null) eh (this, e);
  264. }
  265. }
  266. protected virtual void OnRowUpdating (GridViewUpdateEventArgs e)
  267. {
  268. if (Events != null) {
  269. GridViewUpdateEventHandler eh = (GridViewUpdateEventHandler) Events [RowUpdatingEvent];
  270. if (eh != null) {
  271. eh (this, e);
  272. return;
  273. }
  274. }
  275. if (!IsBoundUsingDataSourceID)
  276. throw new HttpException (String.Format ("The GridView '{0}' fired event RowUpdating which wasn't handled.", ID));
  277. }
  278. protected virtual void OnSelectedIndexChanged (EventArgs e)
  279. {
  280. if (Events != null) {
  281. EventHandler eh = (EventHandler) Events [SelectedIndexChangedEvent];
  282. if (eh != null) eh (this, e);
  283. }
  284. }
  285. protected virtual void OnSelectedIndexChanging (GridViewSelectEventArgs e)
  286. {
  287. if (Events != null) {
  288. GridViewSelectEventHandler eh = (GridViewSelectEventHandler) Events [SelectedIndexChangingEvent];
  289. if (eh != null) eh (this, e);
  290. }
  291. }
  292. protected virtual void OnSorted (EventArgs e)
  293. {
  294. if (Events != null) {
  295. EventHandler eh = (EventHandler) Events [SortedEvent];
  296. if (eh != null) eh (this, e);
  297. }
  298. }
  299. protected virtual void OnSorting (GridViewSortEventArgs e)
  300. {
  301. if (Events != null) {
  302. GridViewSortEventHandler eh = (GridViewSortEventHandler) Events [SortingEvent];
  303. if (eh != null) {
  304. eh (this, e);
  305. return;
  306. }
  307. }
  308. if (!IsBoundUsingDataSourceID)
  309. throw new HttpException (String.Format ("The GridView '{0}' fired event Sorting which wasn't handled.", ID));
  310. }
  311. [WebCategoryAttribute ("Paging")]
  312. [DefaultValueAttribute (false)]
  313. public virtual bool AllowPaging {
  314. get {
  315. object ob = ViewState ["AllowPaging"];
  316. if (ob != null)
  317. return (bool) ob;
  318. return false;
  319. }
  320. set {
  321. if (value == AllowPaging)
  322. return;
  323. ViewState ["AllowPaging"] = value;
  324. RequireBinding ();
  325. }
  326. }
  327. [WebCategoryAttribute ("Behavior")]
  328. [DefaultValueAttribute (false)]
  329. public virtual bool AllowSorting {
  330. get {
  331. object ob = ViewState ["AllowSorting"];
  332. if (ob != null)
  333. return (bool) ob;
  334. return false;
  335. }
  336. set {
  337. if (value == AllowSorting)
  338. return;
  339. ViewState ["AllowSorting"] = value;
  340. RequireBinding ();
  341. }
  342. }
  343. [WebCategoryAttribute ("Styles")]
  344. [PersistenceMode (PersistenceMode.InnerProperty)]
  345. [NotifyParentProperty (true)]
  346. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  347. public TableItemStyle AlternatingRowStyle {
  348. get {
  349. if (alternatingRowStyle == null) {
  350. alternatingRowStyle = new TableItemStyle ();
  351. if (IsTrackingViewState)
  352. alternatingRowStyle.TrackViewState();
  353. }
  354. return alternatingRowStyle;
  355. }
  356. }
  357. [WebCategoryAttribute ("Behavior")]
  358. [DefaultValueAttribute (false)]
  359. public virtual bool AutoGenerateEditButton {
  360. get {
  361. object ob = ViewState ["AutoGenerateEditButton"];
  362. if (ob != null)
  363. return (bool) ob;
  364. return false;
  365. }
  366. set {
  367. if (value == AutoGenerateEditButton)
  368. return;
  369. ViewState ["AutoGenerateEditButton"] = value;
  370. RequireBinding ();
  371. }
  372. }
  373. [WebCategoryAttribute ("Behavior")]
  374. [DefaultValueAttribute (false)]
  375. public virtual bool AutoGenerateDeleteButton {
  376. get {
  377. object ob = ViewState ["AutoGenerateDeleteButton"];
  378. if (ob != null)
  379. return (bool) ob;
  380. return false;
  381. }
  382. set {
  383. if (value == AutoGenerateDeleteButton)
  384. return;
  385. ViewState ["AutoGenerateDeleteButton"] = value;
  386. RequireBinding ();
  387. }
  388. }
  389. [WebCategoryAttribute ("Behavior")]
  390. [DefaultValueAttribute (false)]
  391. public virtual bool AutoGenerateSelectButton {
  392. get {
  393. object ob = ViewState ["AutoGenerateSelectButton"];
  394. if (ob != null)
  395. return (bool) ob;
  396. return false;
  397. }
  398. set {
  399. if (value == AutoGenerateSelectButton)
  400. return;
  401. ViewState ["AutoGenerateSelectButton"] = value;
  402. RequireBinding ();
  403. }
  404. }
  405. [WebCategoryAttribute ("Behavior")]
  406. [DefaultValueAttribute (true)]
  407. public virtual bool AutoGenerateColumns {
  408. get {
  409. object ob = ViewState ["AutoGenerateColumns"];
  410. if (ob != null)
  411. return (bool) ob;
  412. return true;
  413. }
  414. set {
  415. if (value == AutoGenerateColumns)
  416. return;
  417. ViewState ["AutoGenerateColumns"] = value;
  418. RequireBinding ();
  419. }
  420. }
  421. [UrlPropertyAttribute]
  422. [WebCategoryAttribute ("Appearance")]
  423. [DefaultValueAttribute ("")]
  424. [EditorAttribute ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  425. public virtual string BackImageUrl {
  426. get {
  427. if (ControlStyleCreated)
  428. return ((TableStyle) ControlStyle).BackImageUrl;
  429. return String.Empty;
  430. }
  431. set { ((TableStyle) ControlStyle).BackImageUrl = value; }
  432. }
  433. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  434. [BrowsableAttribute (false)]
  435. public virtual GridViewRow BottomPagerRow {
  436. get {
  437. EnsureDataBound ();
  438. return bottomPagerRow;
  439. }
  440. }
  441. [WebCategoryAttribute ("Accessibility")]
  442. [DefaultValueAttribute ("")]
  443. [LocalizableAttribute (true)]
  444. public virtual string Caption {
  445. get {
  446. object ob = ViewState ["Caption"];
  447. if (ob != null)
  448. return (string) ob;
  449. return String.Empty;
  450. }
  451. set { ViewState ["Caption"] = value; }
  452. }
  453. [WebCategoryAttribute ("Accessibility")]
  454. [DefaultValueAttribute (TableCaptionAlign.NotSet)]
  455. public virtual TableCaptionAlign CaptionAlign
  456. {
  457. get {
  458. object o = ViewState ["CaptionAlign"];
  459. if(o != null)
  460. return (TableCaptionAlign) o;
  461. return TableCaptionAlign.NotSet;
  462. }
  463. set { ViewState ["CaptionAlign"] = value; }
  464. }
  465. [WebCategoryAttribute ("Layout")]
  466. [DefaultValueAttribute (-1)]
  467. public virtual int CellPadding
  468. {
  469. get {
  470. if (ControlStyleCreated)
  471. return ((TableStyle) ControlStyle).CellPadding;
  472. return -1;
  473. }
  474. set { ((TableStyle) ControlStyle).CellPadding = value; }
  475. }
  476. [WebCategoryAttribute ("Layout")]
  477. [DefaultValueAttribute (0)]
  478. public virtual int CellSpacing
  479. {
  480. get {
  481. if (ControlStyleCreated)
  482. return ((TableStyle) ControlStyle).CellSpacing;
  483. return 0;
  484. }
  485. set { ((TableStyle) ControlStyle).CellSpacing = value; }
  486. }
  487. [EditorAttribute ("System.Web.UI.Design.WebControls.DataControlFieldTypeEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  488. [MergablePropertyAttribute (false)]
  489. [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
  490. [DefaultValueAttribute (null)]
  491. [WebCategoryAttribute ("Misc")]
  492. public virtual DataControlFieldCollection Columns {
  493. get {
  494. if (columns == null) {
  495. columns = new DataControlFieldCollection ();
  496. columns.FieldsChanged += new EventHandler (OnFieldsChanged);
  497. if (IsTrackingViewState)
  498. ((IStateManager)columns).TrackViewState ();
  499. }
  500. return columns;
  501. }
  502. }
  503. [BrowsableAttribute(false)]
  504. #if NET_4_0
  505. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  506. #endif
  507. public IAutoFieldGenerator ColumnsGenerator {
  508. get;
  509. set;
  510. }
  511. [DefaultValueAttribute (null)]
  512. [WebCategoryAttribute ("Data")]
  513. [TypeConverter (typeof(StringArrayConverter))]
  514. [EditorAttribute ("System.Web.UI.Design.WebControls.DataFieldEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  515. public virtual string[] DataKeyNames {
  516. get {
  517. if (dataKeyNames != null)
  518. return dataKeyNames;
  519. return emptyKeys;
  520. }
  521. set {
  522. dataKeyNames = value;
  523. RequireBinding ();
  524. }
  525. }
  526. List <DataKey> DataKeyList {
  527. get {
  528. if (_dataKeyList == null)
  529. _dataKeyList = new List <DataKey> ();
  530. return _dataKeyList;
  531. }
  532. }
  533. #if NET_4_0
  534. List <DataKey> DataKeySuffixList {
  535. get {
  536. if (_dataKeySuffixList == null)
  537. _dataKeySuffixList = new List <DataKey> ();
  538. return _dataKeySuffixList;
  539. }
  540. }
  541. #endif
  542. [BrowsableAttribute (false)]
  543. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  544. public virtual DataKeyArray DataKeys {
  545. get {
  546. if (keys == null) {
  547. keys = new DataKeyArray (DataKeyList);
  548. if (IsTrackingViewState)
  549. ((IStateManager) keys).TrackViewState ();
  550. }
  551. return keys;
  552. }
  553. }
  554. DataKey OldEditValues {
  555. get {
  556. if (oldEditValues == null) {
  557. oldEditValues = new DataKey (new OrderedDictionary ());
  558. }
  559. return oldEditValues;
  560. }
  561. }
  562. [WebCategoryAttribute ("Misc")]
  563. [DefaultValueAttribute (-1)]
  564. public virtual int EditIndex {
  565. get { return editIndex; }
  566. set {
  567. if (value == editIndex)
  568. return;
  569. editIndex = value;
  570. RequireBinding ();
  571. }
  572. }
  573. [WebCategoryAttribute ("Styles")]
  574. [PersistenceMode (PersistenceMode.InnerProperty)]
  575. [NotifyParentProperty (true)]
  576. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  577. public TableItemStyle EditRowStyle {
  578. get {
  579. if (editRowStyle == null) {
  580. editRowStyle = new TableItemStyle ();
  581. if (IsTrackingViewState)
  582. editRowStyle.TrackViewState();
  583. }
  584. return editRowStyle;
  585. }
  586. }
  587. [WebCategoryAttribute ("Styles")]
  588. [PersistenceMode (PersistenceMode.InnerProperty)]
  589. [NotifyParentProperty (true)]
  590. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  591. public TableItemStyle EmptyDataRowStyle {
  592. get {
  593. if (emptyDataRowStyle == null) {
  594. emptyDataRowStyle = new TableItemStyle ();
  595. if (IsTrackingViewState)
  596. emptyDataRowStyle.TrackViewState();
  597. }
  598. return emptyDataRowStyle;
  599. }
  600. }
  601. [DefaultValue (null)]
  602. [TemplateContainer (typeof(GridViewRow), BindingDirection.OneWay)]
  603. [PersistenceMode (PersistenceMode.InnerProperty)]
  604. [Browsable (false)]
  605. public virtual ITemplate EmptyDataTemplate {
  606. get { return emptyDataTemplate; }
  607. set { emptyDataTemplate = value; }
  608. }
  609. [LocalizableAttribute (true)]
  610. [WebCategoryAttribute ("Appearance")]
  611. [DefaultValueAttribute ("")]
  612. public virtual string EmptyDataText {
  613. get {
  614. object ob = ViewState ["EmptyDataText"];
  615. if (ob != null)
  616. return (string) ob;
  617. return String.Empty;
  618. }
  619. set {
  620. if (value == EmptyDataText)
  621. return;
  622. ViewState ["EmptyDataText"] = value;
  623. RequireBinding ();
  624. }
  625. }
  626. [WebCategoryAttribute ("Behavior")]
  627. [DefaultValueAttribute (false)]
  628. public virtual bool EnableSortingAndPagingCallbacks {
  629. get {
  630. object ob = ViewState ["EnableSortingAndPagingCallbacks"];
  631. if (ob != null)
  632. return (bool) ob;
  633. return false;
  634. }
  635. set {
  636. if (value == EnableSortingAndPagingCallbacks)
  637. return;
  638. ViewState ["EnableSortingAndPagingCallbacks"] = value;
  639. RequireBinding ();
  640. }
  641. }
  642. [MonoTODO ("Make use of it in the code")]
  643. [DefaultValue (true)]
  644. public virtual bool EnableModelValidation {
  645. get;
  646. set;
  647. }
  648. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  649. [BrowsableAttribute (false)]
  650. public virtual GridViewRow FooterRow {
  651. get {
  652. if (table != null) {
  653. for (int index = table.Rows.Count - 1; index >= 0; index--) {
  654. GridViewRow row = (GridViewRow) table.Rows [index];
  655. switch (row.RowType) {
  656. case DataControlRowType.Separator:
  657. case DataControlRowType.Pager:
  658. continue;
  659. case DataControlRowType.Footer:
  660. return row;
  661. default:
  662. break;
  663. }
  664. }
  665. }
  666. return null;
  667. }
  668. }
  669. [WebCategoryAttribute ("Styles")]
  670. [PersistenceMode (PersistenceMode.InnerProperty)]
  671. [NotifyParentProperty (true)]
  672. [DefaultValue (null)]
  673. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  674. public TableItemStyle FooterStyle {
  675. get {
  676. if (footerStyle == null) {
  677. footerStyle = new TableItemStyle ();
  678. if (IsTrackingViewState)
  679. footerStyle.TrackViewState();
  680. }
  681. return footerStyle;
  682. }
  683. }
  684. [WebCategoryAttribute ("Appearance")]
  685. [DefaultValueAttribute (GridLines.Both)]
  686. public virtual GridLines GridLines {
  687. get {
  688. if (ControlStyleCreated)
  689. return ((TableStyle) ControlStyle).GridLines;
  690. return GridLines.Both;
  691. }
  692. set { ((TableStyle) ControlStyle).GridLines = value; }
  693. }
  694. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  695. [BrowsableAttribute (false)]
  696. public virtual GridViewRow HeaderRow {
  697. get {
  698. if (table != null) {
  699. for (int index = 0, total = table.Rows.Count; index < total; index++) {
  700. GridViewRow row = (GridViewRow) table.Rows [index];
  701. switch (row.RowType) {
  702. case DataControlRowType.Separator:
  703. case DataControlRowType.Pager:
  704. continue;
  705. case DataControlRowType.Header:
  706. return row;
  707. default:
  708. break;
  709. }
  710. }
  711. }
  712. return null;
  713. }
  714. }
  715. [WebCategoryAttribute ("Styles")]
  716. [PersistenceMode (PersistenceMode.InnerProperty)]
  717. [NotifyParentProperty (true)]
  718. [DefaultValue (null)]
  719. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  720. public TableItemStyle HeaderStyle {
  721. get {
  722. if (headerStyle == null) {
  723. headerStyle = new TableItemStyle ();
  724. if (IsTrackingViewState)
  725. headerStyle.TrackViewState();
  726. }
  727. return headerStyle;
  728. }
  729. }
  730. [Category ("Layout")]
  731. [DefaultValueAttribute (HorizontalAlign.NotSet)]
  732. public virtual HorizontalAlign HorizontalAlign {
  733. get {
  734. if (ControlStyleCreated)
  735. return ((TableStyle) ControlStyle).HorizontalAlign;
  736. return HorizontalAlign.NotSet;
  737. }
  738. set { ((TableStyle) ControlStyle).HorizontalAlign = value; }
  739. }
  740. [BrowsableAttribute (false)]
  741. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  742. public virtual int PageCount {
  743. get { return pageCount; }
  744. private set { pageCount = value; }
  745. }
  746. [WebCategoryAttribute ("Paging")]
  747. [BrowsableAttribute (true)]
  748. [DefaultValueAttribute (0)]
  749. public virtual int PageIndex {
  750. get { return pageIndex; }
  751. set {
  752. if (value == pageIndex)
  753. return;
  754. pageIndex = value;
  755. RequireBinding ();
  756. }
  757. }
  758. [DefaultValueAttribute (10)]
  759. [WebCategoryAttribute ("Paging")]
  760. public virtual int PageSize {
  761. get {
  762. object ob = ViewState ["PageSize"];
  763. if (ob != null)
  764. return (int) ob;
  765. return 10;
  766. }
  767. set {
  768. if (value == PageSize)
  769. return;
  770. ViewState ["PageSize"] = value;
  771. RequireBinding ();
  772. }
  773. }
  774. [WebCategoryAttribute ("Paging")]
  775. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Content)]
  776. [NotifyParentPropertyAttribute (true)]
  777. [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
  778. public virtual PagerSettings PagerSettings {
  779. get {
  780. if (pagerSettings == null) {
  781. pagerSettings = new PagerSettings (this);
  782. if (IsTrackingViewState)
  783. ((IStateManager)pagerSettings).TrackViewState ();
  784. }
  785. return pagerSettings;
  786. }
  787. }
  788. [WebCategoryAttribute ("Styles")]
  789. [PersistenceMode (PersistenceMode.InnerProperty)]
  790. [NotifyParentProperty (true)]
  791. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  792. public TableItemStyle PagerStyle {
  793. get {
  794. if (pagerStyle == null) {
  795. pagerStyle = new TableItemStyle ();
  796. if (IsTrackingViewState)
  797. pagerStyle.TrackViewState();
  798. }
  799. return pagerStyle;
  800. }
  801. }
  802. [DefaultValue (null)]
  803. [PersistenceMode (PersistenceMode.InnerProperty)]
  804. [Browsable (false)]
  805. [TemplateContainerAttribute(typeof(GridViewRow))]
  806. public virtual ITemplate PagerTemplate {
  807. get { return pagerTemplate; }
  808. set { pagerTemplate = value; }
  809. }
  810. [DefaultValueAttribute ("")]
  811. [WebCategoryAttribute ("Accessibility")]
  812. // [TypeConverterAttribute (typeof(System.Web.UI.Design.DataColumnSelectionConverter)]
  813. public virtual string RowHeaderColumn {
  814. get {
  815. object ob = ViewState ["RowHeaderColumn"];
  816. if (ob != null)
  817. return (string) ob;
  818. return String.Empty;
  819. }
  820. set {
  821. if (value == RowHeaderColumn)
  822. return;
  823. ViewState ["RowHeaderColumn"] = value;
  824. RequireBinding ();
  825. }
  826. }
  827. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  828. [BrowsableAttribute (false)]
  829. public virtual GridViewRowCollection Rows {
  830. get {
  831. EnsureChildControls ();
  832. if (rows == null)
  833. rows = new GridViewRowCollection (new ArrayList ());
  834. return rows;
  835. }
  836. }
  837. [WebCategoryAttribute ("Styles")]
  838. [PersistenceMode (PersistenceMode.InnerProperty)]
  839. [NotifyParentProperty (true)]
  840. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  841. public TableItemStyle RowStyle {
  842. get {
  843. if (rowStyle == null) {
  844. rowStyle = new TableItemStyle ();
  845. if (IsTrackingViewState)
  846. rowStyle.TrackViewState();
  847. }
  848. return rowStyle;
  849. }
  850. }
  851. [BrowsableAttribute (false)]
  852. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  853. public virtual DataKey SelectedDataKey {
  854. get {
  855. if (DataKeyNames.Length == 0)
  856. throw new InvalidOperationException (String.Format ("Data keys must be specified on GridView '{0}' before the selected data keys can be retrieved. Use the DataKeyNames property to specify data keys.", ID));
  857. if (selectedIndex >= 0 && selectedIndex < DataKeys.Count)
  858. return DataKeys [selectedIndex];
  859. else
  860. return null;
  861. }
  862. }
  863. [MonoTODO]
  864. [Browsable(false)]
  865. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  866. public virtual DataKey SelectedPersistedDataKey {
  867. get; set;
  868. }
  869. [MonoTODO]
  870. DataKey IPersistedSelector.DataKey {
  871. get { return SelectedPersistedDataKey; }
  872. set { SelectedPersistedDataKey = value; }
  873. }
  874. [BindableAttribute (true)]
  875. [DefaultValueAttribute (-1)]
  876. public virtual int SelectedIndex {
  877. get { return selectedIndex; }
  878. set {
  879. if (rows != null && selectedIndex >= 0 && selectedIndex < Rows.Count) {
  880. int oldIndex = selectedIndex;
  881. selectedIndex = -1;
  882. Rows [oldIndex].RowState = GetRowState (oldIndex);
  883. }
  884. selectedIndex = value;
  885. if (rows != null && selectedIndex >= 0 && selectedIndex < Rows.Count)
  886. Rows [selectedIndex].RowState = GetRowState (selectedIndex);
  887. }
  888. }
  889. [BrowsableAttribute (false)]
  890. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  891. public virtual GridViewRow SelectedRow {
  892. get {
  893. if (selectedIndex >= 0 && selectedIndex < Rows.Count)
  894. return Rows [selectedIndex];
  895. else
  896. return null;
  897. }
  898. }
  899. [WebCategoryAttribute ("Styles")]
  900. [PersistenceMode (PersistenceMode.InnerProperty)]
  901. [NotifyParentProperty (true)]
  902. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  903. public TableItemStyle SelectedRowStyle {
  904. get {
  905. if (selectedRowStyle == null) {
  906. selectedRowStyle = new TableItemStyle ();
  907. if (IsTrackingViewState)
  908. selectedRowStyle.TrackViewState();
  909. }
  910. return selectedRowStyle;
  911. }
  912. }
  913. [BrowsableAttribute (false)]
  914. public object SelectedValue {
  915. get {
  916. if (SelectedDataKey != null)
  917. return SelectedDataKey.Value;
  918. else
  919. return null;
  920. }
  921. }
  922. [WebCategoryAttribute ("Appearance")]
  923. [DefaultValueAttribute (false)]
  924. public virtual bool ShowFooter {
  925. get {
  926. object ob = ViewState ["ShowFooter"];
  927. if (ob != null)
  928. return (bool) ob;
  929. return false;
  930. }
  931. set {
  932. if (value == ShowFooter)
  933. return;
  934. ViewState ["ShowFooter"] = value;
  935. RequireBinding ();
  936. }
  937. }
  938. [WebCategoryAttribute ("Appearance")]
  939. [DefaultValueAttribute (true)]
  940. public virtual bool ShowHeader {
  941. get {
  942. object ob = ViewState ["ShowHeader"];
  943. if (ob != null)
  944. return (bool) ob;
  945. return true;
  946. }
  947. set {
  948. if (value == ShowHeader)
  949. return;
  950. ViewState ["ShowHeader"] = value;
  951. RequireBinding ();
  952. }
  953. }
  954. [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
  955. [BrowsableAttribute (false)]
  956. [DefaultValueAttribute (SortDirection.Ascending)]
  957. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  958. public virtual SortDirection SortDirection {
  959. get { return sortDirection; }
  960. private set {
  961. if (sortDirection == value)
  962. return;
  963. sortDirection = value;
  964. RequireBinding ();
  965. }
  966. }
  967. [BrowsableAttribute (false)]
  968. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  969. public virtual string SortExpression {
  970. get {
  971. if (sortExpression == null)
  972. return String.Empty;
  973. return sortExpression;
  974. }
  975. private set {
  976. if (sortExpression == value)
  977. return;
  978. sortExpression = value;
  979. RequireBinding ();
  980. }
  981. }
  982. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  983. [BrowsableAttribute (false)]
  984. public virtual GridViewRow TopPagerRow {
  985. get {
  986. EnsureDataBound ();
  987. return topPagerRow;
  988. }
  989. }
  990. [WebCategoryAttribute ("Accessibility")]
  991. [DefaultValueAttribute (true)]
  992. public virtual bool UseAccessibleHeader {
  993. get {
  994. object ob = ViewState ["UseAccessibleHeader"];
  995. if (ob != null)
  996. return (bool) ob;
  997. return true;
  998. }
  999. set {
  1000. if (value == UseAccessibleHeader)
  1001. return;
  1002. ViewState ["UseAccessibleHeader"] = value;
  1003. RequireBinding ();
  1004. }
  1005. }
  1006. #if NET_4_0
  1007. [TypeConverter (typeof(StringArrayConverter))]
  1008. [DefaultValue (null)]
  1009. public virtual string[] ClientIDRowSuffix {
  1010. get;
  1011. set;
  1012. }
  1013. [BrowsableAttribute(false)]
  1014. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  1015. public DataKeyArray ClientIDRowSuffixDataKeys {
  1016. get {
  1017. if (rowSuffixKeys == null) {
  1018. rowSuffixKeys = new DataKeyArray (DataKeySuffixList);
  1019. if (IsTrackingViewState)
  1020. ((IStateManager) rowSuffixKeys).TrackViewState ();
  1021. }
  1022. return rowSuffixKeys;
  1023. }
  1024. }
  1025. [DefaultValue (false)]
  1026. public virtual bool EnablePersistedSelection {
  1027. get {
  1028. throw new NotImplementedException ();
  1029. }
  1030. set {
  1031. throw new NotImplementedException ();
  1032. }
  1033. }
  1034. IAutoFieldGenerator IFieldControl.FieldsGenerator {
  1035. get {
  1036. throw new NotImplementedException ();
  1037. }
  1038. set {
  1039. throw new NotImplementedException ();
  1040. }
  1041. }
  1042. [DefaultValue (false)]
  1043. public virtual bool ShowHeaderWhenEmpty {
  1044. get { return ViewState.GetBool ("ShowHeaderWhenEmpty", false); }
  1045. set {
  1046. if (value == ShowHeaderWhenEmpty)
  1047. return;
  1048. ViewState ["ShowHeaderWhenEmpty"] = value;
  1049. RequireBinding ();
  1050. }
  1051. }
  1052. [PersistenceMode (PersistenceMode.InnerProperty)]
  1053. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  1054. [NotifyParentProperty (true)]
  1055. public TableItemStyle SortedAscendingCellStyle {
  1056. get {
  1057. if (sortedAscendingCellStyle == null) {
  1058. sortedAscendingCellStyle = new TableItemStyle ();
  1059. if (IsTrackingViewState)
  1060. ((IStateManager)sortedAscendingCellStyle).TrackViewState ();
  1061. }
  1062. return sortedAscendingCellStyle;
  1063. }
  1064. }
  1065. [PersistenceMode (PersistenceMode.InnerProperty)]
  1066. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  1067. [NotifyParentProperty (true)]
  1068. public TableItemStyle SortedAscendingHeaderStyle {
  1069. get {
  1070. if (sortedAscendingHeaderStyle == null) {
  1071. sortedAscendingHeaderStyle = new TableItemStyle ();
  1072. if (IsTrackingViewState)
  1073. ((IStateManager)sortedAscendingHeaderStyle).TrackViewState ();
  1074. }
  1075. return sortedAscendingHeaderStyle;
  1076. }
  1077. }
  1078. [PersistenceMode (PersistenceMode.InnerProperty)]
  1079. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  1080. [NotifyParentProperty (true)]
  1081. public TableItemStyle SortedDescendingCellStyle {
  1082. get {
  1083. if (sortedDescendingCellStyle == null) {
  1084. sortedDescendingCellStyle = new TableItemStyle ();
  1085. if (IsTrackingViewState)
  1086. ((IStateManager)sortedDescendingCellStyle).TrackViewState ();
  1087. }
  1088. return sortedDescendingCellStyle;
  1089. }
  1090. }
  1091. [PersistenceMode (PersistenceMode.InnerProperty)]
  1092. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  1093. [NotifyParentProperty (true)]
  1094. public TableItemStyle SortedDescendingHeaderStyle {
  1095. get {
  1096. if (sortedDescendingHeaderStyle == null) {
  1097. sortedDescendingHeaderStyle = new TableItemStyle ();
  1098. if (IsTrackingViewState)
  1099. ((IStateManager)sortedDescendingHeaderStyle).TrackViewState ();
  1100. }
  1101. return sortedDescendingHeaderStyle;
  1102. }
  1103. }
  1104. #endif
  1105. public virtual bool IsBindableType (Type type)
  1106. {
  1107. return type.IsPrimitive || type == typeof (string) || type == typeof (decimal) || type == typeof (DateTime) || type == typeof (Guid);
  1108. }
  1109. // MSDN: The CreateDataSourceSelectArguments method is a helper method called by
  1110. // the GridView control to create the DataSourceSelectArguments object that
  1111. // contains the arguments passed to the data source. In this implementation,
  1112. // the DataSourceSelectArguments object contains the arguments for paging operations.
  1113. protected override DataSourceSelectArguments CreateDataSourceSelectArguments ()
  1114. {
  1115. DataSourceSelectArguments arg = DataSourceSelectArguments.Empty;
  1116. DataSourceView view= GetData();
  1117. if (AllowPaging && view.CanPage) {
  1118. arg.StartRowIndex = PageIndex * PageSize;
  1119. if (view.CanRetrieveTotalRowCount) {
  1120. arg.RetrieveTotalRowCount = true;
  1121. arg.MaximumRows = PageSize;
  1122. } else
  1123. arg.MaximumRows = -1;
  1124. }
  1125. if (IsBoundUsingDataSourceID && !String.IsNullOrEmpty (sortExpression)) {
  1126. if (sortDirection == SortDirection.Ascending)
  1127. arg.SortExpression = sortExpression;
  1128. else
  1129. arg.SortExpression = sortExpression + " DESC";
  1130. }
  1131. return arg;
  1132. }
  1133. protected virtual ICollection CreateColumns (PagedDataSource dataSource, bool useDataSource)
  1134. {
  1135. bool autoGenerate = AutoGenerateColumns;
  1136. if (autoGenerate) {
  1137. IAutoFieldGenerator fieldGenerator = ColumnsGenerator;
  1138. if (fieldGenerator != null)
  1139. return fieldGenerator.GenerateFields (this);
  1140. }
  1141. ArrayList fields = new ArrayList ();
  1142. if (AutoGenerateEditButton || AutoGenerateDeleteButton || AutoGenerateSelectButton) {
  1143. CommandField field = new CommandField ();
  1144. field.ShowEditButton = AutoGenerateEditButton;
  1145. field.ShowDeleteButton = AutoGenerateDeleteButton;
  1146. field.ShowSelectButton = AutoGenerateSelectButton;
  1147. fields.Add (field);
  1148. }
  1149. fields.AddRange (Columns);
  1150. if (autoGenerate) {
  1151. if (useDataSource)
  1152. autoFieldProperties = CreateAutoFieldProperties (dataSource);
  1153. if (autoFieldProperties != null) {
  1154. foreach (AutoGeneratedFieldProperties props in autoFieldProperties)
  1155. fields.Add (CreateAutoGeneratedColumn (props));
  1156. }
  1157. }
  1158. return fields;
  1159. }
  1160. protected virtual AutoGeneratedField CreateAutoGeneratedColumn (AutoGeneratedFieldProperties fieldProperties)
  1161. {
  1162. return new AutoGeneratedField (fieldProperties);
  1163. }
  1164. AutoGeneratedFieldProperties[] CreateAutoFieldProperties (PagedDataSource source)
  1165. {
  1166. if(source == null) return null;
  1167. PropertyDescriptorCollection props = source.GetItemProperties (new PropertyDescriptor[0]);
  1168. Type prop_type;
  1169. ArrayList retVal = new ArrayList();
  1170. if (props == null) {
  1171. object fitem = null;
  1172. prop_type = null;
  1173. PropertyInfo prop_item = source.DataSource.GetType().GetProperty("Item",
  1174. BindingFlags.Instance | BindingFlags.Static |
  1175. BindingFlags.Public, null, null,
  1176. new Type[] { typeof(int) }, null);
  1177. if (prop_item != null)
  1178. prop_type = prop_item.PropertyType;
  1179. if (prop_type == null || prop_type == typeof(object)) {
  1180. IEnumerator en = source.GetEnumerator();
  1181. if (en != null && en.MoveNext ()) {
  1182. fitem = en.Current;
  1183. _dataEnumerator = en;
  1184. }
  1185. if (fitem != null)
  1186. prop_type = fitem.GetType();
  1187. }
  1188. if (fitem != null && fitem is ICustomTypeDescriptor)
  1189. props = TypeDescriptor.GetProperties(fitem);
  1190. else if (prop_type != null) {
  1191. if (IsBindableType (prop_type)) {
  1192. AutoGeneratedFieldProperties field = new AutoGeneratedFieldProperties ();
  1193. ((IStateManager)field).TrackViewState();
  1194. field.Name = "Item";
  1195. field.DataField = BoundField.ThisExpression;
  1196. field.Type = prop_type;
  1197. retVal.Add (field);
  1198. } else
  1199. props = TypeDescriptor.GetProperties (prop_type);
  1200. }
  1201. }
  1202. if (props != null && props.Count > 0) {
  1203. foreach (PropertyDescriptor current in props) {
  1204. if (IsBindableType (current.PropertyType)) {
  1205. AutoGeneratedFieldProperties field = new AutoGeneratedFieldProperties ();
  1206. ((IStateManager)field).TrackViewState();
  1207. field.Name = current.Name;
  1208. field.DataField = current.Name;
  1209. for (int i = 0; i < DataKeyNames.Length; i++) {
  1210. if (string.Compare (DataKeyNames [i], current.Name, StringComparison.InvariantCultureIgnoreCase) == 0) {
  1211. field.IsReadOnly = true;
  1212. break;
  1213. }
  1214. }
  1215. field.Type = current.PropertyType;
  1216. retVal.Add (field);
  1217. }
  1218. }
  1219. }
  1220. if (retVal.Count > 0)
  1221. return (AutoGeneratedFieldProperties[]) retVal.ToArray (typeof(AutoGeneratedFieldProperties));
  1222. else
  1223. return new AutoGeneratedFieldProperties [0];
  1224. }
  1225. protected virtual GridViewRow CreateRow (int rowIndex, int dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState)
  1226. {
  1227. GridViewRow row = new GridViewRow (rowIndex, dataSourceIndex, rowType, rowState);
  1228. return row;
  1229. }
  1230. void RequireBinding ()
  1231. {
  1232. if (Initialized)
  1233. RequiresDataBinding = true;
  1234. }
  1235. protected virtual Table CreateChildTable ()
  1236. {
  1237. return new ContainedTable (this);
  1238. }
  1239. void CreateHeaderRow (Table mainTable, DataControlField[] fields, bool dataBinding)
  1240. {
  1241. GridViewRow headerRow = CreateRow (-1, -1, DataControlRowType.Header, DataControlRowState.Normal);
  1242. InitializeRow (headerRow, fields);
  1243. OnRowCreated (new GridViewRowEventArgs (headerRow));
  1244. mainTable.Rows.Add (headerRow);
  1245. if (dataBinding) {
  1246. headerRow.DataBind ();
  1247. OnRowDataBound (new GridViewRowEventArgs (headerRow));
  1248. }
  1249. }
  1250. protected override int CreateChildControls (IEnumerable data, bool dataBinding)
  1251. {
  1252. // clear GridView
  1253. Controls.Clear ();
  1254. table = null;
  1255. rows = null;
  1256. if (data == null)
  1257. return 0;
  1258. PagedDataSource dataSource;
  1259. if (dataBinding) {
  1260. DataSourceView view = GetData ();
  1261. dataSource = new PagedDataSource ();
  1262. dataSource.DataSource = data;
  1263. if (AllowPaging) {
  1264. dataSource.AllowPaging = true;
  1265. dataSource.PageSize = PageSize;
  1266. if (view.CanPage) {
  1267. dataSource.AllowServerPaging = true;
  1268. if (SelectArguments.RetrieveTotalRowCount)
  1269. dataSource.VirtualCount = SelectArguments.TotalRowCount;
  1270. }
  1271. if (PageIndex >= dataSource.PageCount)
  1272. pageIndex = dataSource.PageCount - 1;
  1273. dataSource.CurrentPageIndex = PageIndex;
  1274. }
  1275. PageCount = dataSource.PageCount;
  1276. } else {
  1277. dataSource = new PagedDataSource ();
  1278. dataSource.DataSource = data;
  1279. if (AllowPaging) {
  1280. dataSource.AllowPaging = true;
  1281. dataSource.PageSize = PageSize;
  1282. dataSource.CurrentPageIndex = PageIndex;
  1283. }
  1284. }
  1285. bool createPager = AllowPaging && (PageCount >= 1) && PagerSettings.Visible;
  1286. ArrayList list = new ArrayList ();
  1287. // Creates the set of fields to show
  1288. _dataEnumerator = null;
  1289. ICollection fieldCollection = CreateColumns (dataSource, dataBinding);
  1290. int fieldCount = fieldCollection.Count;
  1291. DataControlField dcf;
  1292. DataControlField[] fields = new DataControlField [fieldCount];
  1293. fieldCollection.CopyTo (fields, 0);
  1294. for (int i = 0; i < fieldCount; i++) {
  1295. dcf = fields [i];
  1296. dcf.Initialize (AllowSorting, this);
  1297. if (EnableSortingAndPagingCallbacks)
  1298. dcf.ValidateSupportsCallback ();
  1299. }
  1300. bool skip_first = false;
  1301. IEnumerator enumerator;
  1302. if (_dataEnumerator != null) {
  1303. // replaced when creating bound columns
  1304. enumerator = _dataEnumerator;
  1305. skip_first = true;
  1306. } else
  1307. enumerator = dataSource.GetEnumerator ();
  1308. // Main table creation
  1309. Table mainTable = ContainedTable;
  1310. List <DataKey> dataKeyList;
  1311. string[] dataKeyNames;
  1312. #if NET_4_0
  1313. List <DataKey> dataKeySuffixList;
  1314. string[] clientIDRowSuffix;
  1315. #endif
  1316. if (dataBinding) {
  1317. dataKeyList = DataKeyList;
  1318. dataKeyNames = DataKeyNames;
  1319. #if NET_4_0
  1320. dataKeySuffixList = DataKeySuffixList;
  1321. clientIDRowSuffix = ClientIDRowSuffix;
  1322. #endif
  1323. } else {
  1324. dataKeyList = null;
  1325. dataKeyNames = null;
  1326. #if NET_4_0
  1327. dataKeySuffixList = null;
  1328. clientIDRowSuffix = null;
  1329. #endif
  1330. }
  1331. while (skip_first || enumerator.MoveNext ()) {
  1332. skip_first = false;
  1333. object obj = enumerator.Current;
  1334. if (list.Count == 0) {
  1335. if (createPager && (PagerSettings.Position == PagerPosition.Top || PagerSettings.Position == PagerPosition.TopAndBottom)) {
  1336. topPagerRow = CreatePagerRow (fieldCount, dataSource);
  1337. OnRowCreated (new GridViewRowEventArgs (topPagerRow));
  1338. mainTable.Rows.Add (topPagerRow);
  1339. if (dataBinding) {
  1340. topPagerRow.DataBind ();
  1341. OnRowDataBound (new GridViewRowEventArgs (topPagerRow));
  1342. }
  1343. if (PageCount == 1)
  1344. topPagerRow.Visible = false;
  1345. }
  1346. if (ShowHeader)
  1347. CreateHeaderRow (mainTable, fields, dataBinding);
  1348. }
  1349. DataControlRowState rstate = GetRowState (list.Count);
  1350. GridViewRow row = CreateRow (list.Count, list.Count, DataControlRowType.DataRow, rstate);
  1351. row.DataItem = obj;
  1352. list.Add (row);
  1353. InitializeRow (row, fields);
  1354. OnRowCreated (new GridViewRowEventArgs (row));
  1355. mainTable.Rows.Add (row);
  1356. if (dataBinding) {
  1357. row.DataBind ();
  1358. if (EditIndex == row.RowIndex)
  1359. oldEditValues = new DataKey (GetRowValues (row, true, true));
  1360. dataKeyList.Add (new DataKey (CreateRowDataKey (row), dataKeyNames));
  1361. #if NET_4_0
  1362. dataKeySuffixList.Add (new DataKey (CreateRowSuffixDataKey (row), clientIDRowSuffix));
  1363. #endif
  1364. OnRowDataBound (new GridViewRowEventArgs (row));
  1365. }
  1366. }
  1367. int retValue;
  1368. if (list.Count == 0) {
  1369. #if NET_4_0
  1370. if (ShowHeader && ShowHeaderWhenEmpty)
  1371. CreateHeaderRow (mainTable, fields, dataBinding);
  1372. #endif
  1373. GridViewRow emptyRow = CreateEmptyrRow (fieldCount);
  1374. if (emptyRow != null) {
  1375. OnRowCreated (new GridViewRowEventArgs (emptyRow));
  1376. mainTable.Rows.Add (emptyRow);
  1377. if (dataBinding) {
  1378. emptyRow.DataBind ();
  1379. OnRowDataBound (new GridViewRowEventArgs (emptyRow));
  1380. }
  1381. }
  1382. #if NET_4_0
  1383. if (mainTable.Rows.Count == 0)
  1384. table = null;
  1385. #endif
  1386. return 0;
  1387. } else {
  1388. GridViewRow footerRow = CreateRow (-1, -1, DataControlRowType.Footer, DataControlRowState.Normal);
  1389. InitializeRow (footerRow, fields);
  1390. OnRowCreated (new GridViewRowEventArgs (footerRow));
  1391. mainTable.Rows.Add (footerRow);
  1392. if (dataBinding) {
  1393. footerRow.DataBind ();
  1394. OnRowDataBound (new GridViewRowEventArgs (footerRow));
  1395. }
  1396. if (createPager && (PagerSettings.Position == PagerPosition.Bottom || PagerSettings.Position == PagerPosition.TopAndBottom)) {
  1397. bottomPagerRow = CreatePagerRow (fieldCount, dataSource);
  1398. OnRowCreated (new GridViewRowEventArgs (bottomPagerRow));
  1399. mainTable.Rows.Add (bottomPagerRow);
  1400. if (dataBinding) {
  1401. bottomPagerRow.DataBind ();
  1402. OnRowDataBound (new GridViewRowEventArgs (bottomPagerRow));
  1403. }
  1404. if (PageCount == 1)
  1405. bottomPagerRow.Visible = false;
  1406. }
  1407. }
  1408. rows = new GridViewRowCollection (list);
  1409. if (!dataBinding)
  1410. return -1;
  1411. if (AllowPaging)
  1412. return dataSource.DataSourceCount;
  1413. else
  1414. return list.Count;
  1415. }
  1416. Table ContainedTable {
  1417. get {
  1418. if (table == null) {
  1419. table = CreateChildTable ();
  1420. Controls.Add (table);
  1421. }
  1422. return table;
  1423. }
  1424. }
  1425. protected override Style CreateControlStyle ()
  1426. {
  1427. TableStyle style = new TableStyle (ViewState);
  1428. style.GridLines = GridLines.Both;
  1429. style.CellSpacing = 0;
  1430. return style;
  1431. }
  1432. DataControlRowState GetRowState (int index)
  1433. {
  1434. DataControlRowState rstate = (index % 2) == 0 ? DataControlRowState.Normal : DataControlRowState.Alternate;
  1435. if (index == SelectedIndex)
  1436. rstate |= DataControlRowState.Selected;
  1437. if (index == EditIndex)
  1438. rstate |= DataControlRowState.Edit;
  1439. return rstate;
  1440. }
  1441. GridViewRow CreatePagerRow (int fieldCount, PagedDataSource dataSource)
  1442. {
  1443. GridViewRow row = CreateRow (-1, -1, DataControlRowType.Pager, DataControlRowState.Normal);
  1444. InitializePager (row, fieldCount, dataSource);
  1445. return row;
  1446. }
  1447. protected virtual void InitializePager (GridViewRow row, int columnSpan, PagedDataSource dataSource)
  1448. {
  1449. TableCell cell = new TableCell ();
  1450. if (columnSpan > 1)
  1451. cell.ColumnSpan = columnSpan;
  1452. if (pagerTemplate != null)
  1453. pagerTemplate.InstantiateIn (cell);
  1454. else
  1455. cell.Controls.Add (PagerSettings.CreatePagerControl (dataSource.CurrentPageIndex, dataSource.PageCount));
  1456. row.Cells.Add (cell);
  1457. }
  1458. GridViewRow CreateEmptyrRow (int fieldCount)
  1459. {
  1460. if (emptyDataTemplate == null && String.IsNullOrEmpty (EmptyDataText))
  1461. return null;
  1462. GridViewRow row = CreateRow (-1, -1, DataControlRowType.EmptyDataRow, DataControlRowState.Normal);
  1463. TableCell cell = new TableCell ();
  1464. cell.ColumnSpan = fieldCount;
  1465. if (emptyDataTemplate != null)
  1466. emptyDataTemplate.InstantiateIn (cell);
  1467. else
  1468. cell.Text = EmptyDataText;
  1469. row.Cells.Add (cell);
  1470. return row;
  1471. }
  1472. protected virtual void InitializeRow (GridViewRow row, DataControlField[] fields)
  1473. {
  1474. DataControlCellType ctype;
  1475. bool accessibleHeader = false;
  1476. switch (row.RowType) {
  1477. case DataControlRowType.Header:
  1478. ctype = DataControlCellType.Header;
  1479. accessibleHeader = UseAccessibleHeader;
  1480. break;
  1481. case DataControlRowType.Footer:
  1482. ctype = DataControlCellType.Footer;
  1483. break;
  1484. default:
  1485. ctype = DataControlCellType.DataCell;
  1486. break;
  1487. }
  1488. for (int n=0; n<fields.Length; n++) {
  1489. DataControlField field = fields [n];
  1490. DataControlFieldCell cell;
  1491. if (((field is BoundField) && ((BoundField)field).DataField == RowHeaderColumn) || accessibleHeader)
  1492. cell = new DataControlFieldHeaderCell (field, accessibleHeader ? TableHeaderScope.Column : TableHeaderScope.Row);
  1493. else
  1494. cell = new DataControlFieldCell (field);
  1495. row.Cells.Add (cell);
  1496. field.InitializeCell (cell, ctype, row.RowState, row.RowIndex);
  1497. }
  1498. }
  1499. void LoadAndCacheProperties (string[] names, object dataItem, ref PropertyDescriptor[] cache)
  1500. {
  1501. if (cache != null)
  1502. return;
  1503. PropertyDescriptorCollection props = TypeDescriptor.GetProperties (dataItem);
  1504. int len = names != null ? names.Length : 0;
  1505. cache = new PropertyDescriptor [len];
  1506. for (int n = 0; n < len; n++) {
  1507. string propName = names [n];
  1508. PropertyDescriptor p = props.Find (propName, true);
  1509. if (p == null)
  1510. throw new InvalidOperationException ("Property '" + propName + "' not found in object of type " + dataItem.GetType ());
  1511. cache [n] = p;
  1512. }
  1513. }
  1514. IOrderedDictionary CreateDictionaryFromProperties (PropertyDescriptor[] cache, object dataItem)
  1515. {
  1516. OrderedDictionary dic = new OrderedDictionary ();
  1517. foreach (PropertyDescriptor p in cache)
  1518. dic [p.Name] = p.GetValue (dataItem);
  1519. return dic;
  1520. }
  1521. IOrderedDictionary CreateRowDataKey (GridViewRow row)
  1522. {
  1523. object dataItem = row.DataItem;
  1524. LoadAndCacheProperties (DataKeyNames, dataItem, ref cachedKeyProperties);
  1525. return CreateDictionaryFromProperties (cachedKeyProperties, dataItem);
  1526. }
  1527. #if NET_4_0
  1528. IOrderedDictionary CreateRowSuffixDataKey (GridViewRow row)
  1529. {
  1530. object dataItem = row.DataItem;
  1531. LoadAndCacheProperties (ClientIDRowSuffix, dataItem, ref cachedSuffixKeyProperties);
  1532. return CreateDictionaryFromProperties (cachedSuffixKeyProperties, dataItem);
  1533. }
  1534. #endif
  1535. IOrderedDictionary GetRowValues (GridViewRow row, bool includeReadOnlyFields, bool includePrimaryKey)
  1536. {
  1537. OrderedDictionary dic = new OrderedDictionary ();
  1538. ExtractRowValues (dic, row, includeReadOnlyFields, includePrimaryKey);
  1539. return dic;
  1540. }
  1541. protected virtual void ExtractRowValues (IOrderedDictionary fieldValues, GridViewRow row, bool includeReadOnlyFields, bool includePrimaryKey)
  1542. {
  1543. DataControlField field;
  1544. foreach (TableCell cell in row.Cells) {
  1545. DataControlFieldCell c = cell as DataControlFieldCell;
  1546. if (c == null)
  1547. continue;
  1548. field = c.ContainingField;
  1549. if (field != null && !field.Visible)
  1550. continue;
  1551. c.ContainingField.ExtractValuesFromCell (fieldValues, c, row.RowState, includeReadOnlyFields);
  1552. }
  1553. if (!includePrimaryKey && DataKeyNames != null)
  1554. foreach (string key in DataKeyNames)
  1555. fieldValues.Remove (key);
  1556. }
  1557. protected override HtmlTextWriterTag TagKey {
  1558. get {
  1559. if (EnableSortingAndPagingCallbacks)
  1560. return HtmlTextWriterTag.Div;
  1561. else
  1562. return HtmlTextWriterTag.Table;
  1563. }
  1564. }
  1565. public sealed override void DataBind ()
  1566. {
  1567. DataKeyList.Clear ();
  1568. cachedKeyProperties = null;
  1569. base.DataBind ();
  1570. keys = new DataKeyArray (DataKeyList);
  1571. GridViewRow row = HeaderRow;
  1572. if (row != null)
  1573. row.Visible = ShowHeader;
  1574. row = FooterRow;
  1575. if (row != null)
  1576. row.Visible = ShowFooter;
  1577. }
  1578. protected internal override void PerformDataBinding (IEnumerable data)
  1579. {
  1580. base.PerformDataBinding (data);
  1581. }
  1582. protected internal virtual void PrepareControlHierarchy ()
  1583. {
  1584. if (table == null)
  1585. return;
  1586. table.Caption = Caption;
  1587. table.CaptionAlign = CaptionAlign;
  1588. table.CopyBaseAttributes (this);
  1589. foreach (GridViewRow row in table.Rows) {
  1590. switch (row.RowType) {
  1591. case DataControlRowType.Header:
  1592. if (headerStyle != null && !headerStyle.IsEmpty)
  1593. row.ControlStyle.MergeWith(headerStyle);
  1594. row.Visible = ShowHeader;
  1595. break;
  1596. case DataControlRowType.Footer:
  1597. if (footerStyle != null && !footerStyle.IsEmpty)
  1598. row.ControlStyle.MergeWith (footerStyle);
  1599. row.Visible = ShowFooter;
  1600. break;
  1601. case DataControlRowType.Pager:
  1602. if (pagerStyle != null && !pagerStyle.IsEmpty)
  1603. row.ControlStyle.MergeWith (pagerStyle);
  1604. break;
  1605. case DataControlRowType.EmptyDataRow:
  1606. if (emptyDataRowStyle != null && !emptyDataRowStyle.IsEmpty)
  1607. row.ControlStyle.MergeWith (emptyDataRowStyle);
  1608. break;
  1609. case DataControlRowType.DataRow:
  1610. if ((row.RowState & DataControlRowState.Edit) != 0 && editRowStyle != null && !editRowStyle.IsEmpty)
  1611. row.ControlStyle.MergeWith (editRowStyle);
  1612. if ((row.RowState & DataControlRowState.Selected) != 0 && selectedRowStyle != null && !selectedRowStyle.IsEmpty)
  1613. row.ControlStyle.MergeWith (selectedRowStyle);
  1614. if ((row.RowState & DataControlRowState.Alternate) != 0 && alternatingRowStyle != null && !alternatingRowStyle.IsEmpty)
  1615. row.ControlStyle.MergeWith (alternatingRowStyle);
  1616. if (rowStyle != null && !rowStyle.IsEmpty)
  1617. row.ControlStyle.MergeWith (rowStyle);
  1618. break;
  1619. default:
  1620. break;
  1621. }
  1622. #if NET_4_0
  1623. string sortExpression = SortExpression;
  1624. bool haveSorting = !String.IsNullOrEmpty (sortExpression);
  1625. #endif
  1626. foreach (TableCell cell in row.Cells) {
  1627. DataControlFieldCell fcell = cell as DataControlFieldCell;
  1628. if (fcell != null) {
  1629. DataControlField field = fcell.ContainingField;
  1630. if (field == null)
  1631. continue;
  1632. if (!field.Visible) {
  1633. cell.Visible = false;
  1634. continue;
  1635. }
  1636. switch (row.RowType) {
  1637. case DataControlRowType.Header:
  1638. if (field.HeaderStyleCreated && !field.HeaderStyle.IsEmpty)
  1639. cell.ControlStyle.MergeWith (field.HeaderStyle);
  1640. #if NET_4_0
  1641. if (haveSorting)
  1642. MergeWithSortingStyle (sortExpression, sortedAscendingHeaderStyle, sortedDescendingHeaderStyle, field, cell);
  1643. #endif
  1644. break;
  1645. case DataControlRowType.Footer:
  1646. if (field.FooterStyleCreated && !field.FooterStyle.IsEmpty)
  1647. cell.ControlStyle.MergeWith (field.FooterStyle);
  1648. break;
  1649. default:
  1650. if (field.ControlStyleCreated && !field.ControlStyle.IsEmpty) {
  1651. foreach (Control c in cell.Controls) {
  1652. WebControl wc = c as WebControl;
  1653. if (wc != null)
  1654. wc.ControlStyle.MergeWith (field.ControlStyle);
  1655. }
  1656. }
  1657. if (field.ItemStyleCreated && !field.ItemStyle.IsEmpty)
  1658. cell.ControlStyle.MergeWith (field.ItemStyle);
  1659. #if NET_4_0
  1660. if (haveSorting)
  1661. MergeWithSortingStyle (sortExpression, sortedAscendingCellStyle, sortedDescendingCellStyle, field, cell);
  1662. #endif
  1663. break;
  1664. }
  1665. }
  1666. }
  1667. }
  1668. }
  1669. #if NET_4_0
  1670. void MergeWithSortingStyle (string sortExpression, TableItemStyle ascending, TableItemStyle descending, DataControlField field, TableCell cell)
  1671. {
  1672. if (String.Compare (field.SortExpression, sortExpression, StringComparison.OrdinalIgnoreCase) != 0)
  1673. return;
  1674. cell.ControlStyle.MergeWith (SortDirection == SortDirection.Ascending ? ascending : descending);
  1675. }
  1676. #endif
  1677. protected internal override void OnInit (EventArgs e)
  1678. {
  1679. Page page = Page;
  1680. if (page != null)
  1681. page.RegisterRequiresControlState (this);
  1682. base.OnInit (e);
  1683. }
  1684. void OnFieldsChanged (object sender, EventArgs args)
  1685. {
  1686. RequireBinding ();
  1687. }
  1688. protected override void OnDataPropertyChanged ()
  1689. {
  1690. base.OnDataPropertyChanged ();
  1691. RequireBinding ();
  1692. }
  1693. protected override void OnDataSourceViewChanged (object sender, EventArgs e)
  1694. {
  1695. base.OnDataSourceViewChanged (sender, e);
  1696. RequireBinding ();
  1697. }
  1698. protected override bool OnBubbleEvent (object source, EventArgs e)
  1699. {
  1700. GridViewCommandEventArgs args = e as GridViewCommandEventArgs;
  1701. if (args != null) {
  1702. bool causesValidation = false;
  1703. IButtonControl button = args.CommandSource as IButtonControl;
  1704. if (button != null && button.CausesValidation) {
  1705. Page.Validate (button.ValidationGroup);
  1706. causesValidation = true;
  1707. }
  1708. OnRowCommand (args);
  1709. string param = args.CommandArgument as string;
  1710. if (param == null || param.Length == 0) {
  1711. GridViewRow row = args.Row;
  1712. if (row != null)
  1713. param = row.RowIndex.ToString();
  1714. }
  1715. ProcessEvent (args.CommandName, param, causesValidation);
  1716. return true;
  1717. }
  1718. return base.OnBubbleEvent (source, e);
  1719. }
  1720. void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
  1721. {
  1722. ValidateEvent (UniqueID, eventArgument);
  1723. RaisePostBackEvent (eventArgument);
  1724. }
  1725. // This is prolly obsolete
  1726. protected virtual void RaisePostBackEvent (string eventArgument)
  1727. {
  1728. GridViewCommandEventArgs args;
  1729. int i = eventArgument.IndexOf ('$');
  1730. if (i != -1)
  1731. args = new GridViewCommandEventArgs (this, new CommandEventArgs (eventArgument.Substring (0, i), eventArgument.Substring (i + 1)));
  1732. else
  1733. args = new GridViewCommandEventArgs (this, new CommandEventArgs (eventArgument, null));
  1734. OnRowCommand (args);
  1735. ProcessEvent (args.CommandName, (string) args.CommandArgument, false);
  1736. }
  1737. void ProcessEvent (string eventName, string param, bool causesValidation)
  1738. {
  1739. switch (eventName) {
  1740. case DataControlCommands.PageCommandName:
  1741. int newIndex = -1;
  1742. switch (param) {
  1743. case DataControlCommands.FirstPageCommandArgument:
  1744. newIndex = 0;
  1745. break;
  1746. case DataControlCommands.LastPageCommandArgument:
  1747. newIndex = PageCount - 1;
  1748. break;
  1749. case DataControlCommands.NextPageCommandArgument:
  1750. newIndex = PageIndex + 1;
  1751. break;
  1752. case DataControlCommands.PreviousPageCommandArgument:
  1753. newIndex = PageIndex - 1;
  1754. break;
  1755. default:
  1756. int paramIndex = 0;
  1757. int.TryParse (param, out paramIndex);
  1758. newIndex = paramIndex - 1;
  1759. break;
  1760. }
  1761. SetPageIndex (newIndex);
  1762. break;
  1763. case DataControlCommands.FirstPageCommandArgument:
  1764. SetPageIndex (0);
  1765. break;
  1766. case DataControlCommands.LastPageCommandArgument:
  1767. SetPageIndex (PageCount - 1);
  1768. break;
  1769. case DataControlCommands.NextPageCommandArgument:
  1770. if (PageIndex < PageCount - 1)
  1771. SetPageIndex (PageIndex + 1);
  1772. break;
  1773. case DataControlCommands.PreviousPageCommandArgument:
  1774. if (PageIndex > 0)
  1775. SetPageIndex (PageIndex - 1);
  1776. break;
  1777. case DataControlCommands.SelectCommandName:
  1778. SelectRow (int.Parse (param));
  1779. break;
  1780. case DataControlCommands.EditCommandName:
  1781. SetEditRow (int.Parse (param));
  1782. break;
  1783. case DataControlCommands.UpdateCommandName:
  1784. int editIndex = int.Parse (param);
  1785. UpdateRow (Rows [editIndex], editIndex, causesValidation);
  1786. break;
  1787. case DataControlCommands.CancelCommandName:
  1788. CancelEdit ();
  1789. break;
  1790. case DataControlCommands.DeleteCommandName:
  1791. DeleteRow (int.Parse (param));
  1792. break;
  1793. case DataControlCommands.SortCommandName:
  1794. Sort (param);
  1795. break;
  1796. }
  1797. }
  1798. void Sort (string newSortExpression)
  1799. {
  1800. SortDirection newDirection = SortDirection.Ascending;
  1801. if (sortExpression == newSortExpression && sortDirection == SortDirection.Ascending)
  1802. newDirection = SortDirection.Descending;
  1803. Sort (newSortExpression, newDirection);
  1804. }
  1805. public virtual void Sort (string newSortExpression, SortDirection newSortDirection)
  1806. {
  1807. GridViewSortEventArgs args = new GridViewSortEventArgs (newSortExpression, newSortDirection);
  1808. OnSorting (args);
  1809. if (args.Cancel)
  1810. return;
  1811. if (IsBoundUsingDataSourceID) {
  1812. EditIndex = -1;
  1813. PageIndex = 0;
  1814. SortExpression = args.SortExpression;
  1815. SortDirection = args.SortDirection;
  1816. }
  1817. OnSorted (EventArgs.Empty);
  1818. }
  1819. #if NET_4_0
  1820. public
  1821. #endif
  1822. void SelectRow (int index)
  1823. {
  1824. GridViewSelectEventArgs args = new GridViewSelectEventArgs (index);
  1825. OnSelectedIndexChanging (args);
  1826. if (!args.Cancel) {
  1827. RequireBinding ();
  1828. SelectedIndex = args.NewSelectedIndex;
  1829. OnSelectedIndexChanged (EventArgs.Empty);
  1830. }
  1831. }
  1832. #if NET_4_0
  1833. public
  1834. #endif
  1835. void SetPageIndex (int newIndex)
  1836. {
  1837. GridViewPageEventArgs args = new GridViewPageEventArgs (newIndex);
  1838. OnPageIndexChanging (args);
  1839. if (args.Cancel || !IsBoundUsingDataSourceID)
  1840. return;
  1841. EndRowEdit ();
  1842. PageIndex = args.NewPageIndex;
  1843. OnPageIndexChanged (EventArgs.Empty);
  1844. }
  1845. #if NET_4_0
  1846. public
  1847. #endif
  1848. void SetEditRow (int index)
  1849. {
  1850. GridViewEditEventArgs args = new GridViewEditEventArgs (index);
  1851. OnRowEditing (args);
  1852. if (args.Cancel || !IsBoundUsingDataSourceID)
  1853. return;
  1854. EditIndex = args.NewEditIndex;
  1855. }
  1856. void CancelEdit ()
  1857. {
  1858. GridViewCancelEditEventArgs args = new GridViewCancelEditEventArgs (EditIndex);
  1859. OnRowCancelingEdit (args);
  1860. if (args.Cancel || !IsBoundUsingDataSourceID)
  1861. return;
  1862. EndRowEdit ();
  1863. }
  1864. [MonoTODO ("Support two-way binding expressions")]
  1865. public virtual void UpdateRow (int rowIndex, bool causesValidation)
  1866. {
  1867. if (rowIndex != EditIndex)
  1868. throw new NotSupportedException ();
  1869. GridViewRow row = Rows [rowIndex];
  1870. UpdateRow (row, rowIndex, causesValidation);
  1871. }
  1872. void UpdateRow (GridViewRow row, int rowIndex, bool causesValidation)
  1873. {
  1874. if (causesValidation && Page != null && !Page.IsValid)
  1875. return;
  1876. currentEditOldValues = CopyOrderedDictionary (OldEditValues.Values);
  1877. currentEditRowKeys = CopyOrderedDictionary (DataKeys [rowIndex].Values);
  1878. currentEditNewValues = GetRowValues (row, false, false);
  1879. GridViewUpdateEventArgs args = new GridViewUpdateEventArgs (rowIndex, currentEditRowKeys, currentEditOldValues, currentEditNewValues);
  1880. OnRowUpdating (args);
  1881. if (args.Cancel || !IsBoundUsingDataSourceID)
  1882. return;
  1883. DataSourceView view = GetData ();
  1884. if (view == null)
  1885. throw new HttpException ("The DataSourceView associated to data bound control was null");
  1886. view.Update (currentEditRowKeys, currentEditNewValues, currentEditOldValues, new DataSourceViewOperationCallback (UpdateCallback));
  1887. }
  1888. static IOrderedDictionary CopyOrderedDictionary (IOrderedDictionary sourceDic)
  1889. {
  1890. OrderedDictionary copyDic = new OrderedDictionary ();
  1891. foreach (object key in sourceDic.Keys)
  1892. copyDic.Add (key, sourceDic [key]);
  1893. return copyDic;
  1894. }
  1895. bool UpdateCallback (int recordsAffected, Exception exception)
  1896. {
  1897. GridViewUpdatedEventArgs dargs = new GridViewUpdatedEventArgs (recordsAffected, exception, currentEditRowKeys, currentEditOldValues, currentEditNewValues);
  1898. OnRowUpdated (dargs);
  1899. if (!dargs.KeepInEditMode)
  1900. EndRowEdit ();
  1901. return dargs.ExceptionHandled;
  1902. }
  1903. public virtual void DeleteRow (int rowIndex)
  1904. {
  1905. GridViewRow row = Rows [rowIndex];
  1906. currentEditRowKeys = CopyOrderedDictionary (DataKeys [rowIndex].Values);
  1907. currentEditNewValues = GetRowValues (row, true, true);
  1908. GridViewDeleteEventArgs args = new GridViewDeleteEventArgs (rowIndex, currentEditRowKeys, currentEditNewValues);
  1909. OnRowDeleting (args);
  1910. if (args.Cancel || !IsBoundUsingDataSourceID)
  1911. return;
  1912. RequireBinding ();
  1913. DataSourceView view = GetData ();
  1914. if (view != null)
  1915. view.Delete (currentEditRowKeys, currentEditNewValues, new DataSourceViewOperationCallback (DeleteCallback));
  1916. else {
  1917. GridViewDeletedEventArgs dargs = new GridViewDeletedEventArgs (0, null, currentEditRowKeys, currentEditNewValues);
  1918. OnRowDeleted (dargs);
  1919. }
  1920. }
  1921. bool DeleteCallback (int recordsAffected, Exception exception)
  1922. {
  1923. GridViewDeletedEventArgs dargs = new GridViewDeletedEventArgs (recordsAffected, exception, currentEditRowKeys, currentEditNewValues);
  1924. OnRowDeleted (dargs);
  1925. return dargs.ExceptionHandled;
  1926. }
  1927. void EndRowEdit ()
  1928. {
  1929. EditIndex = -1;
  1930. oldEditValues = new DataKey (new OrderedDictionary ());
  1931. currentEditRowKeys = null;
  1932. currentEditOldValues = null;
  1933. currentEditNewValues = null;
  1934. }
  1935. protected internal override void LoadControlState (object ob)
  1936. {
  1937. if (ob == null)
  1938. return;
  1939. object[] state = (object[]) ob;
  1940. base.LoadControlState (state[0]);
  1941. pageIndex = (int) state[1];
  1942. selectedIndex = (int) state[2];
  1943. editIndex = (int) state[3];
  1944. sortExpression = (string) state[4];
  1945. sortDirection = (SortDirection) state[5];
  1946. DataKeyNames = (string []) state [6];
  1947. if (state [7] != null)
  1948. LoadDataKeyArrayState ((object []) state [7], out keys);
  1949. if (state [8] != null)
  1950. ((IStateManager) OldEditValues).LoadViewState (state [8]);
  1951. pageCount = (int)state [9];
  1952. #if NET_4_0
  1953. if (state [10] != null)
  1954. ClientIDRowSuffix = (string[]) state [10];
  1955. if (state [11] != null)
  1956. LoadDataKeyArrayState ((object []) state [11], out rowSuffixKeys);
  1957. #endif
  1958. }
  1959. protected internal override object SaveControlState ()
  1960. {
  1961. if (EnableSortingAndPagingCallbacks) {
  1962. Page page = Page;
  1963. ClientScriptManager scriptManager = page != null ? page.ClientScript : null;
  1964. if (scriptManager != null) {
  1965. scriptManager.RegisterHiddenField (ClientID + "_Page", PageIndex.ToString ());
  1966. scriptManager.RegisterHiddenField (ClientID + "_SortExpression", SortExpression);
  1967. scriptManager.RegisterHiddenField (ClientID + "_SortDirection", ((int)SortDirection).ToString());
  1968. }
  1969. }
  1970. object bstate = base.SaveControlState ();
  1971. return new object [] {
  1972. bstate,
  1973. pageIndex,
  1974. selectedIndex,
  1975. editIndex,
  1976. sortExpression,
  1977. sortDirection,
  1978. DataKeyNames,
  1979. SaveDataKeyArrayState (keys),
  1980. (oldEditValues == null ? null : ((IStateManager)oldEditValues).SaveViewState ()),
  1981. pageCount,
  1982. #if NET_4_0
  1983. ClientIDRowSuffix,
  1984. SaveDataKeyArrayState (rowSuffixKeys)
  1985. #endif
  1986. };
  1987. }
  1988. object [] SaveDataKeyArrayState (DataKeyArray keys)
  1989. {
  1990. if (keys == null)
  1991. return null;
  1992. object [] state = new object [keys.Count];
  1993. for (int i = 0; i < keys.Count; i++)
  1994. state [i] = ((IStateManager) keys [i]).SaveViewState ();
  1995. return state;
  1996. }
  1997. void LoadDataKeyArrayState (object [] state, out DataKeyArray keys)
  1998. {
  1999. List <DataKey> dataKeyList = DataKeyList;
  2000. string[] dataKeyNames = DataKeyNames;
  2001. int dataKeyNamesLen = dataKeyNames.Length;
  2002. for (int i = 0; i < state.Length; i++) {
  2003. DataKey dataKey = new DataKey (new OrderedDictionary (dataKeyNamesLen), dataKeyNames);
  2004. ((IStateManager) dataKey).LoadViewState (state [i]);
  2005. dataKeyList.Add (dataKey);
  2006. }
  2007. keys = new DataKeyArray (dataKeyList);
  2008. }
  2009. protected override void TrackViewState ()
  2010. {
  2011. base.TrackViewState();
  2012. if (columns != null)
  2013. ((IStateManager)columns).TrackViewState();
  2014. if (pagerSettings != null)
  2015. ((IStateManager)pagerSettings).TrackViewState();
  2016. if (alternatingRowStyle != null)
  2017. ((IStateManager)alternatingRowStyle).TrackViewState();
  2018. if (footerStyle != null)
  2019. ((IStateManager)footerStyle).TrackViewState();
  2020. if (headerStyle != null)
  2021. ((IStateManager)headerStyle).TrackViewState();
  2022. if (pagerStyle != null)
  2023. ((IStateManager)pagerStyle).TrackViewState();
  2024. if (rowStyle != null)
  2025. ((IStateManager)rowStyle).TrackViewState();
  2026. if (selectedRowStyle != null)
  2027. ((IStateManager)selectedRowStyle).TrackViewState();
  2028. if (editRowStyle != null)
  2029. ((IStateManager)editRowStyle).TrackViewState();
  2030. if (emptyDataRowStyle != null)
  2031. ((IStateManager)emptyDataRowStyle).TrackViewState();
  2032. #if NET_4_0
  2033. if (sortedAscendingCellStyle != null)
  2034. ((IStateManager)sortedAscendingCellStyle).TrackViewState ();
  2035. if (sortedAscendingHeaderStyle != null)
  2036. ((IStateManager)sortedAscendingHeaderStyle).TrackViewState ();
  2037. if (sortedDescendingCellStyle != null)
  2038. ((IStateManager)sortedDescendingCellStyle).TrackViewState ();
  2039. if (sortedDescendingHeaderStyle != null)
  2040. ((IStateManager)sortedDescendingHeaderStyle).TrackViewState ();
  2041. if (rowSuffixKeys != null)
  2042. ((IStateManager) rowSuffixKeys).TrackViewState ();
  2043. #endif
  2044. if (keys != null)
  2045. ((IStateManager)keys).TrackViewState();
  2046. if (autoFieldProperties != null) {
  2047. foreach (IStateManager sm in autoFieldProperties)
  2048. sm.TrackViewState ();
  2049. }
  2050. }
  2051. protected override object SaveViewState ()
  2052. {
  2053. object[] autoFieldsData = null;
  2054. if (autoFieldProperties != null) {
  2055. object[] data = new object [autoFieldProperties.Length];
  2056. bool allNull = true;
  2057. for (int n = 0; n < data.Length; n++) {
  2058. data [n] = ((IStateManager)autoFieldProperties [n]).SaveViewState ();
  2059. if (data [n] != null)
  2060. allNull = false;
  2061. }
  2062. if (!allNull)
  2063. autoFieldsData = data;
  2064. }
  2065. object[] states = {
  2066. base.SaveViewState(), // 0
  2067. (columns == null ? null : ((IStateManager)columns).SaveViewState()), // 1
  2068. (pagerSettings == null ? null : ((IStateManager)pagerSettings).SaveViewState()), // 2
  2069. (alternatingRowStyle == null ? null : ((IStateManager)alternatingRowStyle).SaveViewState()), // 3
  2070. (footerStyle == null ? null : ((IStateManager)footerStyle).SaveViewState()), // 4
  2071. (headerStyle == null ? null : ((IStateManager)headerStyle).SaveViewState()), // 5
  2072. (pagerStyle == null ? null : ((IStateManager)pagerStyle).SaveViewState()), // 6
  2073. (rowStyle == null ? null : ((IStateManager)rowStyle).SaveViewState()), // 7
  2074. (selectedRowStyle == null ? null : ((IStateManager)selectedRowStyle).SaveViewState()), // 8
  2075. (editRowStyle == null ? null : ((IStateManager)editRowStyle).SaveViewState()), // 9
  2076. (emptyDataRowStyle == null ? null : ((IStateManager)emptyDataRowStyle).SaveViewState()), // 10
  2077. autoFieldsData, // 11
  2078. #if NET_4_0
  2079. sortedAscendingCellStyle == null ? null : ((IStateManager)sortedAscendingCellStyle).SaveViewState (), // 12
  2080. sortedAscendingHeaderStyle == null ? null : ((IStateManager)sortedAscendingHeaderStyle).SaveViewState (), // 13
  2081. sortedDescendingCellStyle == null ? null : ((IStateManager)sortedDescendingCellStyle).SaveViewState (), // 14
  2082. sortedDescendingHeaderStyle == null ? null : ((IStateManager)sortedDescendingHeaderStyle).SaveViewState () // 15
  2083. #endif
  2084. };
  2085. for (int i = states.Length - 1; i >= 0; i--) {
  2086. if (states [i] != null)
  2087. return states;
  2088. }
  2089. return null;
  2090. }
  2091. protected override void LoadViewState (object savedState)
  2092. {
  2093. if (savedState == null) {
  2094. base.LoadViewState (null);
  2095. return;
  2096. }
  2097. object [] states = (object []) savedState;
  2098. if (states[11] != null) {
  2099. object[] data = (object[]) states [11];
  2100. autoFieldProperties = new AutoGeneratedFieldProperties [data.Length];
  2101. for (int n=0; n<data.Length; n++) {
  2102. IStateManager p = new AutoGeneratedFieldProperties ();
  2103. p.TrackViewState ();
  2104. p.LoadViewState (data [n]);
  2105. autoFieldProperties [n] = (AutoGeneratedFieldProperties) p;
  2106. }
  2107. }
  2108. base.LoadViewState (states[0]);
  2109. if (states[1] != null)
  2110. ((IStateManager)Columns).LoadViewState (states[1]);
  2111. if (states[2] != null)
  2112. ((IStateManager)PagerSettings).LoadViewState (states[2]);
  2113. if (states[3] != null)
  2114. ((IStateManager)AlternatingRowStyle).LoadViewState (states[3]);
  2115. if (states[4] != null)
  2116. ((IStateManager)FooterStyle).LoadViewState (states[4]);
  2117. if (states[5] != null)
  2118. ((IStateManager)HeaderStyle).LoadViewState (states[5]);
  2119. if (states[6] != null)
  2120. ((IStateManager)PagerStyle).LoadViewState (states[6]);
  2121. if (states[7] != null)
  2122. ((IStateManager)RowStyle).LoadViewState (states[7]);
  2123. if (states[8] != null)
  2124. ((IStateManager)SelectedRowStyle).LoadViewState (states[8]);
  2125. if (states[9] != null)
  2126. ((IStateManager)EditRowStyle).LoadViewState (states[9]);
  2127. if (states[10] != null)
  2128. ((IStateManager)EmptyDataRowStyle).LoadViewState (states[10]);
  2129. #if NET_4_0
  2130. if (states [12] != null)
  2131. ((IStateManager)sortedAscendingCellStyle).LoadViewState (states [12]);
  2132. if (states [13] != null)
  2133. ((IStateManager)sortedAscendingHeaderStyle).LoadViewState (states [13]);
  2134. if (states [14] != null)
  2135. ((IStateManager)sortedDescendingCellStyle).LoadViewState (states [14]);
  2136. if (states [15] != null)
  2137. ((IStateManager)sortedDescendingHeaderStyle).LoadViewState (states [15]);
  2138. #endif
  2139. }
  2140. void ICallbackEventHandler.RaiseCallbackEvent (string eventArgs)
  2141. {
  2142. RaiseCallbackEvent (eventArgs);
  2143. }
  2144. protected virtual void RaiseCallbackEvent (string eventArgs)
  2145. {
  2146. string[] clientData = eventArgs.Split ('|');
  2147. PageIndex = int.Parse (clientData[0]);
  2148. SortExpression = HttpUtility.UrlDecode (clientData [1]);
  2149. SortDirection = (SortDirection) int.Parse (clientData [2]);
  2150. RaisePostBackEvent (clientData[3]);
  2151. DataBind ();
  2152. }
  2153. string ICallbackEventHandler.GetCallbackResult ()
  2154. {
  2155. return GetCallbackResult ();
  2156. }
  2157. protected virtual string GetCallbackResult ()
  2158. {
  2159. PrepareControlHierarchy ();
  2160. StringWriter sw = new StringWriter ();
  2161. sw.Write (PageIndex.ToString () + '|' + SortExpression + '|' + (int) SortDirection + '|');
  2162. HtmlTextWriter writer = new HtmlTextWriter (sw);
  2163. RenderGrid (writer);
  2164. return sw.ToString ();
  2165. }
  2166. string ICallbackContainer.GetCallbackScript (IButtonControl control, string argument)
  2167. {
  2168. return GetCallbackScript (control, argument);
  2169. }
  2170. protected virtual string GetCallbackScript (IButtonControl control, string argument)
  2171. {
  2172. if (EnableSortingAndPagingCallbacks) {
  2173. Page page = Page;
  2174. if (page != null)
  2175. page.ClientScript.RegisterForEventValidation (UniqueID, argument);
  2176. return "javascript:GridView_ClientEvent (\"" + ClientID + "\",\"" + control.CommandName + "$" + control.CommandArgument + "\"); return false;";
  2177. } else
  2178. return null;
  2179. }
  2180. protected override void OnPagePreLoad (object sender, EventArgs e)
  2181. {
  2182. base.OnPagePreLoad (sender, e);
  2183. Page page = Page;
  2184. if (page != null && page.IsPostBack && EnableSortingAndPagingCallbacks) {
  2185. HttpRequest req = page.Request;
  2186. int value;
  2187. if (req != null) {
  2188. if (int.TryParse (req.Form [ClientID + "_Page"], out value))
  2189. PageIndex = value;
  2190. if (int.TryParse (req.Form [ClientID + "_SortDirection"], out value))
  2191. SortDirection = (SortDirection) value;
  2192. SortExpression = req.Form [ClientID + "_SortExpression"];
  2193. }
  2194. }
  2195. }
  2196. const string onPreRenderScript = @"var {0} = new Object ();
  2197. {0}.pageIndex = {1};
  2198. {0}.sortExp = {2};
  2199. {0}.sortDir = {3};
  2200. {0}.uid = {4};
  2201. {0}.form = {5};
  2202. ";
  2203. protected internal override void OnPreRender (EventArgs e)
  2204. {
  2205. base.OnPreRender (e);
  2206. if (EnableSortingAndPagingCallbacks) {
  2207. Page page = Page;
  2208. ClientScriptManager scriptManager = page != null ? page.ClientScript : null;
  2209. if (scriptManager != null) {
  2210. if (!scriptManager.IsClientScriptIncludeRegistered (typeof(GridView), "GridView.js")) {
  2211. string url = scriptManager.GetWebResourceUrl (typeof(GridView), "GridView.js");
  2212. scriptManager.RegisterClientScriptInclude (typeof(GridView), "GridView.js", url);
  2213. }
  2214. string cgrid = ClientID + "_data";
  2215. string script = String.Format (onPreRenderScript,
  2216. cgrid,
  2217. ClientScriptManager.GetScriptLiteral (PageIndex),
  2218. ClientScriptManager.GetScriptLiteral (SortExpression == null ? String.Empty : SortExpression),
  2219. ClientScriptManager.GetScriptLiteral ((int) SortDirection),
  2220. ClientScriptManager.GetScriptLiteral (UniqueID),
  2221. page.theForm);
  2222. scriptManager.RegisterStartupScript (typeof(TreeView), this.UniqueID, script, true);
  2223. // Make sure the basic script infrastructure is rendered
  2224. scriptManager.GetCallbackEventReference (this, "null", String.Empty, "null");
  2225. scriptManager.GetPostBackClientHyperlink (this, String.Empty);
  2226. }
  2227. }
  2228. }
  2229. protected internal override void Render (HtmlTextWriter writer)
  2230. {
  2231. PrepareControlHierarchy ();
  2232. if (EnableSortingAndPagingCallbacks)
  2233. writer.AddAttribute (HtmlTextWriterAttribute.Id, ClientID + "_div");
  2234. writer.RenderBeginTag (HtmlTextWriterTag.Div);
  2235. RenderGrid (writer);
  2236. writer.RenderEndTag ();
  2237. }
  2238. void RenderGrid (HtmlTextWriter writer)
  2239. {
  2240. if (table == null)
  2241. return;
  2242. table.Render (writer);
  2243. }
  2244. PostBackOptions IPostBackContainer.GetPostBackOptions (IButtonControl control)
  2245. {
  2246. if (control == null)
  2247. throw new ArgumentNullException ("control");
  2248. if (control.CausesValidation)
  2249. throw new InvalidOperationException ("A button that causes validation in GridView '" + ID + "' is attempting to use the container GridView as the post back target. The button should either turn off validation or use itself as the post back container.");
  2250. PostBackOptions options = new PostBackOptions (this);
  2251. options.Argument = control.CommandName + "$" + control.CommandArgument;
  2252. options.RequiresJavaScriptProtocol = true;
  2253. return options;
  2254. }
  2255. }
  2256. }