GridView.cs 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208
  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. int fieldCount = fieldCollection.Count;
  1148. DataControlField dcf;
  1149. DataControlField[] fields = new DataControlField [fieldCount];
  1150. fieldCollection.CopyTo (fields, 0);
  1151. for (int i = 0; i < fieldCount; i++) {
  1152. dcf = fields [i];
  1153. dcf.Initialize (AllowSorting, this);
  1154. if (EnableSortingAndPagingCallbacks)
  1155. dcf.ValidateSupportsCallback ();
  1156. }
  1157. bool skip_first = false;
  1158. IEnumerator enumerator;
  1159. if (_dataEnumerator != null) {
  1160. // replaced when creating bound columns
  1161. enumerator = _dataEnumerator;
  1162. skip_first = true;
  1163. }
  1164. else {
  1165. enumerator = dataSource.GetEnumerator ();
  1166. }
  1167. // Main table creation
  1168. while (skip_first || enumerator.MoveNext ()) {
  1169. skip_first = false;
  1170. object obj = enumerator.Current;
  1171. if (list.Count == 0) {
  1172. if (createPager && (PagerSettings.Position == PagerPosition.Top || PagerSettings.Position == PagerPosition.TopAndBottom)) {
  1173. topPagerRow = CreatePagerRow (fieldCount, dataSource);
  1174. OnRowCreated (new GridViewRowEventArgs (topPagerRow));
  1175. ContainedTable.Rows.Add (topPagerRow);
  1176. if (dataBinding) {
  1177. topPagerRow.DataBind ();
  1178. OnRowDataBound (new GridViewRowEventArgs (topPagerRow));
  1179. }
  1180. if (PageCount == 1)
  1181. topPagerRow.Visible = false;
  1182. }
  1183. GridViewRow headerRow = CreateRow (-1, -1, DataControlRowType.Header, DataControlRowState.Normal);
  1184. InitializeRow (headerRow, fields);
  1185. OnRowCreated (new GridViewRowEventArgs (headerRow));
  1186. ContainedTable.Rows.Add (headerRow);
  1187. if (dataBinding) {
  1188. headerRow.DataBind ();
  1189. OnRowDataBound (new GridViewRowEventArgs (headerRow));
  1190. }
  1191. }
  1192. DataControlRowState rstate = GetRowState (list.Count);
  1193. GridViewRow row = CreateRow (list.Count, list.Count, DataControlRowType.DataRow, rstate);
  1194. row.DataItem = obj;
  1195. list.Add (row);
  1196. InitializeRow (row, fields);
  1197. OnRowCreated (new GridViewRowEventArgs (row));
  1198. ContainedTable.Rows.Add (row);
  1199. if (dataBinding) {
  1200. row.DataBind ();
  1201. OnRowDataBound (new GridViewRowEventArgs (row));
  1202. if (EditIndex == row.RowIndex)
  1203. oldEditValues = new DataKey (GetRowValues (row, true, true));
  1204. DataKeyArrayList.Add (new DataKey (CreateRowDataKey (row), DataKeyNames));
  1205. }
  1206. }
  1207. if (list.Count == 0) {
  1208. GridViewRow emptyRow = CreateEmptyrRow (fieldCount);
  1209. if (emptyRow != null) {
  1210. OnRowCreated (new GridViewRowEventArgs (emptyRow));
  1211. ContainedTable.Rows.Add (emptyRow);
  1212. if (dataBinding) {
  1213. emptyRow.DataBind ();
  1214. OnRowDataBound (new GridViewRowEventArgs (emptyRow));
  1215. }
  1216. }
  1217. return 0;
  1218. }
  1219. else {
  1220. GridViewRow footerRow = CreateRow (-1, -1, DataControlRowType.Footer, DataControlRowState.Normal);
  1221. InitializeRow (footerRow, fields);
  1222. OnRowCreated (new GridViewRowEventArgs (footerRow));
  1223. ContainedTable.Rows.Add (footerRow);
  1224. if (dataBinding) {
  1225. footerRow.DataBind ();
  1226. OnRowDataBound (new GridViewRowEventArgs (footerRow));
  1227. }
  1228. if (createPager && (PagerSettings.Position == PagerPosition.Bottom || PagerSettings.Position == PagerPosition.TopAndBottom)) {
  1229. bottomPagerRow = CreatePagerRow (fieldCount, dataSource);
  1230. OnRowCreated (new GridViewRowEventArgs (bottomPagerRow));
  1231. ContainedTable.Rows.Add (bottomPagerRow);
  1232. if (dataBinding) {
  1233. bottomPagerRow.DataBind ();
  1234. OnRowDataBound (new GridViewRowEventArgs (bottomPagerRow));
  1235. }
  1236. if (PageCount == 1)
  1237. bottomPagerRow.Visible = false;
  1238. }
  1239. }
  1240. rows = new GridViewRowCollection (list);
  1241. if (!dataBinding)
  1242. return -1;
  1243. if (AllowPaging)
  1244. return dataSource.DataSourceCount;
  1245. else
  1246. return list.Count;
  1247. }
  1248. Table ContainedTable {
  1249. get {
  1250. if (table == null) {
  1251. table = CreateChildTable ();
  1252. Controls.Add (table);
  1253. }
  1254. return table;
  1255. }
  1256. }
  1257. protected override Style CreateControlStyle ()
  1258. {
  1259. TableStyle style = new TableStyle (ViewState);
  1260. style.GridLines = GridLines.Both;
  1261. style.CellSpacing = 0;
  1262. return style;
  1263. }
  1264. DataControlRowState GetRowState (int index)
  1265. {
  1266. DataControlRowState rstate = (index % 2) == 0 ? DataControlRowState.Normal : DataControlRowState.Alternate;
  1267. if (index == SelectedIndex) rstate |= DataControlRowState.Selected;
  1268. if (index == EditIndex) rstate |= DataControlRowState.Edit;
  1269. return rstate;
  1270. }
  1271. GridViewRow CreatePagerRow (int fieldCount, PagedDataSource dataSource)
  1272. {
  1273. GridViewRow row = CreateRow (-1, -1, DataControlRowType.Pager, DataControlRowState.Normal);
  1274. InitializePager (row, fieldCount, dataSource);
  1275. return row;
  1276. }
  1277. protected virtual void InitializePager (GridViewRow row, int columnSpan, PagedDataSource dataSource)
  1278. {
  1279. TableCell cell = new TableCell ();
  1280. if (columnSpan > 1)
  1281. cell.ColumnSpan = columnSpan;
  1282. if (pagerTemplate != null)
  1283. pagerTemplate.InstantiateIn (cell);
  1284. else
  1285. cell.Controls.Add (PagerSettings.CreatePagerControl (dataSource.CurrentPageIndex, dataSource.PageCount));
  1286. row.Cells.Add (cell);
  1287. }
  1288. GridViewRow CreateEmptyrRow (int fieldCount)
  1289. {
  1290. if (emptyDataTemplate == null && String.IsNullOrEmpty (EmptyDataText))
  1291. return null;
  1292. GridViewRow row = CreateRow (-1, -1, DataControlRowType.EmptyDataRow, DataControlRowState.Normal);
  1293. TableCell cell = new TableCell ();
  1294. cell.ColumnSpan = fieldCount;
  1295. if (emptyDataTemplate != null)
  1296. emptyDataTemplate.InstantiateIn (cell);
  1297. else
  1298. cell.Text = EmptyDataText;
  1299. row.Cells.Add (cell);
  1300. return row;
  1301. }
  1302. protected virtual void InitializeRow (GridViewRow row, DataControlField[] fields)
  1303. {
  1304. DataControlCellType ctype;
  1305. bool accessibleHeader = false;
  1306. switch (row.RowType) {
  1307. case DataControlRowType.Header:
  1308. ctype = DataControlCellType.Header;
  1309. accessibleHeader = UseAccessibleHeader;
  1310. break;
  1311. case DataControlRowType.Footer:
  1312. ctype = DataControlCellType.Footer;
  1313. break;
  1314. default:
  1315. ctype = DataControlCellType.DataCell;
  1316. break;
  1317. }
  1318. for (int n=0; n<fields.Length; n++) {
  1319. DataControlField field = fields [n];
  1320. DataControlFieldCell cell;
  1321. if (((field is BoundField) && ((BoundField)field).DataField == RowHeaderColumn) || accessibleHeader)
  1322. cell = new DataControlFieldHeaderCell (field, accessibleHeader ? TableHeaderScope.Column : TableHeaderScope.Row);
  1323. else
  1324. cell = new DataControlFieldCell (field);
  1325. row.Cells.Add (cell);
  1326. field.InitializeCell (cell, ctype, row.RowState, row.RowIndex);
  1327. }
  1328. }
  1329. IOrderedDictionary CreateRowDataKey (GridViewRow row)
  1330. {
  1331. if (cachedKeyProperties == null) {
  1332. PropertyDescriptorCollection props = TypeDescriptor.GetProperties (row.DataItem);
  1333. cachedKeyProperties = new PropertyDescriptor [DataKeyNames.Length];
  1334. for (int n=0; n<DataKeyNames.Length; n++) {
  1335. PropertyDescriptor p = props.Find (DataKeyNames [n], true);
  1336. if (p == null)
  1337. throw new InvalidOperationException ("Property '" + DataKeyNames [n] + "' not found in object of type " + row.DataItem.GetType ());
  1338. cachedKeyProperties [n] = p;
  1339. }
  1340. }
  1341. OrderedDictionary dic = new OrderedDictionary ();
  1342. foreach (PropertyDescriptor p in cachedKeyProperties)
  1343. dic [p.Name] = p.GetValue (row.DataItem);
  1344. return dic;
  1345. }
  1346. IOrderedDictionary GetRowValues (GridViewRow row, bool includeReadOnlyFields, bool includePrimaryKey)
  1347. {
  1348. OrderedDictionary dic = new OrderedDictionary ();
  1349. ExtractRowValues (dic, row, includeReadOnlyFields, includePrimaryKey);
  1350. return dic;
  1351. }
  1352. protected virtual void ExtractRowValues (IOrderedDictionary fieldValues, GridViewRow row, bool includeReadOnlyFields, bool includePrimaryKey)
  1353. {
  1354. DataControlField field;
  1355. foreach (TableCell cell in row.Cells) {
  1356. DataControlFieldCell c = cell as DataControlFieldCell;
  1357. if (c == null)
  1358. continue;
  1359. field = c.ContainingField;
  1360. if (field != null && !field.Visible)
  1361. continue;
  1362. c.ContainingField.ExtractValuesFromCell (fieldValues, c, row.RowState, includeReadOnlyFields);
  1363. }
  1364. if (!includePrimaryKey && DataKeyNames != null)
  1365. foreach (string key in DataKeyNames)
  1366. fieldValues.Remove (key);
  1367. }
  1368. protected override HtmlTextWriterTag TagKey {
  1369. get {
  1370. if (EnableSortingAndPagingCallbacks)
  1371. return HtmlTextWriterTag.Div;
  1372. else
  1373. return HtmlTextWriterTag.Table;
  1374. }
  1375. }
  1376. public sealed override void DataBind ()
  1377. {
  1378. DataKeyArrayList.Clear ();
  1379. cachedKeyProperties = null;
  1380. base.DataBind ();
  1381. keys = new DataKeyArray (DataKeyArrayList);
  1382. }
  1383. protected internal override void PerformDataBinding (IEnumerable data)
  1384. {
  1385. base.PerformDataBinding (data);
  1386. }
  1387. protected internal virtual void PrepareControlHierarchy ()
  1388. {
  1389. if (table == null)
  1390. return;
  1391. table.Caption = Caption;
  1392. table.CaptionAlign = CaptionAlign;
  1393. table.CopyBaseAttributes (this);
  1394. foreach (GridViewRow row in table.Rows) {
  1395. switch (row.RowType) {
  1396. case DataControlRowType.Header:
  1397. if (headerStyle != null && !headerStyle.IsEmpty)
  1398. row.ControlStyle.MergeWith(headerStyle);
  1399. row.Visible = ShowHeader;
  1400. break;
  1401. case DataControlRowType.Footer:
  1402. if (footerStyle != null && !footerStyle.IsEmpty)
  1403. row.ControlStyle.MergeWith (footerStyle);
  1404. row.Visible = ShowFooter;
  1405. break;
  1406. case DataControlRowType.Pager:
  1407. if (pagerStyle != null && !pagerStyle.IsEmpty)
  1408. row.ControlStyle.MergeWith (pagerStyle);
  1409. break;
  1410. case DataControlRowType.EmptyDataRow:
  1411. if (emptyDataRowStyle != null && !emptyDataRowStyle.IsEmpty)
  1412. row.ControlStyle.MergeWith (emptyDataRowStyle);
  1413. break;
  1414. case DataControlRowType.DataRow:
  1415. if ((row.RowState & DataControlRowState.Edit) != 0 && editRowStyle != null && !editRowStyle.IsEmpty)
  1416. row.ControlStyle.MergeWith (editRowStyle);
  1417. if ((row.RowState & DataControlRowState.Selected) != 0 && selectedRowStyle != null && !selectedRowStyle.IsEmpty)
  1418. row.ControlStyle.MergeWith (selectedRowStyle);
  1419. if ((row.RowState & DataControlRowState.Alternate) != 0 && alternatingRowStyle != null && !alternatingRowStyle.IsEmpty)
  1420. row.ControlStyle.MergeWith (alternatingRowStyle);
  1421. if (rowStyle != null && !rowStyle.IsEmpty)
  1422. row.ControlStyle.MergeWith (rowStyle);
  1423. break;
  1424. default:
  1425. break;
  1426. }
  1427. foreach (TableCell cell in row.Cells) {
  1428. DataControlFieldCell fcell = cell as DataControlFieldCell;
  1429. if (fcell != null) {
  1430. DataControlField field = fcell.ContainingField;
  1431. if (field == null)
  1432. continue;
  1433. if (!field.Visible) {
  1434. cell.Visible = false;
  1435. continue;
  1436. }
  1437. switch (row.RowType) {
  1438. case DataControlRowType.Header:
  1439. if (field.HeaderStyleCreated && !field.HeaderStyle.IsEmpty)
  1440. cell.ControlStyle.MergeWith (field.HeaderStyle);
  1441. break;
  1442. case DataControlRowType.Footer:
  1443. if (field.FooterStyleCreated && !field.FooterStyle.IsEmpty)
  1444. cell.ControlStyle.MergeWith (field.FooterStyle);
  1445. break;
  1446. default:
  1447. if (field.ControlStyleCreated && !field.ControlStyle.IsEmpty)
  1448. foreach (Control c in cell.Controls) {
  1449. WebControl wc = c as WebControl;
  1450. if (wc != null)
  1451. wc.ControlStyle.MergeWith (field.ControlStyle);
  1452. }
  1453. if (field.ItemStyleCreated && !field.ItemStyle.IsEmpty)
  1454. cell.ControlStyle.MergeWith (field.ItemStyle);
  1455. break;
  1456. }
  1457. }
  1458. }
  1459. }
  1460. }
  1461. protected internal override void OnInit (EventArgs e)
  1462. {
  1463. Page.RegisterRequiresControlState (this);
  1464. base.OnInit (e);
  1465. }
  1466. void OnFieldsChanged (object sender, EventArgs args)
  1467. {
  1468. RequireBinding ();
  1469. }
  1470. protected override void OnDataPropertyChanged ()
  1471. {
  1472. base.OnDataPropertyChanged ();
  1473. RequireBinding ();
  1474. }
  1475. protected override void OnDataSourceViewChanged (object sender, EventArgs e)
  1476. {
  1477. base.OnDataSourceViewChanged (sender, e);
  1478. RequireBinding ();
  1479. }
  1480. protected override bool OnBubbleEvent (object source, EventArgs e)
  1481. {
  1482. GridViewCommandEventArgs args = e as GridViewCommandEventArgs;
  1483. if (args != null) {
  1484. bool causesValidation = false;
  1485. IButtonControl button = args.CommandSource as IButtonControl;
  1486. if (button != null && button.CausesValidation) {
  1487. Page.Validate (button.ValidationGroup);
  1488. causesValidation = true;
  1489. }
  1490. OnRowCommand (args);
  1491. string param = args.CommandArgument as string;
  1492. if (param == null || param.Length == 0) {
  1493. GridViewRow row = args.Row;
  1494. if (row != null)
  1495. param = row.RowIndex.ToString();
  1496. }
  1497. ProcessEvent (args.CommandName, param, causesValidation);
  1498. return true;
  1499. }
  1500. return base.OnBubbleEvent (source, e);
  1501. }
  1502. void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
  1503. {
  1504. RaisePostBackEvent (eventArgument);
  1505. }
  1506. // This is prolly obsolete
  1507. protected virtual void RaisePostBackEvent (string eventArgument)
  1508. {
  1509. GridViewCommandEventArgs args;
  1510. int i = eventArgument.IndexOf ('$');
  1511. if (i != -1)
  1512. args = new GridViewCommandEventArgs (this, new CommandEventArgs (eventArgument.Substring (0, i), eventArgument.Substring (i + 1)));
  1513. else
  1514. args = new GridViewCommandEventArgs (this, new CommandEventArgs (eventArgument, null));
  1515. OnRowCommand (args);
  1516. ProcessEvent (args.CommandName, (string) args.CommandArgument, false);
  1517. }
  1518. void ProcessEvent (string eventName, string param, bool causesValidation)
  1519. {
  1520. switch (eventName)
  1521. {
  1522. case DataControlCommands.PageCommandName:
  1523. int newIndex = -1;
  1524. switch (param) {
  1525. case DataControlCommands.FirstPageCommandArgument:
  1526. newIndex = 0;
  1527. break;
  1528. case DataControlCommands.LastPageCommandArgument:
  1529. newIndex = PageCount - 1;
  1530. break;
  1531. case DataControlCommands.NextPageCommandArgument:
  1532. newIndex = PageIndex + 1;
  1533. break;
  1534. case DataControlCommands.PreviousPageCommandArgument:
  1535. newIndex = PageIndex - 1;
  1536. break;
  1537. default:
  1538. int paramIndex = 0;
  1539. int.TryParse (param, out paramIndex);
  1540. newIndex = paramIndex - 1;
  1541. break;
  1542. }
  1543. ShowPage (newIndex);
  1544. break;
  1545. case DataControlCommands.FirstPageCommandArgument:
  1546. ShowPage (0);
  1547. break;
  1548. case DataControlCommands.LastPageCommandArgument:
  1549. ShowPage (PageCount - 1);
  1550. break;
  1551. case DataControlCommands.NextPageCommandArgument:
  1552. if (PageIndex < PageCount - 1)
  1553. ShowPage (PageIndex + 1);
  1554. break;
  1555. case DataControlCommands.PreviousPageCommandArgument:
  1556. if (PageIndex > 0)
  1557. ShowPage (PageIndex - 1);
  1558. break;
  1559. case DataControlCommands.SelectCommandName:
  1560. SelectRow (int.Parse (param));
  1561. break;
  1562. case DataControlCommands.EditCommandName:
  1563. EditRow (int.Parse (param));
  1564. break;
  1565. case DataControlCommands.UpdateCommandName:
  1566. int editIndex = int.Parse (param);
  1567. UpdateRow (Rows [editIndex], editIndex, causesValidation);
  1568. break;
  1569. case DataControlCommands.CancelCommandName:
  1570. CancelEdit ();
  1571. break;
  1572. case DataControlCommands.DeleteCommandName:
  1573. DeleteRow (int.Parse (param));
  1574. break;
  1575. case DataControlCommands.SortCommandName:
  1576. Sort (param);
  1577. break;
  1578. }
  1579. }
  1580. void Sort (string newSortExpression)
  1581. {
  1582. SortDirection newDirection = SortDirection.Ascending;
  1583. if (sortExpression == newSortExpression && sortDirection == SortDirection.Ascending)
  1584. newDirection = SortDirection.Descending;
  1585. Sort (newSortExpression, newDirection);
  1586. }
  1587. public virtual void Sort (string newSortExpression, SortDirection newSortDirection)
  1588. {
  1589. GridViewSortEventArgs args = new GridViewSortEventArgs (newSortExpression, newSortDirection);
  1590. OnSorting (args);
  1591. if (args.Cancel) return;
  1592. if (IsBoundUsingDataSourceID) {
  1593. EditIndex = -1;
  1594. PageIndex = 0;
  1595. SortExpression = args.SortExpression;
  1596. SortDirection = args.SortDirection;
  1597. }
  1598. OnSorted (EventArgs.Empty);
  1599. }
  1600. void SelectRow (int index)
  1601. {
  1602. GridViewSelectEventArgs args = new GridViewSelectEventArgs (index);
  1603. OnSelectedIndexChanging (args);
  1604. if (!args.Cancel) {
  1605. RequireBinding ();
  1606. SelectedIndex = args.NewSelectedIndex;
  1607. OnSelectedIndexChanged (EventArgs.Empty);
  1608. }
  1609. }
  1610. void ShowPage (int newIndex)
  1611. {
  1612. GridViewPageEventArgs args = new GridViewPageEventArgs (newIndex);
  1613. OnPageIndexChanging (args);
  1614. if (args.Cancel || !IsBoundUsingDataSourceID)
  1615. return;
  1616. EndRowEdit ();
  1617. PageIndex = args.NewPageIndex;
  1618. OnPageIndexChanged (EventArgs.Empty);
  1619. }
  1620. void EditRow (int index)
  1621. {
  1622. GridViewEditEventArgs args = new GridViewEditEventArgs (index);
  1623. OnRowEditing (args);
  1624. if (args.Cancel || !IsBoundUsingDataSourceID)
  1625. return;
  1626. EditIndex = args.NewEditIndex;
  1627. }
  1628. void CancelEdit ()
  1629. {
  1630. GridViewCancelEditEventArgs args = new GridViewCancelEditEventArgs (EditIndex);
  1631. OnRowCancelingEdit (args);
  1632. if (args.Cancel || !IsBoundUsingDataSourceID)
  1633. return;
  1634. EndRowEdit ();
  1635. }
  1636. [MonoTODO ("Support two-way binding expressions")]
  1637. public virtual void UpdateRow (int rowIndex, bool causesValidation)
  1638. {
  1639. if (rowIndex != EditIndex) throw new NotSupportedException ();
  1640. GridViewRow row = Rows [rowIndex];
  1641. UpdateRow (row, rowIndex, causesValidation);
  1642. }
  1643. void UpdateRow (GridViewRow row, int rowIndex, bool causesValidation)
  1644. {
  1645. if (causesValidation && Page != null && !Page.IsValid)
  1646. return;
  1647. currentEditOldValues = CopyOrderedDictionary (OldEditValues.Values);
  1648. currentEditRowKeys = CopyOrderedDictionary (DataKeys [rowIndex].Values);
  1649. currentEditNewValues = GetRowValues (row, false, false);
  1650. GridViewUpdateEventArgs args = new GridViewUpdateEventArgs (rowIndex, currentEditRowKeys, currentEditOldValues, currentEditNewValues);
  1651. OnRowUpdating (args);
  1652. if (args.Cancel || !IsBoundUsingDataSourceID)
  1653. return;
  1654. DataSourceView view = GetData ();
  1655. if (view == null)
  1656. throw new HttpException ("The DataSourceView associated to data bound control was null");
  1657. view.Update (currentEditRowKeys, currentEditNewValues, currentEditOldValues, new DataSourceViewOperationCallback (UpdateCallback));
  1658. }
  1659. static IOrderedDictionary CopyOrderedDictionary (IOrderedDictionary sourceDic) {
  1660. OrderedDictionary copyDic = new OrderedDictionary ();
  1661. foreach (object key in sourceDic.Keys) {
  1662. copyDic.Add (key, sourceDic [key]);
  1663. }
  1664. return copyDic;
  1665. }
  1666. bool UpdateCallback (int recordsAffected, Exception exception)
  1667. {
  1668. GridViewUpdatedEventArgs dargs = new GridViewUpdatedEventArgs (recordsAffected, exception, currentEditRowKeys, currentEditOldValues, currentEditNewValues);
  1669. OnRowUpdated (dargs);
  1670. if (!dargs.KeepInEditMode)
  1671. EndRowEdit ();
  1672. return dargs.ExceptionHandled;
  1673. }
  1674. public virtual void DeleteRow (int rowIndex)
  1675. {
  1676. GridViewRow row = Rows [rowIndex];
  1677. currentEditRowKeys = CopyOrderedDictionary (DataKeys [rowIndex].Values);
  1678. currentEditNewValues = GetRowValues (row, true, true);
  1679. GridViewDeleteEventArgs args = new GridViewDeleteEventArgs (rowIndex, currentEditRowKeys, currentEditNewValues);
  1680. OnRowDeleting (args);
  1681. if (args.Cancel || !IsBoundUsingDataSourceID)
  1682. return;
  1683. RequireBinding ();
  1684. DataSourceView view = GetData ();
  1685. if (view != null)
  1686. view.Delete (currentEditRowKeys, currentEditNewValues, new DataSourceViewOperationCallback (DeleteCallback));
  1687. else {
  1688. GridViewDeletedEventArgs dargs = new GridViewDeletedEventArgs (0, null, currentEditRowKeys, currentEditNewValues);
  1689. OnRowDeleted (dargs);
  1690. }
  1691. }
  1692. bool DeleteCallback (int recordsAffected, Exception exception)
  1693. {
  1694. GridViewDeletedEventArgs dargs = new GridViewDeletedEventArgs (recordsAffected, exception, currentEditRowKeys, currentEditNewValues);
  1695. OnRowDeleted (dargs);
  1696. return dargs.ExceptionHandled;
  1697. }
  1698. void EndRowEdit ()
  1699. {
  1700. EditIndex = -1;
  1701. oldEditValues = new DataKey (new OrderedDictionary ());
  1702. currentEditRowKeys = null;
  1703. currentEditOldValues = null;
  1704. currentEditNewValues = null;
  1705. }
  1706. protected internal override void LoadControlState (object ob)
  1707. {
  1708. if (ob == null) return;
  1709. object[] state = (object[]) ob;
  1710. base.LoadControlState (state[0]);
  1711. pageIndex = (int) state[1];
  1712. selectedIndex = (int) state[2];
  1713. editIndex = (int) state[3];
  1714. sortExpression = (string) state[4];
  1715. sortDirection = (SortDirection) state[5];
  1716. DataKeyNames = (string []) state [6];
  1717. if (state [7] != null)
  1718. LoadDataKeyArrayState ((object []) state [7]);
  1719. if (state [8] != null)
  1720. ((IStateManager) OldEditValues).LoadViewState (state [8]);
  1721. }
  1722. protected internal override object SaveControlState ()
  1723. {
  1724. if (EnableSortingAndPagingCallbacks) {
  1725. Page.ClientScript.RegisterHiddenField (ClientID + "_Page", PageIndex.ToString ());
  1726. Page.ClientScript.RegisterHiddenField (ClientID + "_SortExpression", SortExpression);
  1727. Page.ClientScript.RegisterHiddenField (ClientID + "_SortDirection", ((int)SortDirection).ToString());
  1728. }
  1729. object bstate = base.SaveControlState ();
  1730. return new object [] {
  1731. bstate,
  1732. pageIndex,
  1733. selectedIndex,
  1734. editIndex,
  1735. sortExpression,
  1736. sortDirection,
  1737. DataKeyNames,
  1738. SaveDataKeyArrayState (),
  1739. (oldEditValues == null ? null : ((IStateManager)oldEditValues).SaveViewState ())
  1740. };
  1741. }
  1742. object [] SaveDataKeyArrayState ()
  1743. {
  1744. if (keys == null)
  1745. return null;
  1746. object [] state = new object [keys.Count];
  1747. for (int i = 0; i < keys.Count; i++) {
  1748. state [i] = ((IStateManager) keys [i]).SaveViewState ();
  1749. }
  1750. return state;
  1751. }
  1752. void LoadDataKeyArrayState (object [] state)
  1753. {
  1754. for (int i = 0; i < state.Length; i++) {
  1755. DataKey dataKey = new DataKey (new OrderedDictionary (DataKeyNames.Length), DataKeyNames);
  1756. ((IStateManager) dataKey).LoadViewState (state [i]);
  1757. DataKeyArrayList.Add (dataKey);
  1758. }
  1759. keys = new DataKeyArray (DataKeyArrayList);
  1760. }
  1761. protected override void TrackViewState()
  1762. {
  1763. base.TrackViewState();
  1764. if (columns != null) ((IStateManager)columns).TrackViewState();
  1765. if (pagerSettings != null) ((IStateManager)pagerSettings).TrackViewState();
  1766. if (alternatingRowStyle != null) ((IStateManager)alternatingRowStyle).TrackViewState();
  1767. if (footerStyle != null) ((IStateManager)footerStyle).TrackViewState();
  1768. if (headerStyle != null) ((IStateManager)headerStyle).TrackViewState();
  1769. if (pagerStyle != null) ((IStateManager)pagerStyle).TrackViewState();
  1770. if (rowStyle != null) ((IStateManager)rowStyle).TrackViewState();
  1771. if (selectedRowStyle != null) ((IStateManager)selectedRowStyle).TrackViewState();
  1772. if (editRowStyle != null) ((IStateManager)editRowStyle).TrackViewState();
  1773. if (emptyDataRowStyle != null) ((IStateManager)emptyDataRowStyle).TrackViewState();
  1774. if (keys != null) ((IStateManager)keys).TrackViewState();
  1775. if (autoFieldProperties != null) {
  1776. foreach (IStateManager sm in autoFieldProperties)
  1777. sm.TrackViewState ();
  1778. }
  1779. }
  1780. protected override object SaveViewState()
  1781. {
  1782. object[] states = new object [12];
  1783. states[0] = base.SaveViewState();
  1784. states[1] = (columns == null ? null : ((IStateManager)columns).SaveViewState());
  1785. states[2] = (pagerSettings == null ? null : ((IStateManager)pagerSettings).SaveViewState());
  1786. states[3] = (alternatingRowStyle == null ? null : ((IStateManager)alternatingRowStyle).SaveViewState());
  1787. states[4] = (footerStyle == null ? null : ((IStateManager)footerStyle).SaveViewState());
  1788. states[5] = (headerStyle == null ? null : ((IStateManager)headerStyle).SaveViewState());
  1789. states[6] = (pagerStyle == null ? null : ((IStateManager)pagerStyle).SaveViewState());
  1790. states[7] = (rowStyle == null ? null : ((IStateManager)rowStyle).SaveViewState());
  1791. states[8] = (selectedRowStyle == null ? null : ((IStateManager)selectedRowStyle).SaveViewState());
  1792. states[9] = (editRowStyle == null ? null : ((IStateManager)editRowStyle).SaveViewState());
  1793. states[10] = (emptyDataRowStyle == null ? null : ((IStateManager)emptyDataRowStyle).SaveViewState());
  1794. if (autoFieldProperties != null) {
  1795. object[] data = new object [autoFieldProperties.Length];
  1796. bool allNull = true;
  1797. for (int n=0; n<data.Length; n++) {
  1798. data [n] = ((IStateManager)autoFieldProperties [n]).SaveViewState ();
  1799. if (data [n] != null) allNull = false;
  1800. }
  1801. if (!allNull) states [11] = data;
  1802. }
  1803. for (int i = states.Length - 1; i >= 0; i--) {
  1804. if (states [i] != null)
  1805. return states;
  1806. }
  1807. return null;
  1808. }
  1809. protected override void LoadViewState (object savedState)
  1810. {
  1811. if (savedState == null) {
  1812. base.LoadViewState (null);
  1813. return;
  1814. }
  1815. object [] states = (object []) savedState;
  1816. if (states[11] != null) {
  1817. object[] data = (object[]) states [11];
  1818. autoFieldProperties = new AutoGeneratedFieldProperties [data.Length];
  1819. for (int n=0; n<data.Length; n++) {
  1820. IStateManager p = new AutoGeneratedFieldProperties ();
  1821. p.TrackViewState ();
  1822. p.LoadViewState (data [n]);
  1823. autoFieldProperties [n] = (AutoGeneratedFieldProperties) p;
  1824. }
  1825. }
  1826. base.LoadViewState (states[0]);
  1827. if (states[1] != null) ((IStateManager)Columns).LoadViewState (states[1]);
  1828. if (states[2] != null) ((IStateManager)PagerSettings).LoadViewState (states[2]);
  1829. if (states[3] != null) ((IStateManager)AlternatingRowStyle).LoadViewState (states[3]);
  1830. if (states[4] != null) ((IStateManager)FooterStyle).LoadViewState (states[4]);
  1831. if (states[5] != null) ((IStateManager)HeaderStyle).LoadViewState (states[5]);
  1832. if (states[6] != null) ((IStateManager)PagerStyle).LoadViewState (states[6]);
  1833. if (states[7] != null) ((IStateManager)RowStyle).LoadViewState (states[7]);
  1834. if (states[8] != null) ((IStateManager)SelectedRowStyle).LoadViewState (states[8]);
  1835. if (states[9] != null) ((IStateManager)EditRowStyle).LoadViewState (states[9]);
  1836. if (states[10] != null) ((IStateManager)EmptyDataRowStyle).LoadViewState (states[10]);
  1837. }
  1838. void ICallbackEventHandler.RaiseCallbackEvent (string eventArgs)
  1839. {
  1840. RaiseCallbackEvent (eventArgs);
  1841. }
  1842. protected virtual void RaiseCallbackEvent (string eventArgs)
  1843. {
  1844. string[] clientData = eventArgs.Split ('|');
  1845. PageIndex = int.Parse (clientData[0]);
  1846. SortExpression = HttpUtility.UrlDecode (clientData [1]);
  1847. SortDirection = (SortDirection) int.Parse (clientData [2]);
  1848. RaisePostBackEvent (clientData[3]);
  1849. DataBind ();
  1850. }
  1851. string ICallbackEventHandler.GetCallbackResult ()
  1852. {
  1853. return GetCallbackResult ();
  1854. }
  1855. protected virtual string GetCallbackResult ()
  1856. {
  1857. PrepareControlHierarchy ();
  1858. StringWriter sw = new StringWriter ();
  1859. sw.Write (PageIndex.ToString () + '|' + SortExpression + '|' + (int) SortDirection + '|');
  1860. HtmlTextWriter writer = new HtmlTextWriter (sw);
  1861. RenderGrid (writer);
  1862. return sw.ToString ();
  1863. }
  1864. string ICallbackContainer.GetCallbackScript (IButtonControl control, string argument)
  1865. {
  1866. return GetCallbackScript (control, argument);
  1867. }
  1868. protected virtual string GetCallbackScript (IButtonControl control, string argument)
  1869. {
  1870. if (EnableSortingAndPagingCallbacks)
  1871. return "javascript:GridView_ClientEvent (\"" + ClientID + "\",\"" + control.CommandName + "$" + control.CommandArgument + "\"); return false;";
  1872. else
  1873. return null;
  1874. }
  1875. protected override void OnPagePreLoad (object sender, EventArgs e)
  1876. {
  1877. base.OnPagePreLoad (sender, e);
  1878. if (Page.IsPostBack && EnableSortingAndPagingCallbacks) {
  1879. int page;
  1880. if (int.TryParse (Page.Request.Form [ClientID + "_Page"], out page))
  1881. PageIndex = page;
  1882. int dir;
  1883. if (int.TryParse (Page.Request.Form [ClientID + "_SortDirection"], out dir))
  1884. SortDirection = (SortDirection) dir;
  1885. SortExpression = Page.Request.Form [ClientID + "_SortExpression"];
  1886. }
  1887. }
  1888. const string onPreRenderScript = @"var {0} = new Object ();
  1889. {0}.pageIndex = {1};
  1890. {0}.sortExp = {2};
  1891. {0}.sortDir = {3};
  1892. {0}.uid = {4};
  1893. {0}.form = {5};
  1894. ";
  1895. protected internal override void OnPreRender (EventArgs e)
  1896. {
  1897. base.OnPreRender (e);
  1898. if (EnableSortingAndPagingCallbacks) {
  1899. if (!Page.ClientScript.IsClientScriptIncludeRegistered (typeof(GridView), "GridView.js")) {
  1900. string url = Page.ClientScript.GetWebResourceUrl (typeof(GridView), "GridView.js");
  1901. Page.ClientScript.RegisterClientScriptInclude (typeof(GridView), "GridView.js", url);
  1902. }
  1903. string cgrid = ClientID + "_data";
  1904. string script = String.Format (onPreRenderScript,
  1905. cgrid,
  1906. ClientScriptManager.GetScriptLiteral (PageIndex),
  1907. ClientScriptManager.GetScriptLiteral (SortExpression == null ? "" : SortExpression),
  1908. ClientScriptManager.GetScriptLiteral ((int) SortDirection),
  1909. ClientScriptManager.GetScriptLiteral (UniqueID),
  1910. Page.theForm);
  1911. Page.ClientScript.RegisterStartupScript (typeof(TreeView), this.UniqueID, script, true);
  1912. // Make sure the basic script infrastructure is rendered
  1913. Page.ClientScript.GetCallbackEventReference (this, "null", "", "null");
  1914. Page.ClientScript.GetPostBackClientHyperlink (this, "");
  1915. }
  1916. }
  1917. protected internal override void Render (HtmlTextWriter writer)
  1918. {
  1919. PrepareControlHierarchy ();
  1920. if (EnableSortingAndPagingCallbacks)
  1921. writer.AddAttribute (HtmlTextWriterAttribute.Id, ClientID + "_div");
  1922. writer.RenderBeginTag (HtmlTextWriterTag.Div);
  1923. RenderGrid (writer);
  1924. writer.RenderEndTag ();
  1925. }
  1926. void RenderGrid (HtmlTextWriter writer)
  1927. {
  1928. if (table == null)
  1929. return;
  1930. table.Render (writer);
  1931. }
  1932. PostBackOptions IPostBackContainer.GetPostBackOptions (IButtonControl control)
  1933. {
  1934. if (control == null)
  1935. throw new ArgumentNullException ("control");
  1936. if (control.CausesValidation)
  1937. 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.");
  1938. PostBackOptions options = new PostBackOptions (this);
  1939. options.Argument = control.CommandName + "$" + control.CommandArgument;
  1940. options.RequiresJavaScriptProtocol = true;
  1941. return options;
  1942. }
  1943. }
  1944. }
  1945. #endif