GridView.cs 65 KB

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