GridView.cs 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226
  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. static readonly object PageIndexChangedEvent = new object();
  77. static readonly object PageIndexChangingEvent = new object();
  78. static readonly object RowCancelingEditEvent = new object();
  79. static readonly object RowCommandEvent = new object();
  80. static readonly object RowCreatedEvent = new object();
  81. static readonly object RowDataBoundEvent = new object();
  82. static readonly object RowDeletedEvent = new object();
  83. static readonly object RowDeletingEvent = new object();
  84. static readonly object RowEditingEvent = new object();
  85. static readonly object RowUpdatedEvent = new object();
  86. static readonly object RowUpdatingEvent = new object();
  87. static readonly object SelectedIndexChangedEvent = new object();
  88. static readonly object SelectedIndexChangingEvent = new object();
  89. static readonly object SortedEvent = new object();
  90. 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. [BrowsableAttribute(false)]
  490. public IAutoFieldGenerator ColumnsGenerator {
  491. get;
  492. set;
  493. }
  494. [DefaultValueAttribute (null)]
  495. [WebCategoryAttribute ("Data")]
  496. [TypeConverter (typeof(StringArrayConverter))]
  497. [EditorAttribute ("System.Web.UI.Design.WebControls.DataFieldEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  498. public virtual string[] DataKeyNames
  499. {
  500. get {
  501. if (dataKeyNames != null)
  502. return dataKeyNames;
  503. return emptyKeys;
  504. }
  505. set {
  506. dataKeyNames = value;
  507. RequireBinding ();
  508. }
  509. }
  510. ArrayList DataKeyArrayList {
  511. get {
  512. if (_dataKeyArrayList == null) {
  513. _dataKeyArrayList = new ArrayList ();
  514. }
  515. return _dataKeyArrayList;
  516. }
  517. }
  518. [BrowsableAttribute (false)]
  519. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  520. public virtual DataKeyArray DataKeys {
  521. get {
  522. if (keys == null) {
  523. keys = new DataKeyArray (DataKeyArrayList);
  524. if (IsTrackingViewState)
  525. ((IStateManager) keys).TrackViewState ();
  526. }
  527. return keys;
  528. }
  529. }
  530. DataKey OldEditValues {
  531. get {
  532. if (oldEditValues == null) {
  533. oldEditValues = new DataKey (new OrderedDictionary ());
  534. }
  535. return oldEditValues;
  536. }
  537. }
  538. [WebCategoryAttribute ("Misc")]
  539. [DefaultValueAttribute (-1)]
  540. public virtual int EditIndex {
  541. get {
  542. return editIndex;
  543. }
  544. set {
  545. if (value == editIndex)
  546. return;
  547. editIndex = value;
  548. RequireBinding ();
  549. }
  550. }
  551. [WebCategoryAttribute ("Styles")]
  552. [PersistenceMode (PersistenceMode.InnerProperty)]
  553. [NotifyParentProperty (true)]
  554. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  555. public TableItemStyle EditRowStyle {
  556. get {
  557. if (editRowStyle == null) {
  558. editRowStyle = new TableItemStyle ();
  559. if (IsTrackingViewState)
  560. editRowStyle.TrackViewState();
  561. }
  562. return editRowStyle;
  563. }
  564. }
  565. [WebCategoryAttribute ("Styles")]
  566. [PersistenceMode (PersistenceMode.InnerProperty)]
  567. [NotifyParentProperty (true)]
  568. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  569. public TableItemStyle EmptyDataRowStyle {
  570. get {
  571. if (emptyDataRowStyle == null) {
  572. emptyDataRowStyle = new TableItemStyle ();
  573. if (IsTrackingViewState)
  574. emptyDataRowStyle.TrackViewState();
  575. }
  576. return emptyDataRowStyle;
  577. }
  578. }
  579. [DefaultValue (null)]
  580. [TemplateContainer (typeof(GridViewRow), BindingDirection.OneWay)]
  581. [PersistenceMode (PersistenceMode.InnerProperty)]
  582. [Browsable (false)]
  583. public virtual ITemplate EmptyDataTemplate {
  584. get { return emptyDataTemplate; }
  585. set { emptyDataTemplate = value; }
  586. }
  587. [LocalizableAttribute (true)]
  588. [WebCategoryAttribute ("Appearance")]
  589. [DefaultValueAttribute ("")]
  590. public virtual string EmptyDataText {
  591. get {
  592. object ob = ViewState ["EmptyDataText"];
  593. if (ob != null) return (string) ob;
  594. return string.Empty;
  595. }
  596. set {
  597. if (value == EmptyDataText)
  598. return;
  599. ViewState ["EmptyDataText"] = value;
  600. RequireBinding ();
  601. }
  602. }
  603. [WebCategoryAttribute ("Behavior")]
  604. [DefaultValueAttribute (false)]
  605. public virtual bool EnableSortingAndPagingCallbacks {
  606. get {
  607. object ob = ViewState ["EnableSortingAndPagingCallbacks"];
  608. if (ob != null) return (bool) ob;
  609. return false;
  610. }
  611. set {
  612. if (value == EnableSortingAndPagingCallbacks)
  613. return;
  614. ViewState ["EnableSortingAndPagingCallbacks"] = value;
  615. RequireBinding ();
  616. }
  617. }
  618. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  619. [BrowsableAttribute (false)]
  620. public virtual GridViewRow FooterRow {
  621. get {
  622. if (table != null) {
  623. for (int index = table.Rows.Count - 1; index >= 0; index--) {
  624. GridViewRow row = (GridViewRow) table.Rows [index];
  625. switch (row.RowType) {
  626. case DataControlRowType.Separator:
  627. case DataControlRowType.Pager:
  628. continue;
  629. case DataControlRowType.Footer:
  630. return row;
  631. default:
  632. break;
  633. }
  634. }
  635. }
  636. return null;
  637. }
  638. }
  639. [WebCategoryAttribute ("Styles")]
  640. [PersistenceMode (PersistenceMode.InnerProperty)]
  641. [NotifyParentProperty (true)]
  642. [DefaultValue (null)]
  643. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  644. public TableItemStyle FooterStyle {
  645. get {
  646. if (footerStyle == null) {
  647. footerStyle = new TableItemStyle ();
  648. if (IsTrackingViewState)
  649. footerStyle.TrackViewState();
  650. }
  651. return footerStyle;
  652. }
  653. }
  654. [WebCategoryAttribute ("Appearance")]
  655. [DefaultValueAttribute (GridLines.Both)]
  656. public virtual GridLines GridLines {
  657. get {
  658. if (ControlStyleCreated)
  659. return ((TableStyle) ControlStyle).GridLines;
  660. return GridLines.Both;
  661. }
  662. set {
  663. ((TableStyle) ControlStyle).GridLines = value;
  664. }
  665. }
  666. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  667. [BrowsableAttribute (false)]
  668. public virtual GridViewRow HeaderRow {
  669. get {
  670. if (table != null) {
  671. for (int index = 0, total = table.Rows.Count; index < total; index++) {
  672. GridViewRow row = (GridViewRow) table.Rows [index];
  673. switch (row.RowType) {
  674. case DataControlRowType.Separator:
  675. case DataControlRowType.Pager:
  676. continue;
  677. case DataControlRowType.Header:
  678. return row;
  679. default:
  680. break;
  681. }
  682. }
  683. }
  684. return null;
  685. }
  686. }
  687. [WebCategoryAttribute ("Styles")]
  688. [PersistenceMode (PersistenceMode.InnerProperty)]
  689. [NotifyParentProperty (true)]
  690. [DefaultValue (null)]
  691. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  692. public TableItemStyle HeaderStyle {
  693. get {
  694. if (headerStyle == null) {
  695. headerStyle = new TableItemStyle ();
  696. if (IsTrackingViewState)
  697. headerStyle.TrackViewState();
  698. }
  699. return headerStyle;
  700. }
  701. }
  702. [Category ("Layout")]
  703. [DefaultValueAttribute (HorizontalAlign.NotSet)]
  704. public virtual HorizontalAlign HorizontalAlign {
  705. get {
  706. if (ControlStyleCreated)
  707. return ((TableStyle) ControlStyle).HorizontalAlign;
  708. return HorizontalAlign.NotSet;
  709. }
  710. set {
  711. ((TableStyle) ControlStyle).HorizontalAlign = value;
  712. }
  713. }
  714. [BrowsableAttribute (false)]
  715. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  716. public virtual int PageCount {
  717. get {
  718. return ViewState.GetInt ("PageCount", 0);
  719. }
  720. private set {
  721. ViewState ["PageCount"] = value;
  722. }
  723. }
  724. [WebCategoryAttribute ("Paging")]
  725. [BrowsableAttribute (true)]
  726. [DefaultValueAttribute (0)]
  727. public virtual int PageIndex {
  728. get {
  729. return pageIndex;
  730. }
  731. set {
  732. if (value == pageIndex)
  733. return;
  734. pageIndex = value;
  735. RequireBinding ();
  736. }
  737. }
  738. [DefaultValueAttribute (10)]
  739. [WebCategoryAttribute ("Paging")]
  740. public virtual int PageSize {
  741. get {
  742. object ob = ViewState ["PageSize"];
  743. if (ob != null) return (int) ob;
  744. return 10;
  745. }
  746. set {
  747. if (value == PageSize)
  748. return;
  749. ViewState ["PageSize"] = value;
  750. RequireBinding ();
  751. }
  752. }
  753. [WebCategoryAttribute ("Paging")]
  754. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Content)]
  755. [NotifyParentPropertyAttribute (true)]
  756. [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
  757. public virtual PagerSettings PagerSettings {
  758. get {
  759. if (pagerSettings == null) {
  760. pagerSettings = new PagerSettings (this);
  761. if (IsTrackingViewState)
  762. ((IStateManager)pagerSettings).TrackViewState ();
  763. }
  764. return pagerSettings;
  765. }
  766. }
  767. [WebCategoryAttribute ("Styles")]
  768. [PersistenceMode (PersistenceMode.InnerProperty)]
  769. [NotifyParentProperty (true)]
  770. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  771. public TableItemStyle PagerStyle {
  772. get {
  773. if (pagerStyle == null) {
  774. pagerStyle = new TableItemStyle ();
  775. if (IsTrackingViewState)
  776. pagerStyle.TrackViewState();
  777. }
  778. return pagerStyle;
  779. }
  780. }
  781. [DefaultValue (null)]
  782. /* DataControlPagerCell isnt specified in the docs */
  783. //[TemplateContainer (typeof(DataControlPagerCell), BindingDirection.OneWay)]
  784. [PersistenceMode (PersistenceMode.InnerProperty)]
  785. [Browsable (false)]
  786. public virtual ITemplate PagerTemplate {
  787. get { return pagerTemplate; }
  788. set { pagerTemplate = value; }
  789. }
  790. [DefaultValueAttribute ("")]
  791. [WebCategoryAttribute ("Accessibility")]
  792. // [TypeConverterAttribute (typeof(System.Web.UI.Design.DataColumnSelectionConverter)]
  793. public virtual string RowHeaderColumn {
  794. get {
  795. object ob = ViewState ["RowHeaderColumn"];
  796. if (ob != null) return (string) ob;
  797. return string.Empty;
  798. }
  799. set {
  800. if (value == RowHeaderColumn)
  801. return;
  802. ViewState ["RowHeaderColumn"] = value;
  803. RequireBinding ();
  804. }
  805. }
  806. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  807. [BrowsableAttribute (false)]
  808. public virtual GridViewRowCollection Rows {
  809. get {
  810. EnsureChildControls ();
  811. if (rows == null)
  812. rows = new GridViewRowCollection (new ArrayList ());
  813. return rows;
  814. }
  815. }
  816. [WebCategoryAttribute ("Styles")]
  817. [PersistenceMode (PersistenceMode.InnerProperty)]
  818. [NotifyParentProperty (true)]
  819. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  820. public TableItemStyle RowStyle {
  821. get {
  822. if (rowStyle == null) {
  823. rowStyle = new TableItemStyle ();
  824. if (IsTrackingViewState)
  825. rowStyle.TrackViewState();
  826. }
  827. return rowStyle;
  828. }
  829. }
  830. [BrowsableAttribute (false)]
  831. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  832. public virtual DataKey SelectedDataKey {
  833. get {
  834. if (DataKeyNames.Length == 0)
  835. 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));
  836. if (selectedIndex >= 0 && selectedIndex < DataKeys.Count) {
  837. return DataKeys [selectedIndex];
  838. } else
  839. return null;
  840. }
  841. }
  842. [BindableAttribute (true)]
  843. [DefaultValueAttribute (-1)]
  844. public virtual int SelectedIndex {
  845. get {
  846. return selectedIndex;
  847. }
  848. set {
  849. if (rows != null && selectedIndex >= 0 && selectedIndex < Rows.Count) {
  850. int oldIndex = selectedIndex;
  851. selectedIndex = -1;
  852. Rows [oldIndex].RowState = GetRowState (oldIndex);
  853. }
  854. selectedIndex = value;
  855. if (rows != null && selectedIndex >= 0 && selectedIndex < Rows.Count) {
  856. Rows [selectedIndex].RowState = GetRowState (selectedIndex);
  857. }
  858. }
  859. }
  860. [BrowsableAttribute (false)]
  861. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  862. public virtual GridViewRow SelectedRow {
  863. get {
  864. if (selectedIndex >= 0 && selectedIndex < Rows.Count) {
  865. return Rows [selectedIndex];
  866. } else
  867. return null;
  868. }
  869. }
  870. [WebCategoryAttribute ("Styles")]
  871. [PersistenceMode (PersistenceMode.InnerProperty)]
  872. [NotifyParentProperty (true)]
  873. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  874. public TableItemStyle SelectedRowStyle {
  875. get {
  876. if (selectedRowStyle == null) {
  877. selectedRowStyle = new TableItemStyle ();
  878. if (IsTrackingViewState)
  879. selectedRowStyle.TrackViewState();
  880. }
  881. return selectedRowStyle;
  882. }
  883. }
  884. [BrowsableAttribute (false)]
  885. public object SelectedValue {
  886. get {
  887. if (SelectedDataKey != null)
  888. return SelectedDataKey.Value;
  889. else
  890. return null;
  891. }
  892. }
  893. [WebCategoryAttribute ("Appearance")]
  894. [DefaultValueAttribute (false)]
  895. public virtual bool ShowFooter {
  896. get {
  897. object ob = ViewState ["ShowFooter"];
  898. if (ob != null) return (bool) ob;
  899. return false;
  900. }
  901. set {
  902. if (value == ShowFooter)
  903. return;
  904. ViewState ["ShowFooter"] = value;
  905. RequireBinding ();
  906. }
  907. }
  908. [WebCategoryAttribute ("Appearance")]
  909. [DefaultValueAttribute (true)]
  910. public virtual bool ShowHeader {
  911. get {
  912. object ob = ViewState ["ShowHeader"];
  913. if (ob != null) return (bool) ob;
  914. return true;
  915. }
  916. set {
  917. if (value == ShowHeader)
  918. return;
  919. ViewState ["ShowHeader"] = value;
  920. RequireBinding ();
  921. }
  922. }
  923. [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
  924. [BrowsableAttribute (false)]
  925. [DefaultValueAttribute (SortDirection.Ascending)]
  926. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  927. public virtual SortDirection SortDirection {
  928. get { return sortDirection; }
  929. private set {
  930. if (sortDirection == value)
  931. return;
  932. sortDirection = value;
  933. RequireBinding ();
  934. }
  935. }
  936. [BrowsableAttribute (false)]
  937. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  938. public virtual string SortExpression {
  939. get {
  940. if (sortExpression == null)
  941. return String.Empty;
  942. return sortExpression;
  943. }
  944. private set {
  945. if (sortExpression == value)
  946. return;
  947. sortExpression = value;
  948. RequireBinding ();
  949. }
  950. }
  951. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  952. [BrowsableAttribute (false)]
  953. public virtual GridViewRow TopPagerRow {
  954. get {
  955. EnsureDataBound ();
  956. return topPagerRow;
  957. }
  958. }
  959. [WebCategoryAttribute ("Accessibility")]
  960. [DefaultValueAttribute (true)]
  961. public virtual bool UseAccessibleHeader {
  962. get {
  963. object ob = ViewState ["UseAccessibleHeader"];
  964. if (ob != null) return (bool) ob;
  965. return true;
  966. }
  967. set {
  968. if (value == UseAccessibleHeader)
  969. return;
  970. ViewState ["UseAccessibleHeader"] = value;
  971. RequireBinding ();
  972. }
  973. }
  974. public virtual bool IsBindableType (Type type)
  975. {
  976. return type.IsPrimitive || type == typeof (string) || type == typeof (decimal) || type == typeof (DateTime) || type == typeof (Guid);
  977. }
  978. // MSDN: The CreateDataSourceSelectArguments method is a helper method called by
  979. // the GridView control to create the DataSourceSelectArguments object that
  980. // contains the arguments passed to the data source. In this implementation,
  981. // the DataSourceSelectArguments object contains the arguments for paging operations.
  982. protected override DataSourceSelectArguments CreateDataSourceSelectArguments ()
  983. {
  984. DataSourceSelectArguments arg = DataSourceSelectArguments.Empty;
  985. DataSourceView view= GetData();
  986. if (AllowPaging && view.CanPage) {
  987. arg.StartRowIndex = PageIndex * PageSize;
  988. if (view.CanRetrieveTotalRowCount) {
  989. arg.RetrieveTotalRowCount = true;
  990. arg.MaximumRows = PageSize;
  991. }
  992. else {
  993. arg.MaximumRows = -1;
  994. }
  995. }
  996. if (IsBoundUsingDataSourceID && !String.IsNullOrEmpty (sortExpression)) {
  997. if (sortDirection == SortDirection.Ascending)
  998. arg.SortExpression = sortExpression;
  999. else
  1000. arg.SortExpression = sortExpression + " DESC";
  1001. }
  1002. return arg;
  1003. }
  1004. protected virtual ICollection CreateColumns (PagedDataSource dataSource, bool useDataSource)
  1005. {
  1006. bool autoGenerate = AutoGenerateColumns;
  1007. if (autoGenerate) {
  1008. IAutoFieldGenerator fieldGenerator = ColumnsGenerator;
  1009. if (fieldGenerator != null)
  1010. return fieldGenerator.GenerateFields (this);
  1011. }
  1012. ArrayList fields = new ArrayList ();
  1013. if (AutoGenerateEditButton || AutoGenerateDeleteButton || AutoGenerateSelectButton) {
  1014. CommandField field = new CommandField ();
  1015. field.ShowEditButton = AutoGenerateEditButton;
  1016. field.ShowDeleteButton = AutoGenerateDeleteButton;
  1017. field.ShowSelectButton = AutoGenerateSelectButton;
  1018. fields.Add (field);
  1019. }
  1020. fields.AddRange (Columns);
  1021. if (autoGenerate) {
  1022. if (useDataSource)
  1023. autoFieldProperties = CreateAutoFieldProperties (dataSource);
  1024. if (autoFieldProperties != null) {
  1025. foreach (AutoGeneratedFieldProperties props in autoFieldProperties)
  1026. fields.Add (CreateAutoGeneratedColumn (props));
  1027. }
  1028. }
  1029. return fields;
  1030. }
  1031. protected virtual AutoGeneratedField CreateAutoGeneratedColumn (AutoGeneratedFieldProperties fieldProperties)
  1032. {
  1033. return new AutoGeneratedField (fieldProperties);
  1034. }
  1035. AutoGeneratedFieldProperties[] CreateAutoFieldProperties (PagedDataSource source)
  1036. {
  1037. if(source == null) return null;
  1038. PropertyDescriptorCollection props = source.GetItemProperties (new PropertyDescriptor[0]);
  1039. Type prop_type;
  1040. ArrayList retVal = new ArrayList();
  1041. if (props == null)
  1042. {
  1043. object fitem = null;
  1044. prop_type = null;
  1045. PropertyInfo prop_item = source.DataSource.GetType().GetProperty("Item",
  1046. BindingFlags.Instance | BindingFlags.Static |
  1047. BindingFlags.Public, null, null,
  1048. new Type[] { typeof(int) }, null);
  1049. if (prop_item != null) {
  1050. prop_type = prop_item.PropertyType;
  1051. }
  1052. if (prop_type == null || prop_type == typeof(object)) {
  1053. IEnumerator en = source.GetEnumerator();
  1054. if (en != null && en.MoveNext ()) {
  1055. fitem = en.Current;
  1056. _dataEnumerator = en;
  1057. }
  1058. if (fitem != null)
  1059. prop_type = fitem.GetType();
  1060. }
  1061. if (fitem != null && fitem is ICustomTypeDescriptor) {
  1062. props = TypeDescriptor.GetProperties(fitem);
  1063. } else if (prop_type != null) {
  1064. if (IsBindableType (prop_type)) {
  1065. AutoGeneratedFieldProperties field = new AutoGeneratedFieldProperties ();
  1066. ((IStateManager)field).TrackViewState();
  1067. field.Name = "Item";
  1068. field.DataField = BoundField.ThisExpression;
  1069. field.Type = prop_type;
  1070. retVal.Add (field);
  1071. } else {
  1072. props = TypeDescriptor.GetProperties (prop_type);
  1073. }
  1074. }
  1075. }
  1076. if (props != null && props.Count > 0)
  1077. {
  1078. foreach (PropertyDescriptor current in props) {
  1079. if (IsBindableType (current.PropertyType)) {
  1080. AutoGeneratedFieldProperties field = new AutoGeneratedFieldProperties ();
  1081. ((IStateManager)field).TrackViewState();
  1082. field.Name = current.Name;
  1083. field.DataField = current.Name;
  1084. for (int i = 0; i < DataKeyNames.Length; i++) {
  1085. if (string.Compare (DataKeyNames [i], current.Name, StringComparison.InvariantCultureIgnoreCase) == 0) {
  1086. field.IsReadOnly = true;
  1087. break;
  1088. }
  1089. }
  1090. field.Type = current.PropertyType;
  1091. retVal.Add (field);
  1092. }
  1093. }
  1094. }
  1095. if (retVal.Count > 0)
  1096. return (AutoGeneratedFieldProperties[]) retVal.ToArray (typeof(AutoGeneratedFieldProperties));
  1097. else
  1098. return new AutoGeneratedFieldProperties [0];
  1099. }
  1100. protected virtual GridViewRow CreateRow (int rowIndex, int dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState)
  1101. {
  1102. GridViewRow row = new GridViewRow (rowIndex, dataSourceIndex, rowType, rowState);
  1103. return row;
  1104. }
  1105. void RequireBinding ()
  1106. {
  1107. if (Initialized) {
  1108. RequiresDataBinding = true;
  1109. }
  1110. }
  1111. protected virtual Table CreateChildTable ()
  1112. {
  1113. return new ContainedTable (this);
  1114. }
  1115. protected override int CreateChildControls (IEnumerable data, bool dataBinding)
  1116. {
  1117. // clear GridView
  1118. Controls.Clear ();
  1119. table = null;
  1120. rows = null;
  1121. if (data == null) {
  1122. return 0;
  1123. }
  1124. PagedDataSource dataSource;
  1125. if (dataBinding) {
  1126. DataSourceView view = GetData ();
  1127. dataSource = new PagedDataSource ();
  1128. dataSource.DataSource = data;
  1129. if (AllowPaging) {
  1130. dataSource.AllowPaging = true;
  1131. dataSource.PageSize = PageSize;
  1132. if (view.CanPage) {
  1133. dataSource.AllowServerPaging = true;
  1134. if (SelectArguments.RetrieveTotalRowCount)
  1135. dataSource.VirtualCount = SelectArguments.TotalRowCount;
  1136. }
  1137. if (PageIndex >= dataSource.PageCount)
  1138. pageIndex = dataSource.PageCount - 1;
  1139. dataSource.CurrentPageIndex = PageIndex;
  1140. }
  1141. PageCount = dataSource.PageCount;
  1142. }
  1143. else
  1144. {
  1145. dataSource = new PagedDataSource ();
  1146. dataSource.DataSource = data;
  1147. if (AllowPaging) {
  1148. dataSource.AllowPaging = true;
  1149. dataSource.PageSize = PageSize;
  1150. dataSource.CurrentPageIndex = PageIndex;
  1151. }
  1152. }
  1153. bool createPager = AllowPaging && (PageCount >= 1) && PagerSettings.Visible;
  1154. ArrayList list = new ArrayList ();
  1155. // Creates the set of fields to show
  1156. _dataEnumerator = null;
  1157. ICollection fieldCollection = CreateColumns (dataSource, dataBinding);
  1158. int fieldCount = fieldCollection.Count;
  1159. DataControlField dcf;
  1160. DataControlField[] fields = new DataControlField [fieldCount];
  1161. fieldCollection.CopyTo (fields, 0);
  1162. for (int i = 0; i < fieldCount; i++) {
  1163. dcf = fields [i];
  1164. dcf.Initialize (AllowSorting, this);
  1165. if (EnableSortingAndPagingCallbacks)
  1166. dcf.ValidateSupportsCallback ();
  1167. }
  1168. bool skip_first = false;
  1169. IEnumerator enumerator;
  1170. if (_dataEnumerator != null) {
  1171. // replaced when creating bound columns
  1172. enumerator = _dataEnumerator;
  1173. skip_first = true;
  1174. }
  1175. else {
  1176. enumerator = dataSource.GetEnumerator ();
  1177. }
  1178. // Main table creation
  1179. while (skip_first || enumerator.MoveNext ()) {
  1180. skip_first = false;
  1181. object obj = enumerator.Current;
  1182. if (list.Count == 0) {
  1183. if (createPager && (PagerSettings.Position == PagerPosition.Top || PagerSettings.Position == PagerPosition.TopAndBottom)) {
  1184. topPagerRow = CreatePagerRow (fieldCount, dataSource);
  1185. OnRowCreated (new GridViewRowEventArgs (topPagerRow));
  1186. ContainedTable.Rows.Add (topPagerRow);
  1187. if (dataBinding) {
  1188. topPagerRow.DataBind ();
  1189. OnRowDataBound (new GridViewRowEventArgs (topPagerRow));
  1190. }
  1191. if (PageCount == 1)
  1192. topPagerRow.Visible = false;
  1193. }
  1194. GridViewRow headerRow = CreateRow (-1, -1, DataControlRowType.Header, DataControlRowState.Normal);
  1195. InitializeRow (headerRow, fields);
  1196. OnRowCreated (new GridViewRowEventArgs (headerRow));
  1197. ContainedTable.Rows.Add (headerRow);
  1198. if (dataBinding) {
  1199. headerRow.DataBind ();
  1200. OnRowDataBound (new GridViewRowEventArgs (headerRow));
  1201. }
  1202. }
  1203. DataControlRowState rstate = GetRowState (list.Count);
  1204. GridViewRow row = CreateRow (list.Count, list.Count, DataControlRowType.DataRow, rstate);
  1205. row.DataItem = obj;
  1206. list.Add (row);
  1207. InitializeRow (row, fields);
  1208. OnRowCreated (new GridViewRowEventArgs (row));
  1209. ContainedTable.Rows.Add (row);
  1210. if (dataBinding) {
  1211. row.DataBind ();
  1212. if (EditIndex == row.RowIndex)
  1213. oldEditValues = new DataKey (GetRowValues (row, true, true));
  1214. DataKeyArrayList.Add (new DataKey (CreateRowDataKey (row), DataKeyNames));
  1215. OnRowDataBound (new GridViewRowEventArgs (row));
  1216. }
  1217. }
  1218. if (list.Count == 0) {
  1219. GridViewRow emptyRow = CreateEmptyrRow (fieldCount);
  1220. if (emptyRow != null) {
  1221. OnRowCreated (new GridViewRowEventArgs (emptyRow));
  1222. ContainedTable.Rows.Add (emptyRow);
  1223. if (dataBinding) {
  1224. emptyRow.DataBind ();
  1225. OnRowDataBound (new GridViewRowEventArgs (emptyRow));
  1226. }
  1227. }
  1228. return 0;
  1229. }
  1230. else {
  1231. GridViewRow footerRow = CreateRow (-1, -1, DataControlRowType.Footer, DataControlRowState.Normal);
  1232. InitializeRow (footerRow, fields);
  1233. OnRowCreated (new GridViewRowEventArgs (footerRow));
  1234. ContainedTable.Rows.Add (footerRow);
  1235. if (dataBinding) {
  1236. footerRow.DataBind ();
  1237. OnRowDataBound (new GridViewRowEventArgs (footerRow));
  1238. }
  1239. if (createPager && (PagerSettings.Position == PagerPosition.Bottom || PagerSettings.Position == PagerPosition.TopAndBottom)) {
  1240. bottomPagerRow = CreatePagerRow (fieldCount, dataSource);
  1241. OnRowCreated (new GridViewRowEventArgs (bottomPagerRow));
  1242. ContainedTable.Rows.Add (bottomPagerRow);
  1243. if (dataBinding) {
  1244. bottomPagerRow.DataBind ();
  1245. OnRowDataBound (new GridViewRowEventArgs (bottomPagerRow));
  1246. }
  1247. if (PageCount == 1)
  1248. bottomPagerRow.Visible = false;
  1249. }
  1250. }
  1251. rows = new GridViewRowCollection (list);
  1252. if (!dataBinding)
  1253. return -1;
  1254. if (AllowPaging)
  1255. return dataSource.DataSourceCount;
  1256. else
  1257. return list.Count;
  1258. }
  1259. Table ContainedTable {
  1260. get {
  1261. if (table == null) {
  1262. table = CreateChildTable ();
  1263. Controls.Add (table);
  1264. }
  1265. return table;
  1266. }
  1267. }
  1268. protected override Style CreateControlStyle ()
  1269. {
  1270. TableStyle style = new TableStyle (ViewState);
  1271. style.GridLines = GridLines.Both;
  1272. style.CellSpacing = 0;
  1273. return style;
  1274. }
  1275. DataControlRowState GetRowState (int index)
  1276. {
  1277. DataControlRowState rstate = (index % 2) == 0 ? DataControlRowState.Normal : DataControlRowState.Alternate;
  1278. if (index == SelectedIndex) rstate |= DataControlRowState.Selected;
  1279. if (index == EditIndex) rstate |= DataControlRowState.Edit;
  1280. return rstate;
  1281. }
  1282. GridViewRow CreatePagerRow (int fieldCount, PagedDataSource dataSource)
  1283. {
  1284. GridViewRow row = CreateRow (-1, -1, DataControlRowType.Pager, DataControlRowState.Normal);
  1285. InitializePager (row, fieldCount, dataSource);
  1286. return row;
  1287. }
  1288. protected virtual void InitializePager (GridViewRow row, int columnSpan, PagedDataSource dataSource)
  1289. {
  1290. TableCell cell = new TableCell ();
  1291. if (columnSpan > 1)
  1292. cell.ColumnSpan = columnSpan;
  1293. if (pagerTemplate != null)
  1294. pagerTemplate.InstantiateIn (cell);
  1295. else
  1296. cell.Controls.Add (PagerSettings.CreatePagerControl (dataSource.CurrentPageIndex, dataSource.PageCount));
  1297. row.Cells.Add (cell);
  1298. }
  1299. GridViewRow CreateEmptyrRow (int fieldCount)
  1300. {
  1301. if (emptyDataTemplate == null && String.IsNullOrEmpty (EmptyDataText))
  1302. return null;
  1303. GridViewRow row = CreateRow (-1, -1, DataControlRowType.EmptyDataRow, DataControlRowState.Normal);
  1304. TableCell cell = new TableCell ();
  1305. cell.ColumnSpan = fieldCount;
  1306. if (emptyDataTemplate != null)
  1307. emptyDataTemplate.InstantiateIn (cell);
  1308. else
  1309. cell.Text = EmptyDataText;
  1310. row.Cells.Add (cell);
  1311. return row;
  1312. }
  1313. protected virtual void InitializeRow (GridViewRow row, DataControlField[] fields)
  1314. {
  1315. DataControlCellType ctype;
  1316. bool accessibleHeader = false;
  1317. switch (row.RowType) {
  1318. case DataControlRowType.Header:
  1319. ctype = DataControlCellType.Header;
  1320. accessibleHeader = UseAccessibleHeader;
  1321. break;
  1322. case DataControlRowType.Footer:
  1323. ctype = DataControlCellType.Footer;
  1324. break;
  1325. default:
  1326. ctype = DataControlCellType.DataCell;
  1327. break;
  1328. }
  1329. for (int n=0; n<fields.Length; n++) {
  1330. DataControlField field = fields [n];
  1331. DataControlFieldCell cell;
  1332. if (((field is BoundField) && ((BoundField)field).DataField == RowHeaderColumn) || accessibleHeader)
  1333. cell = new DataControlFieldHeaderCell (field, accessibleHeader ? TableHeaderScope.Column : TableHeaderScope.Row);
  1334. else
  1335. cell = new DataControlFieldCell (field);
  1336. row.Cells.Add (cell);
  1337. field.InitializeCell (cell, ctype, row.RowState, row.RowIndex);
  1338. }
  1339. }
  1340. IOrderedDictionary CreateRowDataKey (GridViewRow row)
  1341. {
  1342. if (cachedKeyProperties == null) {
  1343. PropertyDescriptorCollection props = TypeDescriptor.GetProperties (row.DataItem);
  1344. cachedKeyProperties = new PropertyDescriptor [DataKeyNames.Length];
  1345. for (int n=0; n<DataKeyNames.Length; n++) {
  1346. PropertyDescriptor p = props.Find (DataKeyNames [n], true);
  1347. if (p == null)
  1348. throw new InvalidOperationException ("Property '" + DataKeyNames [n] + "' not found in object of type " + row.DataItem.GetType ());
  1349. cachedKeyProperties [n] = p;
  1350. }
  1351. }
  1352. OrderedDictionary dic = new OrderedDictionary ();
  1353. foreach (PropertyDescriptor p in cachedKeyProperties)
  1354. dic [p.Name] = p.GetValue (row.DataItem);
  1355. return dic;
  1356. }
  1357. IOrderedDictionary GetRowValues (GridViewRow row, bool includeReadOnlyFields, bool includePrimaryKey)
  1358. {
  1359. OrderedDictionary dic = new OrderedDictionary ();
  1360. ExtractRowValues (dic, row, includeReadOnlyFields, includePrimaryKey);
  1361. return dic;
  1362. }
  1363. protected virtual void ExtractRowValues (IOrderedDictionary fieldValues, GridViewRow row, bool includeReadOnlyFields, bool includePrimaryKey)
  1364. {
  1365. DataControlField field;
  1366. foreach (TableCell cell in row.Cells) {
  1367. DataControlFieldCell c = cell as DataControlFieldCell;
  1368. if (c == null)
  1369. continue;
  1370. field = c.ContainingField;
  1371. if (field != null && !field.Visible)
  1372. continue;
  1373. c.ContainingField.ExtractValuesFromCell (fieldValues, c, row.RowState, includeReadOnlyFields);
  1374. }
  1375. if (!includePrimaryKey && DataKeyNames != null)
  1376. foreach (string key in DataKeyNames)
  1377. fieldValues.Remove (key);
  1378. }
  1379. protected override HtmlTextWriterTag TagKey {
  1380. get {
  1381. if (EnableSortingAndPagingCallbacks)
  1382. return HtmlTextWriterTag.Div;
  1383. else
  1384. return HtmlTextWriterTag.Table;
  1385. }
  1386. }
  1387. public sealed override void DataBind ()
  1388. {
  1389. DataKeyArrayList.Clear ();
  1390. cachedKeyProperties = null;
  1391. base.DataBind ();
  1392. keys = new DataKeyArray (DataKeyArrayList);
  1393. }
  1394. protected internal override void PerformDataBinding (IEnumerable data)
  1395. {
  1396. base.PerformDataBinding (data);
  1397. }
  1398. protected internal virtual void PrepareControlHierarchy ()
  1399. {
  1400. if (table == null)
  1401. return;
  1402. table.Caption = Caption;
  1403. table.CaptionAlign = CaptionAlign;
  1404. table.CopyBaseAttributes (this);
  1405. foreach (GridViewRow row in table.Rows) {
  1406. switch (row.RowType) {
  1407. case DataControlRowType.Header:
  1408. if (headerStyle != null && !headerStyle.IsEmpty)
  1409. row.ControlStyle.MergeWith(headerStyle);
  1410. row.Visible = ShowHeader;
  1411. break;
  1412. case DataControlRowType.Footer:
  1413. if (footerStyle != null && !footerStyle.IsEmpty)
  1414. row.ControlStyle.MergeWith (footerStyle);
  1415. row.Visible = ShowFooter;
  1416. break;
  1417. case DataControlRowType.Pager:
  1418. if (pagerStyle != null && !pagerStyle.IsEmpty)
  1419. row.ControlStyle.MergeWith (pagerStyle);
  1420. break;
  1421. case DataControlRowType.EmptyDataRow:
  1422. if (emptyDataRowStyle != null && !emptyDataRowStyle.IsEmpty)
  1423. row.ControlStyle.MergeWith (emptyDataRowStyle);
  1424. break;
  1425. case DataControlRowType.DataRow:
  1426. if ((row.RowState & DataControlRowState.Edit) != 0 && editRowStyle != null && !editRowStyle.IsEmpty)
  1427. row.ControlStyle.MergeWith (editRowStyle);
  1428. if ((row.RowState & DataControlRowState.Selected) != 0 && selectedRowStyle != null && !selectedRowStyle.IsEmpty)
  1429. row.ControlStyle.MergeWith (selectedRowStyle);
  1430. if ((row.RowState & DataControlRowState.Alternate) != 0 && alternatingRowStyle != null && !alternatingRowStyle.IsEmpty)
  1431. row.ControlStyle.MergeWith (alternatingRowStyle);
  1432. if (rowStyle != null && !rowStyle.IsEmpty)
  1433. row.ControlStyle.MergeWith (rowStyle);
  1434. break;
  1435. default:
  1436. break;
  1437. }
  1438. foreach (TableCell cell in row.Cells) {
  1439. DataControlFieldCell fcell = cell as DataControlFieldCell;
  1440. if (fcell != null) {
  1441. DataControlField field = fcell.ContainingField;
  1442. if (field == null)
  1443. continue;
  1444. if (!field.Visible) {
  1445. cell.Visible = false;
  1446. continue;
  1447. }
  1448. switch (row.RowType) {
  1449. case DataControlRowType.Header:
  1450. if (field.HeaderStyleCreated && !field.HeaderStyle.IsEmpty)
  1451. cell.ControlStyle.MergeWith (field.HeaderStyle);
  1452. break;
  1453. case DataControlRowType.Footer:
  1454. if (field.FooterStyleCreated && !field.FooterStyle.IsEmpty)
  1455. cell.ControlStyle.MergeWith (field.FooterStyle);
  1456. break;
  1457. default:
  1458. if (field.ControlStyleCreated && !field.ControlStyle.IsEmpty)
  1459. foreach (Control c in cell.Controls) {
  1460. WebControl wc = c as WebControl;
  1461. if (wc != null)
  1462. wc.ControlStyle.MergeWith (field.ControlStyle);
  1463. }
  1464. if (field.ItemStyleCreated && !field.ItemStyle.IsEmpty)
  1465. cell.ControlStyle.MergeWith (field.ItemStyle);
  1466. break;
  1467. }
  1468. }
  1469. }
  1470. }
  1471. }
  1472. protected internal override void OnInit (EventArgs e)
  1473. {
  1474. Page.RegisterRequiresControlState (this);
  1475. base.OnInit (e);
  1476. }
  1477. void OnFieldsChanged (object sender, EventArgs args)
  1478. {
  1479. RequireBinding ();
  1480. }
  1481. protected override void OnDataPropertyChanged ()
  1482. {
  1483. base.OnDataPropertyChanged ();
  1484. RequireBinding ();
  1485. }
  1486. protected override void OnDataSourceViewChanged (object sender, EventArgs e)
  1487. {
  1488. base.OnDataSourceViewChanged (sender, e);
  1489. RequireBinding ();
  1490. }
  1491. protected override bool OnBubbleEvent (object source, EventArgs e)
  1492. {
  1493. GridViewCommandEventArgs args = e as GridViewCommandEventArgs;
  1494. if (args != null) {
  1495. bool causesValidation = false;
  1496. IButtonControl button = args.CommandSource as IButtonControl;
  1497. if (button != null && button.CausesValidation) {
  1498. Page.Validate (button.ValidationGroup);
  1499. causesValidation = true;
  1500. }
  1501. OnRowCommand (args);
  1502. string param = args.CommandArgument as string;
  1503. if (param == null || param.Length == 0) {
  1504. GridViewRow row = args.Row;
  1505. if (row != null)
  1506. param = row.RowIndex.ToString();
  1507. }
  1508. ProcessEvent (args.CommandName, param, causesValidation);
  1509. return true;
  1510. }
  1511. return base.OnBubbleEvent (source, e);
  1512. }
  1513. void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
  1514. {
  1515. ValidateEvent (UniqueID, eventArgument);
  1516. RaisePostBackEvent (eventArgument);
  1517. }
  1518. // This is prolly obsolete
  1519. protected virtual void RaisePostBackEvent (string eventArgument)
  1520. {
  1521. GridViewCommandEventArgs args;
  1522. int i = eventArgument.IndexOf ('$');
  1523. if (i != -1)
  1524. args = new GridViewCommandEventArgs (this, new CommandEventArgs (eventArgument.Substring (0, i), eventArgument.Substring (i + 1)));
  1525. else
  1526. args = new GridViewCommandEventArgs (this, new CommandEventArgs (eventArgument, null));
  1527. OnRowCommand (args);
  1528. ProcessEvent (args.CommandName, (string) args.CommandArgument, false);
  1529. }
  1530. void ProcessEvent (string eventName, string param, bool causesValidation)
  1531. {
  1532. switch (eventName)
  1533. {
  1534. case DataControlCommands.PageCommandName:
  1535. int newIndex = -1;
  1536. switch (param) {
  1537. case DataControlCommands.FirstPageCommandArgument:
  1538. newIndex = 0;
  1539. break;
  1540. case DataControlCommands.LastPageCommandArgument:
  1541. newIndex = PageCount - 1;
  1542. break;
  1543. case DataControlCommands.NextPageCommandArgument:
  1544. newIndex = PageIndex + 1;
  1545. break;
  1546. case DataControlCommands.PreviousPageCommandArgument:
  1547. newIndex = PageIndex - 1;
  1548. break;
  1549. default:
  1550. int paramIndex = 0;
  1551. int.TryParse (param, out paramIndex);
  1552. newIndex = paramIndex - 1;
  1553. break;
  1554. }
  1555. ShowPage (newIndex);
  1556. break;
  1557. case DataControlCommands.FirstPageCommandArgument:
  1558. ShowPage (0);
  1559. break;
  1560. case DataControlCommands.LastPageCommandArgument:
  1561. ShowPage (PageCount - 1);
  1562. break;
  1563. case DataControlCommands.NextPageCommandArgument:
  1564. if (PageIndex < PageCount - 1)
  1565. ShowPage (PageIndex + 1);
  1566. break;
  1567. case DataControlCommands.PreviousPageCommandArgument:
  1568. if (PageIndex > 0)
  1569. ShowPage (PageIndex - 1);
  1570. break;
  1571. case DataControlCommands.SelectCommandName:
  1572. SelectRow (int.Parse (param));
  1573. break;
  1574. case DataControlCommands.EditCommandName:
  1575. EditRow (int.Parse (param));
  1576. break;
  1577. case DataControlCommands.UpdateCommandName:
  1578. int editIndex = int.Parse (param);
  1579. UpdateRow (Rows [editIndex], editIndex, causesValidation);
  1580. break;
  1581. case DataControlCommands.CancelCommandName:
  1582. CancelEdit ();
  1583. break;
  1584. case DataControlCommands.DeleteCommandName:
  1585. DeleteRow (int.Parse (param));
  1586. break;
  1587. case DataControlCommands.SortCommandName:
  1588. Sort (param);
  1589. break;
  1590. }
  1591. }
  1592. void Sort (string newSortExpression)
  1593. {
  1594. SortDirection newDirection = SortDirection.Ascending;
  1595. if (sortExpression == newSortExpression && sortDirection == SortDirection.Ascending)
  1596. newDirection = SortDirection.Descending;
  1597. Sort (newSortExpression, newDirection);
  1598. }
  1599. public virtual void Sort (string newSortExpression, SortDirection newSortDirection)
  1600. {
  1601. GridViewSortEventArgs args = new GridViewSortEventArgs (newSortExpression, newSortDirection);
  1602. OnSorting (args);
  1603. if (args.Cancel) return;
  1604. if (IsBoundUsingDataSourceID) {
  1605. EditIndex = -1;
  1606. PageIndex = 0;
  1607. SortExpression = args.SortExpression;
  1608. SortDirection = args.SortDirection;
  1609. }
  1610. OnSorted (EventArgs.Empty);
  1611. }
  1612. void SelectRow (int index)
  1613. {
  1614. GridViewSelectEventArgs args = new GridViewSelectEventArgs (index);
  1615. OnSelectedIndexChanging (args);
  1616. if (!args.Cancel) {
  1617. RequireBinding ();
  1618. SelectedIndex = args.NewSelectedIndex;
  1619. OnSelectedIndexChanged (EventArgs.Empty);
  1620. }
  1621. }
  1622. void ShowPage (int newIndex)
  1623. {
  1624. GridViewPageEventArgs args = new GridViewPageEventArgs (newIndex);
  1625. OnPageIndexChanging (args);
  1626. if (args.Cancel || !IsBoundUsingDataSourceID)
  1627. return;
  1628. EndRowEdit ();
  1629. PageIndex = args.NewPageIndex;
  1630. OnPageIndexChanged (EventArgs.Empty);
  1631. }
  1632. void EditRow (int index)
  1633. {
  1634. GridViewEditEventArgs args = new GridViewEditEventArgs (index);
  1635. OnRowEditing (args);
  1636. if (args.Cancel || !IsBoundUsingDataSourceID)
  1637. return;
  1638. EditIndex = args.NewEditIndex;
  1639. }
  1640. void CancelEdit ()
  1641. {
  1642. GridViewCancelEditEventArgs args = new GridViewCancelEditEventArgs (EditIndex);
  1643. OnRowCancelingEdit (args);
  1644. if (args.Cancel || !IsBoundUsingDataSourceID)
  1645. return;
  1646. EndRowEdit ();
  1647. }
  1648. [MonoTODO ("Support two-way binding expressions")]
  1649. public virtual void UpdateRow (int rowIndex, bool causesValidation)
  1650. {
  1651. if (rowIndex != EditIndex) throw new NotSupportedException ();
  1652. GridViewRow row = Rows [rowIndex];
  1653. UpdateRow (row, rowIndex, causesValidation);
  1654. }
  1655. void UpdateRow (GridViewRow row, int rowIndex, bool causesValidation)
  1656. {
  1657. if (causesValidation && Page != null && !Page.IsValid)
  1658. return;
  1659. currentEditOldValues = CopyOrderedDictionary (OldEditValues.Values);
  1660. currentEditRowKeys = CopyOrderedDictionary (DataKeys [rowIndex].Values);
  1661. currentEditNewValues = GetRowValues (row, false, false);
  1662. GridViewUpdateEventArgs args = new GridViewUpdateEventArgs (rowIndex, currentEditRowKeys, currentEditOldValues, currentEditNewValues);
  1663. OnRowUpdating (args);
  1664. if (args.Cancel || !IsBoundUsingDataSourceID)
  1665. return;
  1666. DataSourceView view = GetData ();
  1667. if (view == null)
  1668. throw new HttpException ("The DataSourceView associated to data bound control was null");
  1669. view.Update (currentEditRowKeys, currentEditNewValues, currentEditOldValues, new DataSourceViewOperationCallback (UpdateCallback));
  1670. }
  1671. static IOrderedDictionary CopyOrderedDictionary (IOrderedDictionary sourceDic) {
  1672. OrderedDictionary copyDic = new OrderedDictionary ();
  1673. foreach (object key in sourceDic.Keys) {
  1674. copyDic.Add (key, sourceDic [key]);
  1675. }
  1676. return copyDic;
  1677. }
  1678. bool UpdateCallback (int recordsAffected, Exception exception)
  1679. {
  1680. GridViewUpdatedEventArgs dargs = new GridViewUpdatedEventArgs (recordsAffected, exception, currentEditRowKeys, currentEditOldValues, currentEditNewValues);
  1681. OnRowUpdated (dargs);
  1682. if (!dargs.KeepInEditMode)
  1683. EndRowEdit ();
  1684. return dargs.ExceptionHandled;
  1685. }
  1686. public virtual void DeleteRow (int rowIndex)
  1687. {
  1688. GridViewRow row = Rows [rowIndex];
  1689. currentEditRowKeys = CopyOrderedDictionary (DataKeys [rowIndex].Values);
  1690. currentEditNewValues = GetRowValues (row, true, true);
  1691. GridViewDeleteEventArgs args = new GridViewDeleteEventArgs (rowIndex, currentEditRowKeys, currentEditNewValues);
  1692. OnRowDeleting (args);
  1693. if (args.Cancel || !IsBoundUsingDataSourceID)
  1694. return;
  1695. RequireBinding ();
  1696. DataSourceView view = GetData ();
  1697. if (view != null)
  1698. view.Delete (currentEditRowKeys, currentEditNewValues, new DataSourceViewOperationCallback (DeleteCallback));
  1699. else {
  1700. GridViewDeletedEventArgs dargs = new GridViewDeletedEventArgs (0, null, currentEditRowKeys, currentEditNewValues);
  1701. OnRowDeleted (dargs);
  1702. }
  1703. }
  1704. bool DeleteCallback (int recordsAffected, Exception exception)
  1705. {
  1706. GridViewDeletedEventArgs dargs = new GridViewDeletedEventArgs (recordsAffected, exception, currentEditRowKeys, currentEditNewValues);
  1707. OnRowDeleted (dargs);
  1708. return dargs.ExceptionHandled;
  1709. }
  1710. void EndRowEdit ()
  1711. {
  1712. EditIndex = -1;
  1713. oldEditValues = new DataKey (new OrderedDictionary ());
  1714. currentEditRowKeys = null;
  1715. currentEditOldValues = null;
  1716. currentEditNewValues = null;
  1717. }
  1718. protected internal override void LoadControlState (object ob)
  1719. {
  1720. if (ob == null) return;
  1721. object[] state = (object[]) ob;
  1722. base.LoadControlState (state[0]);
  1723. pageIndex = (int) state[1];
  1724. selectedIndex = (int) state[2];
  1725. editIndex = (int) state[3];
  1726. sortExpression = (string) state[4];
  1727. sortDirection = (SortDirection) state[5];
  1728. DataKeyNames = (string []) state [6];
  1729. if (state [7] != null)
  1730. LoadDataKeyArrayState ((object []) state [7]);
  1731. if (state [8] != null)
  1732. ((IStateManager) OldEditValues).LoadViewState (state [8]);
  1733. }
  1734. protected internal override object SaveControlState ()
  1735. {
  1736. if (EnableSortingAndPagingCallbacks) {
  1737. Page.ClientScript.RegisterHiddenField (ClientID + "_Page", PageIndex.ToString ());
  1738. Page.ClientScript.RegisterHiddenField (ClientID + "_SortExpression", SortExpression);
  1739. Page.ClientScript.RegisterHiddenField (ClientID + "_SortDirection", ((int)SortDirection).ToString());
  1740. }
  1741. object bstate = base.SaveControlState ();
  1742. return new object [] {
  1743. bstate,
  1744. pageIndex,
  1745. selectedIndex,
  1746. editIndex,
  1747. sortExpression,
  1748. sortDirection,
  1749. DataKeyNames,
  1750. SaveDataKeyArrayState (),
  1751. (oldEditValues == null ? null : ((IStateManager)oldEditValues).SaveViewState ())
  1752. };
  1753. }
  1754. object [] SaveDataKeyArrayState ()
  1755. {
  1756. if (keys == null)
  1757. return null;
  1758. object [] state = new object [keys.Count];
  1759. for (int i = 0; i < keys.Count; i++) {
  1760. state [i] = ((IStateManager) keys [i]).SaveViewState ();
  1761. }
  1762. return state;
  1763. }
  1764. void LoadDataKeyArrayState (object [] state)
  1765. {
  1766. for (int i = 0; i < state.Length; i++) {
  1767. DataKey dataKey = new DataKey (new OrderedDictionary (DataKeyNames.Length), DataKeyNames);
  1768. ((IStateManager) dataKey).LoadViewState (state [i]);
  1769. DataKeyArrayList.Add (dataKey);
  1770. }
  1771. keys = new DataKeyArray (DataKeyArrayList);
  1772. }
  1773. protected override void TrackViewState()
  1774. {
  1775. base.TrackViewState();
  1776. if (columns != null) ((IStateManager)columns).TrackViewState();
  1777. if (pagerSettings != null) ((IStateManager)pagerSettings).TrackViewState();
  1778. if (alternatingRowStyle != null) ((IStateManager)alternatingRowStyle).TrackViewState();
  1779. if (footerStyle != null) ((IStateManager)footerStyle).TrackViewState();
  1780. if (headerStyle != null) ((IStateManager)headerStyle).TrackViewState();
  1781. if (pagerStyle != null) ((IStateManager)pagerStyle).TrackViewState();
  1782. if (rowStyle != null) ((IStateManager)rowStyle).TrackViewState();
  1783. if (selectedRowStyle != null) ((IStateManager)selectedRowStyle).TrackViewState();
  1784. if (editRowStyle != null) ((IStateManager)editRowStyle).TrackViewState();
  1785. if (emptyDataRowStyle != null) ((IStateManager)emptyDataRowStyle).TrackViewState();
  1786. if (keys != null) ((IStateManager)keys).TrackViewState();
  1787. if (autoFieldProperties != null) {
  1788. foreach (IStateManager sm in autoFieldProperties)
  1789. sm.TrackViewState ();
  1790. }
  1791. }
  1792. protected override object SaveViewState()
  1793. {
  1794. object[] states = new object [12];
  1795. states[0] = base.SaveViewState();
  1796. states[1] = (columns == null ? null : ((IStateManager)columns).SaveViewState());
  1797. states[2] = (pagerSettings == null ? null : ((IStateManager)pagerSettings).SaveViewState());
  1798. states[3] = (alternatingRowStyle == null ? null : ((IStateManager)alternatingRowStyle).SaveViewState());
  1799. states[4] = (footerStyle == null ? null : ((IStateManager)footerStyle).SaveViewState());
  1800. states[5] = (headerStyle == null ? null : ((IStateManager)headerStyle).SaveViewState());
  1801. states[6] = (pagerStyle == null ? null : ((IStateManager)pagerStyle).SaveViewState());
  1802. states[7] = (rowStyle == null ? null : ((IStateManager)rowStyle).SaveViewState());
  1803. states[8] = (selectedRowStyle == null ? null : ((IStateManager)selectedRowStyle).SaveViewState());
  1804. states[9] = (editRowStyle == null ? null : ((IStateManager)editRowStyle).SaveViewState());
  1805. states[10] = (emptyDataRowStyle == null ? null : ((IStateManager)emptyDataRowStyle).SaveViewState());
  1806. if (autoFieldProperties != null) {
  1807. object[] data = new object [autoFieldProperties.Length];
  1808. bool allNull = true;
  1809. for (int n=0; n<data.Length; n++) {
  1810. data [n] = ((IStateManager)autoFieldProperties [n]).SaveViewState ();
  1811. if (data [n] != null) allNull = false;
  1812. }
  1813. if (!allNull) states [11] = data;
  1814. }
  1815. for (int i = states.Length - 1; i >= 0; i--) {
  1816. if (states [i] != null)
  1817. return states;
  1818. }
  1819. return null;
  1820. }
  1821. protected override void LoadViewState (object savedState)
  1822. {
  1823. if (savedState == null) {
  1824. base.LoadViewState (null);
  1825. return;
  1826. }
  1827. object [] states = (object []) savedState;
  1828. if (states[11] != null) {
  1829. object[] data = (object[]) states [11];
  1830. autoFieldProperties = new AutoGeneratedFieldProperties [data.Length];
  1831. for (int n=0; n<data.Length; n++) {
  1832. IStateManager p = new AutoGeneratedFieldProperties ();
  1833. p.TrackViewState ();
  1834. p.LoadViewState (data [n]);
  1835. autoFieldProperties [n] = (AutoGeneratedFieldProperties) p;
  1836. }
  1837. }
  1838. base.LoadViewState (states[0]);
  1839. if (states[1] != null) ((IStateManager)Columns).LoadViewState (states[1]);
  1840. if (states[2] != null) ((IStateManager)PagerSettings).LoadViewState (states[2]);
  1841. if (states[3] != null) ((IStateManager)AlternatingRowStyle).LoadViewState (states[3]);
  1842. if (states[4] != null) ((IStateManager)FooterStyle).LoadViewState (states[4]);
  1843. if (states[5] != null) ((IStateManager)HeaderStyle).LoadViewState (states[5]);
  1844. if (states[6] != null) ((IStateManager)PagerStyle).LoadViewState (states[6]);
  1845. if (states[7] != null) ((IStateManager)RowStyle).LoadViewState (states[7]);
  1846. if (states[8] != null) ((IStateManager)SelectedRowStyle).LoadViewState (states[8]);
  1847. if (states[9] != null) ((IStateManager)EditRowStyle).LoadViewState (states[9]);
  1848. if (states[10] != null) ((IStateManager)EmptyDataRowStyle).LoadViewState (states[10]);
  1849. }
  1850. void ICallbackEventHandler.RaiseCallbackEvent (string eventArgs)
  1851. {
  1852. RaiseCallbackEvent (eventArgs);
  1853. }
  1854. protected virtual void RaiseCallbackEvent (string eventArgs)
  1855. {
  1856. string[] clientData = eventArgs.Split ('|');
  1857. PageIndex = int.Parse (clientData[0]);
  1858. SortExpression = HttpUtility.UrlDecode (clientData [1]);
  1859. SortDirection = (SortDirection) int.Parse (clientData [2]);
  1860. RaisePostBackEvent (clientData[3]);
  1861. DataBind ();
  1862. }
  1863. string ICallbackEventHandler.GetCallbackResult ()
  1864. {
  1865. return GetCallbackResult ();
  1866. }
  1867. protected virtual string GetCallbackResult ()
  1868. {
  1869. PrepareControlHierarchy ();
  1870. StringWriter sw = new StringWriter ();
  1871. sw.Write (PageIndex.ToString () + '|' + SortExpression + '|' + (int) SortDirection + '|');
  1872. HtmlTextWriter writer = new HtmlTextWriter (sw);
  1873. RenderGrid (writer);
  1874. return sw.ToString ();
  1875. }
  1876. string ICallbackContainer.GetCallbackScript (IButtonControl control, string argument)
  1877. {
  1878. return GetCallbackScript (control, argument);
  1879. }
  1880. protected virtual string GetCallbackScript (IButtonControl control, string argument)
  1881. {
  1882. if (EnableSortingAndPagingCallbacks) {
  1883. Page page = Page;
  1884. if (page != null)
  1885. page.ClientScript.RegisterForEventValidation (UniqueID, argument);
  1886. return "javascript:GridView_ClientEvent (\"" + ClientID + "\",\"" + control.CommandName + "$" + control.CommandArgument + "\"); return false;";
  1887. } else
  1888. return null;
  1889. }
  1890. protected override void OnPagePreLoad (object sender, EventArgs e)
  1891. {
  1892. base.OnPagePreLoad (sender, e);
  1893. if (Page.IsPostBack && EnableSortingAndPagingCallbacks) {
  1894. int page;
  1895. if (int.TryParse (Page.Request.Form [ClientID + "_Page"], out page))
  1896. PageIndex = page;
  1897. int dir;
  1898. if (int.TryParse (Page.Request.Form [ClientID + "_SortDirection"], out dir))
  1899. SortDirection = (SortDirection) dir;
  1900. SortExpression = Page.Request.Form [ClientID + "_SortExpression"];
  1901. }
  1902. }
  1903. const string onPreRenderScript = @"var {0} = new Object ();
  1904. {0}.pageIndex = {1};
  1905. {0}.sortExp = {2};
  1906. {0}.sortDir = {3};
  1907. {0}.uid = {4};
  1908. {0}.form = {5};
  1909. ";
  1910. protected internal override void OnPreRender (EventArgs e)
  1911. {
  1912. base.OnPreRender (e);
  1913. if (EnableSortingAndPagingCallbacks) {
  1914. if (!Page.ClientScript.IsClientScriptIncludeRegistered (typeof(GridView), "GridView.js")) {
  1915. string url = Page.ClientScript.GetWebResourceUrl (typeof(GridView), "GridView.js");
  1916. Page.ClientScript.RegisterClientScriptInclude (typeof(GridView), "GridView.js", url);
  1917. }
  1918. string cgrid = ClientID + "_data";
  1919. string script = String.Format (onPreRenderScript,
  1920. cgrid,
  1921. ClientScriptManager.GetScriptLiteral (PageIndex),
  1922. ClientScriptManager.GetScriptLiteral (SortExpression == null ? "" : SortExpression),
  1923. ClientScriptManager.GetScriptLiteral ((int) SortDirection),
  1924. ClientScriptManager.GetScriptLiteral (UniqueID),
  1925. Page.theForm);
  1926. Page.ClientScript.RegisterStartupScript (typeof(TreeView), this.UniqueID, script, true);
  1927. // Make sure the basic script infrastructure is rendered
  1928. Page.ClientScript.GetCallbackEventReference (this, "null", "", "null");
  1929. Page.ClientScript.GetPostBackClientHyperlink (this, "");
  1930. }
  1931. }
  1932. protected internal override void Render (HtmlTextWriter writer)
  1933. {
  1934. PrepareControlHierarchy ();
  1935. if (EnableSortingAndPagingCallbacks)
  1936. writer.AddAttribute (HtmlTextWriterAttribute.Id, ClientID + "_div");
  1937. writer.RenderBeginTag (HtmlTextWriterTag.Div);
  1938. RenderGrid (writer);
  1939. writer.RenderEndTag ();
  1940. }
  1941. void RenderGrid (HtmlTextWriter writer)
  1942. {
  1943. if (table == null)
  1944. return;
  1945. table.Render (writer);
  1946. }
  1947. PostBackOptions IPostBackContainer.GetPostBackOptions (IButtonControl control)
  1948. {
  1949. if (control == null)
  1950. throw new ArgumentNullException ("control");
  1951. if (control.CausesValidation)
  1952. 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.");
  1953. PostBackOptions options = new PostBackOptions (this);
  1954. options.Argument = control.CommandName + "$" + control.CommandArgument;
  1955. options.RequiresJavaScriptProtocol = true;
  1956. return options;
  1957. }
  1958. }
  1959. }
  1960. #endif