SqlDataSourceView.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  1. //
  2. // System.Web.UI.WebControls.SqlDataSourceView
  3. //
  4. // Authors:
  5. // Ben Maurer ([email protected])
  6. // Sanjay Gupta ([email protected])
  7. //
  8. // (C) 2003 Ben Maurer
  9. // (C) Novell, Inc. (http://www.novell.com)
  10. //
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. #if NET_2_0
  32. using System.Collections;
  33. using System.Collections.Specialized;
  34. using System.Text;
  35. using System.Data;
  36. using System.ComponentModel;
  37. using System.Data.Common;
  38. namespace System.Web.UI.WebControls {
  39. public class SqlDataSourceView : DataSourceView, IStateManager {
  40. HttpContext context;
  41. DbProviderFactory factory;
  42. DbConnection connection;
  43. public SqlDataSourceView (SqlDataSource owner, string name, HttpContext context)
  44. : base (owner, name)
  45. {
  46. this.owner = owner;
  47. this.name = name;
  48. this.context = context;
  49. }
  50. void InitConnection ()
  51. {
  52. if (factory == null) factory = owner.GetDbProviderFactoryInternal ();
  53. if (connection == null) {
  54. connection = factory.CreateConnection ();
  55. connection.ConnectionString = owner.ConnectionString;
  56. }
  57. }
  58. public int Delete (IDictionary keys, IDictionary oldValues)
  59. {
  60. return ExecuteDelete (keys, oldValues);
  61. }
  62. protected override int ExecuteDelete (IDictionary keys, IDictionary oldValues)
  63. {
  64. if (!CanDelete)
  65. throw new NotSupportedException("Delete operation is not supported");
  66. if (oldValues == null && ConflictDetection == ConflictOptions.CompareAllValues)
  67. throw new InvalidOperationException ("oldValues parameters should be specified when ConflictOptions is set to CompareAllValues");
  68. InitConnection ();
  69. DbCommand command = factory.CreateCommand ();
  70. command.CommandText = DeleteCommand;
  71. command.Connection = connection;
  72. if (DeleteCommandType == SqlDataSourceCommandType.Text)
  73. command.CommandType = CommandType.Text;
  74. else
  75. command.CommandType = CommandType.StoredProcedure;
  76. IDictionary oldDataValues;
  77. if (ConflictDetection == ConflictOptions.CompareAllValues) {
  78. oldDataValues = new Hashtable ();
  79. if (keys != null) {
  80. foreach (DictionaryEntry de in keys)
  81. oldDataValues [de.Key] = de.Value;
  82. }
  83. if (oldValues != null) {
  84. foreach (DictionaryEntry de in oldValues)
  85. oldDataValues [de.Key] = de.Value;
  86. }
  87. }
  88. else
  89. oldDataValues = keys;
  90. InitializeParameters (command, DeleteParameters, null, oldDataValues, true);
  91. SqlDataSourceCommandEventArgs args = new SqlDataSourceCommandEventArgs (command);
  92. OnDeleting (args);
  93. if (args.Cancel)
  94. return -1;
  95. bool closed = connection.State == ConnectionState.Closed;
  96. if (closed)
  97. connection.Open();
  98. Exception exception = null;
  99. int result = -1;;
  100. try {
  101. result = command.ExecuteNonQuery();
  102. } catch (Exception e) {
  103. exception = e;
  104. }
  105. if (closed)
  106. connection.Close ();
  107. OnDataSourceViewChanged (EventArgs.Empty);
  108. SqlDataSourceStatusEventArgs deletedArgs =
  109. new SqlDataSourceStatusEventArgs (command, result, exception);
  110. OnDeleted (deletedArgs);
  111. if (exception != null && !deletedArgs.ExceptionHandled)
  112. throw exception;
  113. return result;
  114. }
  115. public int Insert (IDictionary values)
  116. {
  117. return ExecuteInsert (values);
  118. }
  119. protected override int ExecuteInsert (IDictionary values)
  120. {
  121. if (!CanInsert)
  122. throw new NotSupportedException ("Insert operation is not supported");
  123. InitConnection ();
  124. DbCommand command = factory.CreateCommand ();
  125. command.CommandText = InsertCommand;
  126. command.Connection = connection;
  127. if (InsertCommandType == SqlDataSourceCommandType.Text)
  128. command.CommandType = CommandType.Text;
  129. else
  130. command.CommandType = CommandType.StoredProcedure;
  131. InitializeParameters (command, InsertParameters, values, null, false);
  132. SqlDataSourceCommandEventArgs args = new SqlDataSourceCommandEventArgs (command);
  133. OnInserting (args);
  134. if (args.Cancel)
  135. return -1;
  136. bool closed = connection.State == ConnectionState.Closed;
  137. if (closed)
  138. connection.Open ();
  139. Exception exception = null;
  140. int result = -1;
  141. try {
  142. result = command.ExecuteNonQuery ();
  143. }
  144. catch (Exception e) {
  145. exception = e;
  146. }
  147. if (closed)
  148. connection.Close ();
  149. OnDataSourceViewChanged (EventArgs.Empty);
  150. OnInserted (new SqlDataSourceStatusEventArgs (command, result, exception));
  151. if (exception != null)
  152. throw exception;
  153. return result;
  154. }
  155. public IEnumerable Select (DataSourceSelectArguments arguments)
  156. {
  157. return ExecuteSelect (arguments);
  158. }
  159. protected internal override IEnumerable ExecuteSelect (DataSourceSelectArguments arguments)
  160. {
  161. if (SortParameterName.Length > 0 && SelectCommandType == SqlDataSourceCommandType.Text)
  162. throw new NotSupportedException ("The SortParameterName property is only supported with stored procedure commands in SqlDataSource");
  163. if (arguments.SortExpression.Length > 0 && owner.DataSourceMode == SqlDataSourceMode.DataReader)
  164. throw new NotSupportedException ("SqlDataSource cannot sort. Set DataSourceMode to DataSet to enable sorting.");
  165. if (arguments.StartRowIndex > 0 || arguments.MaximumRows > 0)
  166. throw new NotSupportedException ("SqlDataSource does not have paging enabled. Set the DataSourceMode to DataSet to enable paging.");
  167. if (FilterExpression.Length > 0 && owner.DataSourceMode == SqlDataSourceMode.DataReader)
  168. throw new NotSupportedException ("SqlDataSource only supports filtering when the data source's DataSourceMode is set to DataSet.");
  169. InitConnection ();
  170. DbCommand command = factory.CreateCommand ();
  171. command.CommandText = SelectCommand;
  172. command.Connection = connection;
  173. if (SelectCommandType == SqlDataSourceCommandType.Text)
  174. command.CommandType = CommandType.Text;
  175. else {
  176. command.CommandType = CommandType.StoredProcedure;
  177. if (SortParameterName.Length > 0 && arguments.SortExpression.Length > 0)
  178. command.Parameters.Add (CreateDbParameter (SortParameterName, arguments.SortExpression));
  179. }
  180. if (SelectParameters.Count > 0)
  181. InitializeParameters (command, SelectParameters, null, null, false);
  182. Exception exception = null;
  183. if (owner.DataSourceMode == SqlDataSourceMode.DataSet) {
  184. DataView dataView = null;
  185. if (owner.EnableCaching)
  186. dataView = (DataView) owner.Cache.GetCachedObject (SelectCommand, SelectParameters);
  187. if (dataView == null) {
  188. SqlDataSourceSelectingEventArgs selectingArgs = new SqlDataSourceSelectingEventArgs (command, arguments);
  189. OnSelecting (selectingArgs);
  190. if (selectingArgs.Cancel || !PrepareNullParameters (command, CancelSelectOnNullParameter)) {
  191. return null;
  192. }
  193. try {
  194. DbDataAdapter adapter = factory.CreateDataAdapter ();
  195. DataSet dataset = new DataSet ();
  196. adapter.SelectCommand = command;
  197. adapter.Fill (dataset, name);
  198. dataView = dataset.Tables [0].DefaultView;
  199. if (dataView == null)
  200. throw new InvalidOperationException ();
  201. }
  202. catch (Exception e) {
  203. exception = e;
  204. }
  205. int rowsAffected = (dataView == null) ? 0 : dataView.Count;
  206. SqlDataSourceStatusEventArgs selectedArgs = new SqlDataSourceStatusEventArgs (command, rowsAffected, exception);
  207. OnSelected (selectedArgs);
  208. if (exception != null && !selectedArgs.ExceptionHandled)
  209. throw exception;
  210. if (owner.EnableCaching)
  211. owner.Cache.SetCachedObject (SelectCommand, selectParameters, dataView);
  212. }
  213. if (SortParameterName.Length == 0 || SelectCommandType == SqlDataSourceCommandType.Text)
  214. dataView.Sort = arguments.SortExpression;
  215. if (FilterExpression.Length > 0) {
  216. IOrderedDictionary fparams = FilterParameters.GetValues (context, owner);
  217. SqlDataSourceFilteringEventArgs fargs = new SqlDataSourceFilteringEventArgs (fparams);
  218. OnFiltering (fargs);
  219. if (!fargs.Cancel) {
  220. object [] formatValues = new object [fparams.Count];
  221. for (int n = 0; n < formatValues.Length; n++) {
  222. formatValues [n] = fparams [n];
  223. if (formatValues [n] == null) return dataView;
  224. }
  225. dataView.RowFilter = string.Format (FilterExpression, formatValues);
  226. }
  227. }
  228. return dataView;
  229. }
  230. else {
  231. SqlDataSourceSelectingEventArgs selectingArgs = new SqlDataSourceSelectingEventArgs (command, arguments);
  232. OnSelecting (selectingArgs);
  233. if (selectingArgs.Cancel || !PrepareNullParameters (command, CancelSelectOnNullParameter)) {
  234. return null;
  235. }
  236. DbDataReader reader = null;
  237. bool closed = connection.State == ConnectionState.Closed;
  238. if (closed)
  239. connection.Open ();
  240. try {
  241. reader = command.ExecuteReader (closed ? CommandBehavior.CloseConnection : CommandBehavior.Default);
  242. }
  243. catch (Exception e) {
  244. exception = e;
  245. }
  246. SqlDataSourceStatusEventArgs selectedArgs =
  247. new SqlDataSourceStatusEventArgs (command, reader.RecordsAffected, exception);
  248. OnSelected (selectedArgs);
  249. if (exception != null && !selectedArgs.ExceptionHandled)
  250. throw exception;
  251. return reader;
  252. }
  253. }
  254. static bool PrepareNullParameters (DbCommand command, bool cancelIfHas)
  255. {
  256. for (int i = 0; i < command.Parameters.Count; i++) {
  257. DbParameter param = command.Parameters [i];
  258. if (param.Value == null && ((param.Direction & ParameterDirection.Input) != 0)) {
  259. if (cancelIfHas)
  260. return false;
  261. else
  262. param.Value = DBNull.Value;
  263. }
  264. }
  265. return true;
  266. }
  267. public int Update (IDictionary keys, IDictionary values, IDictionary oldValues)
  268. {
  269. return ExecuteUpdate (keys, values, oldValues);
  270. }
  271. protected override int ExecuteUpdate (IDictionary keys, IDictionary values, IDictionary oldValues)
  272. {
  273. if (!CanUpdate)
  274. throw new NotSupportedException ("Update operation is not supported");
  275. if (oldValues == null && ConflictDetection == ConflictOptions.CompareAllValues)
  276. throw new InvalidOperationException ("oldValues parameters should be specified when ConflictOptions is set to CompareAllValues");
  277. InitConnection ();
  278. DbCommand command = factory.CreateCommand ();
  279. command.CommandText = UpdateCommand;
  280. command.Connection = connection;
  281. if (UpdateCommandType == SqlDataSourceCommandType.Text)
  282. command.CommandType = CommandType.Text;
  283. else
  284. command.CommandType = CommandType.StoredProcedure;
  285. IDictionary oldDataValues;
  286. if (ConflictDetection == ConflictOptions.CompareAllValues) {
  287. oldDataValues = new OrderedDictionary ();
  288. if (keys != null) {
  289. foreach (DictionaryEntry de in keys)
  290. oldDataValues [de.Key] = de.Value;
  291. }
  292. if (oldValues != null) {
  293. foreach (DictionaryEntry de in oldValues)
  294. oldDataValues [de.Key] = de.Value;
  295. }
  296. }
  297. else {
  298. oldDataValues = keys;
  299. }
  300. IDictionary dataValues = values;
  301. InitializeParameters (command, UpdateParameters, dataValues, oldDataValues, ConflictDetection == ConflictOptions.OverwriteChanges);
  302. SqlDataSourceCommandEventArgs args = new SqlDataSourceCommandEventArgs (command);
  303. OnUpdating (args);
  304. if (args.Cancel)
  305. return -1;
  306. bool closed = connection.State == ConnectionState.Closed;
  307. if (closed)
  308. connection.Open ();
  309. Exception exception = null;
  310. int result = -1;
  311. try {
  312. result = command.ExecuteNonQuery ();
  313. }
  314. catch (Exception e) {
  315. exception = e;
  316. }
  317. if (closed)
  318. connection.Close ();
  319. OnDataSourceViewChanged (EventArgs.Empty);
  320. SqlDataSourceStatusEventArgs updatedArgs =
  321. new SqlDataSourceStatusEventArgs (command, result, exception);
  322. OnUpdated (updatedArgs);
  323. if (exception != null && !updatedArgs.ExceptionHandled)
  324. throw exception;
  325. return result;
  326. }
  327. string FormatOldParameter (string name)
  328. {
  329. string f = OldValuesParameterFormatString;
  330. if (f.Length > 0)
  331. return String.Format (f, name);
  332. else
  333. return name;
  334. }
  335. object FindValueByName (string parameterName, IDictionary values, bool format)
  336. {
  337. if (values == null)
  338. return null;
  339. foreach (DictionaryEntry de in values) {
  340. string valueName = format == true ? FormatOldParameter (de.Key.ToString ()) : de.Key.ToString ();
  341. if (String.Compare(parameterName, valueName, StringComparison.InvariantCultureIgnoreCase) == 0)
  342. return values [de.Key];
  343. }
  344. return null;
  345. }
  346. void InitializeParameters (DbCommand command, ParameterCollection parameters, IDictionary values, IDictionary oldValues, bool parametersMayMatchOldValues)
  347. {
  348. IOrderedDictionary parameterValues = parameters.GetValues (context, owner);
  349. foreach (string parameterName in parameterValues.Keys) {
  350. Parameter p = parameters [parameterName];
  351. object value = FindValueByName (parameterName, values, false);
  352. string valueName = parameterName;
  353. if (value == null)
  354. value = FindValueByName (parameterName, oldValues, true);
  355. if (value == null && parametersMayMatchOldValues) {
  356. value = FindValueByName (parameterName, oldValues, false);
  357. valueName = FormatOldParameter (parameterName);
  358. }
  359. if (value != null) {
  360. object dbValue = p.ConvertValue (value);
  361. DbParameter newParameter = CreateDbParameter (valueName, dbValue, p.Direction, p.Size);
  362. if (!command.Parameters.Contains (newParameter.ParameterName)) {
  363. command.Parameters.Add (newParameter);
  364. }
  365. }
  366. else {
  367. command.Parameters.Add (CreateDbParameter (p.Name, parameterValues [parameterName], p.Direction, p.Size));
  368. }
  369. }
  370. if (values != null) {
  371. foreach (DictionaryEntry de in values)
  372. if (!command.Parameters.Contains (ParameterPrefix + (string) de.Key))
  373. command.Parameters.Add (CreateDbParameter ((string) de.Key, de.Value));
  374. }
  375. if (oldValues != null) {
  376. foreach (DictionaryEntry de in oldValues)
  377. if (!command.Parameters.Contains (ParameterPrefix + FormatOldParameter ((string) de.Key)))
  378. command.Parameters.Add (CreateDbParameter (FormatOldParameter ((string) de.Key), de.Value));
  379. }
  380. }
  381. private DbParameter CreateDbParameter (string name, object value)
  382. {
  383. return CreateDbParameter (name, value, ParameterDirection.Input, -1);
  384. }
  385. private DbParameter CreateDbParameter (string name, object value, ParameterDirection dir, int size)
  386. {
  387. DbParameter dbp = factory.CreateParameter ();
  388. dbp.ParameterName = ParameterPrefix + name;
  389. dbp.Value = value;
  390. dbp.Direction = dir;
  391. if (size != -1)
  392. dbp.Size = size;
  393. return dbp;
  394. }
  395. void IStateManager.LoadViewState (object savedState)
  396. {
  397. LoadViewState (savedState);
  398. }
  399. object IStateManager.SaveViewState ()
  400. {
  401. return SaveViewState ();
  402. }
  403. void IStateManager.TrackViewState ()
  404. {
  405. TrackViewState ();
  406. }
  407. protected virtual void LoadViewState (object savedState)
  408. {
  409. object [] vs = savedState as object [];
  410. if (vs == null)
  411. return;
  412. if (vs [0] != null) ((IStateManager) deleteParameters).LoadViewState (vs [0]);
  413. if (vs [1] != null) ((IStateManager) filterParameters).LoadViewState (vs [1]);
  414. if (vs [2] != null) ((IStateManager) insertParameters).LoadViewState (vs [2]);
  415. if (vs [3] != null) ((IStateManager) selectParameters).LoadViewState (vs [3]);
  416. if (vs [4] != null) ((IStateManager) updateParameters).LoadViewState (vs [4]);
  417. }
  418. protected virtual object SaveViewState ()
  419. {
  420. object [] vs = new object [5];
  421. if (deleteParameters != null) vs [0] = ((IStateManager) deleteParameters).SaveViewState ();
  422. if (filterParameters != null) vs [1] = ((IStateManager) filterParameters).SaveViewState ();
  423. if (insertParameters != null) vs [2] = ((IStateManager) insertParameters).SaveViewState ();
  424. if (selectParameters != null) vs [3] = ((IStateManager) selectParameters).SaveViewState ();
  425. if (updateParameters != null) vs [4] = ((IStateManager) updateParameters).SaveViewState ();
  426. foreach (object o in vs)
  427. if (o != null) return vs;
  428. return null;
  429. }
  430. protected virtual void TrackViewState ()
  431. {
  432. tracking = true;
  433. if (filterParameters != null) ((IStateManager) filterParameters).TrackViewState ();
  434. if (selectParameters != null) ((IStateManager) selectParameters).TrackViewState ();
  435. }
  436. bool IStateManager.IsTrackingViewState {
  437. get { return IsTrackingViewState; }
  438. }
  439. // LAME SPEC: MSDN says value should be saved in ViewState but tests show otherwise.
  440. private bool cancelSelectOnNullParameter = true;
  441. public bool CancelSelectOnNullParameter {
  442. get { return cancelSelectOnNullParameter; }
  443. set {
  444. if (CancelSelectOnNullParameter == value)
  445. return;
  446. cancelSelectOnNullParameter = value;
  447. OnDataSourceViewChanged (EventArgs.Empty);
  448. }
  449. }
  450. public override bool CanDelete {
  451. get { return DeleteCommand != null && DeleteCommand != ""; }
  452. }
  453. public override bool CanInsert {
  454. get { return InsertCommand != null && InsertCommand != ""; }
  455. }
  456. public override bool CanPage {
  457. /* according to MS, this is false in all cases */
  458. get { return false; }
  459. }
  460. public override bool CanRetrieveTotalRowCount {
  461. /* according to MS, this is false in all cases */
  462. get { return false; }
  463. }
  464. public override bool CanSort {
  465. get {
  466. /* we can sort if we're a DataSet, regardless of sort parameter name.
  467. we can sort if we're a DataReader, if the sort parameter name is not null/"".
  468. */
  469. return (owner.DataSourceMode == SqlDataSourceMode.DataSet
  470. || (SortParameterName != null && SortParameterName != ""));
  471. }
  472. }
  473. public override bool CanUpdate {
  474. get { return UpdateCommand != null && UpdateCommand != ""; }
  475. }
  476. // LAME SPEC: MSDN says value should be saved in ViewState but tests show otherwise.
  477. private ConflictOptions conflictDetection = ConflictOptions.OverwriteChanges;
  478. public ConflictOptions ConflictDetection {
  479. get { return conflictDetection; }
  480. set {
  481. if (ConflictDetection == value)
  482. return;
  483. conflictDetection = value;
  484. OnDataSourceViewChanged (EventArgs.Empty);
  485. }
  486. }
  487. // LAME SPEC: MSDN says value should be saved in ViewState but tests show otherwise.
  488. private string deleteCommand = "";
  489. public string DeleteCommand {
  490. get { return deleteCommand; }
  491. set { deleteCommand = value; }
  492. }
  493. // LAME SPEC: MSDN says value should be saved in ViewState but tests show otherwise.
  494. private SqlDataSourceCommandType deleteCommandType = SqlDataSourceCommandType.Text;
  495. public SqlDataSourceCommandType DeleteCommandType {
  496. get { return deleteCommandType; }
  497. set { deleteCommandType = value; }
  498. }
  499. [DefaultValueAttribute (null)]
  500. [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
  501. [EditorAttribute ("System.Web.UI.Design.WebControls.ParameterCollectionEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  502. public ParameterCollection DeleteParameters {
  503. get { return GetParameterCollection (ref deleteParameters, false, false); }
  504. }
  505. // LAME SPEC: MSDN says value should be saved in ViewState but tests show otherwise.
  506. private string filterExpression = "";
  507. public string FilterExpression {
  508. get { return filterExpression; }
  509. set {
  510. if (FilterExpression == value)
  511. return;
  512. filterExpression = value;
  513. OnDataSourceViewChanged (EventArgs.Empty);
  514. }
  515. }
  516. [EditorAttribute ("System.Web.UI.Design.WebControls.ParameterCollectionEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  517. [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
  518. [DefaultValueAttribute (null)]
  519. public ParameterCollection FilterParameters {
  520. get { return GetParameterCollection (ref filterParameters, true, true); }
  521. }
  522. // LAME SPEC: MSDN says value should be saved in ViewState but tests show otherwise.
  523. private string insertCommand = "";
  524. public string InsertCommand {
  525. get { return insertCommand; }
  526. set { insertCommand = value; }
  527. }
  528. // LAME SPEC: MSDN says value should be saved in ViewState but tests show otherwise.
  529. private SqlDataSourceCommandType insertCommandType = SqlDataSourceCommandType.Text;
  530. public SqlDataSourceCommandType InsertCommandType {
  531. get { return insertCommandType; }
  532. set { insertCommandType = value; }
  533. }
  534. [EditorAttribute ("System.Web.UI.Design.WebControls.ParameterCollectionEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  535. [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
  536. [DefaultValueAttribute (null)]
  537. public ParameterCollection InsertParameters {
  538. get { return GetParameterCollection (ref insertParameters, false, false); }
  539. }
  540. protected bool IsTrackingViewState {
  541. get { return tracking; }
  542. }
  543. // LAME SPEC: MSDN says value should be saved in ViewState but tests show otherwise.
  544. private string oldValuesParameterFormatString = "{0}";
  545. [DefaultValue ("{0}")]
  546. public string OldValuesParameterFormatString {
  547. get { return oldValuesParameterFormatString; }
  548. set {
  549. if (OldValuesParameterFormatString == value)
  550. return;
  551. oldValuesParameterFormatString = value;
  552. OnDataSourceViewChanged (EventArgs.Empty);
  553. }
  554. }
  555. // LAME SPEC: MSDN says value should be saved in ViewState but tests show otherwise.
  556. private string selectCommand;
  557. public string SelectCommand {
  558. get { return selectCommand != null ? selectCommand : string.Empty; }
  559. set {
  560. if (SelectCommand == value)
  561. return;
  562. selectCommand = value;
  563. OnDataSourceViewChanged (EventArgs.Empty);
  564. }
  565. }
  566. // LAME SPEC: MSDN says value should be saved in ViewState but tests show otherwise.
  567. private SqlDataSourceCommandType selectCommandType = SqlDataSourceCommandType.Text;
  568. public SqlDataSourceCommandType SelectCommandType {
  569. get { return selectCommandType; }
  570. set { selectCommandType = value; }
  571. }
  572. public ParameterCollection SelectParameters {
  573. get { return GetParameterCollection (ref selectParameters, true, true); }
  574. }
  575. // LAME SPEC: MSDN says value should be saved in ViewState but tests show otherwise.
  576. private string sortParameterName = "";
  577. public string SortParameterName {
  578. get { return sortParameterName; }
  579. set {
  580. if (SortParameterName == value)
  581. return;
  582. sortParameterName = value;
  583. OnDataSourceViewChanged (EventArgs.Empty);
  584. }
  585. }
  586. // LAME SPEC: MSDN says value should be saved in ViewState but tests show otherwise.
  587. private string updateCommand = "";
  588. public string UpdateCommand {
  589. get { return updateCommand; }
  590. set { updateCommand = value; }
  591. }
  592. // LAME SPEC: MSDN says value should be saved in ViewState but tests show otherwise.
  593. private SqlDataSourceCommandType updateCommandType = SqlDataSourceCommandType.Text;
  594. public SqlDataSourceCommandType UpdateCommandType {
  595. get { return updateCommandType; }
  596. set { updateCommandType = value; }
  597. }
  598. [EditorAttribute ("System.Web.UI.Design.WebControls.ParameterCollectionEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  599. [PersistenceModeAttribute (PersistenceMode.InnerProperty)]
  600. [DefaultValueAttribute (null)]
  601. public ParameterCollection UpdateParameters {
  602. get { return GetParameterCollection (ref updateParameters, false, false); }
  603. }
  604. void ParametersChanged (object source, EventArgs args)
  605. {
  606. OnDataSourceViewChanged (EventArgs.Empty);
  607. }
  608. ParameterCollection GetParameterCollection (ref ParameterCollection output, bool propagateTrackViewState, bool subscribeChanged)
  609. {
  610. if (output != null)
  611. return output;
  612. output = new ParameterCollection ();
  613. if(subscribeChanged)
  614. output.ParametersChanged += new EventHandler (ParametersChanged);
  615. if (IsTrackingViewState && propagateTrackViewState)
  616. ((IStateManager) output).TrackViewState ();
  617. return output;
  618. }
  619. protected virtual string ParameterPrefix {
  620. get {
  621. switch (owner.ProviderName) {
  622. case "":
  623. case "System.Data.SqlClient": return "@";
  624. case "System.Data.OracleClient": return ":";
  625. }
  626. return "";
  627. }
  628. }
  629. ParameterCollection deleteParameters;
  630. ParameterCollection filterParameters;
  631. ParameterCollection insertParameters;
  632. ParameterCollection selectParameters;
  633. ParameterCollection updateParameters;
  634. bool tracking;
  635. string name;
  636. SqlDataSource owner;
  637. #region OnDelete
  638. static readonly object EventDeleted = new object ();
  639. protected virtual void OnDeleted (SqlDataSourceStatusEventArgs e)
  640. {
  641. if (!HasEvents ()) return;
  642. SqlDataSourceStatusEventHandler h = Events [EventDeleted] as SqlDataSourceStatusEventHandler;
  643. if (h != null)
  644. h (this, e);
  645. }
  646. public event SqlDataSourceStatusEventHandler Deleted {
  647. add { Events.AddHandler (EventDeleted, value); }
  648. remove { Events.RemoveHandler (EventDeleted, value); }
  649. }
  650. static readonly object EventDeleting = new object ();
  651. protected virtual void OnDeleting (SqlDataSourceCommandEventArgs e)
  652. {
  653. if (!HasEvents ()) return;
  654. SqlDataSourceCommandEventHandler h = Events [EventDeleting] as SqlDataSourceCommandEventHandler;
  655. if (h != null)
  656. h (this, e);
  657. }
  658. public event SqlDataSourceCommandEventHandler Deleting {
  659. add { Events.AddHandler (EventDeleting, value); }
  660. remove { Events.RemoveHandler (EventDeleting, value); }
  661. }
  662. #endregion
  663. #region OnFiltering
  664. static readonly object EventFiltering = new object ();
  665. protected virtual void OnFiltering (SqlDataSourceFilteringEventArgs e)
  666. {
  667. if (!HasEvents ()) return;
  668. SqlDataSourceFilteringEventHandler h = Events [EventFiltering] as SqlDataSourceFilteringEventHandler;
  669. if (h != null)
  670. h (this, e);
  671. }
  672. public event SqlDataSourceFilteringEventHandler Filtering {
  673. add { Events.AddHandler (EventFiltering, value); }
  674. remove { Events.RemoveHandler (EventFiltering, value); }
  675. }
  676. #endregion
  677. #region OnInsert
  678. static readonly object EventInserted = new object ();
  679. protected virtual void OnInserted (SqlDataSourceStatusEventArgs e)
  680. {
  681. if (!HasEvents ()) return;
  682. SqlDataSourceStatusEventHandler h = Events [EventInserted] as SqlDataSourceStatusEventHandler;
  683. if (h != null)
  684. h (this, e);
  685. }
  686. public event SqlDataSourceStatusEventHandler Inserted {
  687. add { Events.AddHandler (EventInserted, value); }
  688. remove { Events.RemoveHandler (EventInserted, value); }
  689. }
  690. static readonly object EventInserting = new object ();
  691. protected virtual void OnInserting (SqlDataSourceCommandEventArgs e)
  692. {
  693. if (!HasEvents ()) return;
  694. SqlDataSourceCommandEventHandler h = Events [EventInserting] as SqlDataSourceCommandEventHandler;
  695. if (h != null)
  696. h (this, e);
  697. }
  698. public event SqlDataSourceCommandEventHandler Inserting {
  699. add { Events.AddHandler (EventInserting, value); }
  700. remove { Events.RemoveHandler (EventInserting, value); }
  701. }
  702. #endregion
  703. #region OnSelect
  704. static readonly object EventSelected = new object ();
  705. protected virtual void OnSelected (SqlDataSourceStatusEventArgs e)
  706. {
  707. if (!HasEvents ()) return;
  708. SqlDataSourceStatusEventHandler h = Events [EventSelected] as SqlDataSourceStatusEventHandler;
  709. if (h != null)
  710. h (this, e);
  711. }
  712. public event SqlDataSourceStatusEventHandler Selected {
  713. add { Events.AddHandler (EventSelected, value); }
  714. remove { Events.RemoveHandler (EventSelected, value); }
  715. }
  716. static readonly object EventSelecting = new object ();
  717. protected virtual void OnSelecting (SqlDataSourceSelectingEventArgs e)
  718. {
  719. if (!HasEvents ()) return;
  720. SqlDataSourceSelectingEventHandler h = Events [EventSelecting] as SqlDataSourceSelectingEventHandler;
  721. if (h != null)
  722. h (this, e);
  723. }
  724. public event SqlDataSourceSelectingEventHandler Selecting {
  725. add { Events.AddHandler (EventSelecting, value); }
  726. remove { Events.RemoveHandler (EventSelecting, value); }
  727. }
  728. #endregion
  729. #region OnUpdate
  730. static readonly object EventUpdated = new object ();
  731. protected virtual void OnUpdated (SqlDataSourceStatusEventArgs e)
  732. {
  733. if (owner.EnableCaching)
  734. owner.Cache.Expire ();
  735. if (!HasEvents ()) return;
  736. SqlDataSourceStatusEventHandler h = Events [EventUpdated] as SqlDataSourceStatusEventHandler;
  737. if (h != null)
  738. h (this, e);
  739. }
  740. public event SqlDataSourceStatusEventHandler Updated {
  741. add { Events.AddHandler (EventUpdated, value); }
  742. remove { Events.RemoveHandler (EventUpdated, value); }
  743. }
  744. static readonly object EventUpdating = new object ();
  745. protected virtual void OnUpdating (SqlDataSourceCommandEventArgs e)
  746. {
  747. if (!HasEvents ()) return;
  748. SqlDataSourceCommandEventHandler h = Events [EventUpdating] as SqlDataSourceCommandEventHandler;
  749. if (h != null)
  750. h (this, e);
  751. }
  752. public event SqlDataSourceCommandEventHandler Updating {
  753. add { Events.AddHandler (EventUpdating, value); }
  754. remove { Events.RemoveHandler (EventUpdating, value); }
  755. }
  756. #endregion
  757. }
  758. }
  759. #endif