EditCommandColumnTest.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. //
  2. // Tests for System.Web.UI.WebControls.EditCommandColumn.cs
  3. //
  4. // Author:
  5. // Peter Dennis Bartok ([email protected])
  6. //
  7. //
  8. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using NUnit.Framework;
  30. using System;
  31. using System.Collections;
  32. using System.Data;
  33. using System.IO;
  34. using System.Globalization;
  35. using System.Text;
  36. using System.Web;
  37. using System.Web.UI;
  38. using System.Web.UI.WebControls;
  39. using MonoTests.stand_alone.WebHarness;
  40. using MonoTests.SystemWeb.Framework;
  41. namespace MonoTests.System.Web.UI.WebControls
  42. {
  43. [TestFixture]
  44. public class EditCommandColumnTest {
  45. private class DataGridTest : DataGrid {
  46. public ArrayList CreateColumns (PagedDataSource data_source, bool use_data_source) {
  47. return CreateColumnSet (data_source, use_data_source);
  48. }
  49. public void CreateControls (bool use_data_source) {
  50. CreateControlHierarchy (use_data_source);
  51. }
  52. }
  53. [Test]
  54. public void Defaults ()
  55. {
  56. EditCommandColumn e;
  57. e = new EditCommandColumn();
  58. Assert.AreEqual(ButtonColumnType.LinkButton, e.ButtonType, "D1");
  59. Assert.AreEqual(string.Empty, e.CancelText, "D2");
  60. Assert.AreEqual(string.Empty, e.EditText, "D3");
  61. Assert.AreEqual(string.Empty, e.UpdateText, "D4");
  62. #if NET_2_0
  63. Assert.AreEqual (true, e.CausesValidation, "CausesValidation");
  64. Assert.AreEqual (string.Empty, e.ValidationGroup, "ValidationGroup");
  65. #endif
  66. }
  67. [Test]
  68. public void Properties () {
  69. EditCommandColumn e;
  70. e = new EditCommandColumn();
  71. e.ButtonType = ButtonColumnType.PushButton;
  72. Assert.AreEqual(ButtonColumnType.PushButton, e.ButtonType, "P1");
  73. e.CancelText = "Cancel this!";
  74. Assert.AreEqual("Cancel this!", e.CancelText, "D2");
  75. e.EditText = "Edit me good";
  76. Assert.AreEqual("Edit me good", e.EditText, "D3");
  77. e.UpdateText = "Update? What update?";
  78. Assert.AreEqual("Update? What update?", e.UpdateText, "D4");
  79. #if NET_2_0
  80. e.CausesValidation = false;
  81. Assert.AreEqual (false, e.CausesValidation, "CausesValidation");
  82. e.ValidationGroup = "test";
  83. Assert.AreEqual ("test", e.ValidationGroup, "ValidationGroup");
  84. #endif
  85. }
  86. private string ControlMarkup(Control c) {
  87. StringWriter sw = new StringWriter ();
  88. HtmlTextWriter tw = new CleanHtmlTextWriter (sw);
  89. c.RenderControl (tw);
  90. return sw.ToString ();
  91. }
  92. private void ShowControlsRecursive (Control c, int depth) {
  93. StringWriter sw = new StringWriter ();
  94. HtmlTextWriter tw = new CleanHtmlTextWriter (sw);
  95. c.RenderControl (tw);
  96. Console.WriteLine (sw.ToString ());
  97. Console.WriteLine (c);
  98. foreach (Control child in c.Controls)
  99. ShowControlsRecursive (child, depth + 5);
  100. }
  101. [Test]
  102. public void InitializeCell () {
  103. DataGridTest p = new DataGridTest ();
  104. DataTable table = new DataTable ();
  105. EditCommandColumn e;
  106. string markup;
  107. e = new EditCommandColumn();
  108. e.ButtonType = ButtonColumnType.LinkButton;
  109. e.CancelText = "Cancel";
  110. e.EditText = "Edit";
  111. e.UpdateText = "Update";
  112. table.Columns.Add (new DataColumn ("one", typeof (string)));
  113. table.Columns.Add (new DataColumn ("two", typeof (string)));
  114. table.Columns.Add (new DataColumn ("three", typeof (string)));
  115. table.Rows.Add (new object [] { "1", "2", "3" });
  116. p.DataSource = new DataView (table);
  117. p.Columns.Add(e);
  118. e = new EditCommandColumn();
  119. e.ButtonType = ButtonColumnType.PushButton;
  120. e.CancelText = "Abbrechen";
  121. e.EditText = "Bearbeiten";
  122. e.UpdateText = "Refresh";
  123. p.Columns.Add(e);
  124. // This will trigger EditCommandColumn.InitializeCell, without any EditItem set, tests the EditText render
  125. p.CreateControls (true);
  126. p.ID = "sucker";
  127. Assert.AreEqual (2, p.Columns.Count, "I1");
  128. markup = ControlMarkup(p.Controls[0]);
  129. markup = markup.Replace("\t", "");
  130. markup = markup.Replace ("\r", "");
  131. markup = markup.Replace ("\n", "");
  132. #if NET_2_0
  133. HtmlDiff.AssertAreEqual (
  134. "<table border=\"0\"><tr><td>&nbsp;</td><td>&nbsp;</td><td>one</td><td>two</td><td>three</td>" +
  135. "</tr><tr><td><a>Edit</a></td><td><input name=\"sucker$ctl02$ctl00\" type=\"submit\" value=\"Bearbeiten\" /></td><td>1</td><td>2</td><td>3</td>" +
  136. "</tr><tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>" +
  137. "</tr></table>", markup, "I2");
  138. #else
  139. Assert.AreEqual (
  140. "<table border=\"0\" id=\"sucker\"><tr><td>&nbsp;</td><td>&nbsp;</td><td>one</td><td>two</td><td>three</td>" +
  141. "</tr><tr><td><a>Edit</a></td><td><input name=\"sucker:_ctl1:_ctl0\" type=\"submit\" value=\"Bearbeiten\" /></td><td>1</td><td>2</td><td>3</td>" +
  142. "</tr><tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>" +
  143. "</tr></table>", markup, "I2");
  144. #endif
  145. //ShowControlsRecursive (p.Controls [0], 1);
  146. }
  147. [Test]
  148. public void ThisIsADGTest () {
  149. DataGridTest p = new DataGridTest ();
  150. DataTable table = new DataTable ();
  151. EditCommandColumn e;
  152. string markup;
  153. e = new EditCommandColumn();
  154. e.ButtonType = ButtonColumnType.LinkButton;
  155. e.CancelText = "Cancel";
  156. e.EditText = "Edit";
  157. e.UpdateText = "Update";
  158. table.Columns.Add (new DataColumn ("one", typeof (string)));
  159. table.Columns.Add (new DataColumn ("two", typeof (string)));
  160. table.Columns.Add (new DataColumn ("three", typeof (string)));
  161. table.Rows.Add (new object [] { "1", "2", "3" });
  162. p.DataSource = new DataView (table);
  163. p.Columns.Add(e);
  164. e = new EditCommandColumn();
  165. e.ButtonType = ButtonColumnType.PushButton;
  166. e.CancelText = "Abbrechen";
  167. e.EditText = "Bearbeiten";
  168. e.UpdateText = "Refresh";
  169. p.Columns.Add(e);
  170. p.CreateControls (true);
  171. // This is the test we want to run: setting the ID of the table created by
  172. // the datagrid overrides the using the ID of the datagrid itself and uses
  173. // the table ClientID instead.
  174. p.ID = "sucker";
  175. p.Controls [0].ID = "tbl";
  176. Assert.AreEqual (2, p.Columns.Count, "I1");
  177. markup = ControlMarkup(p.Controls[0]);
  178. markup = markup.Replace("\t", "");
  179. markup = markup.Replace ("\r", "");
  180. markup = markup.Replace ("\n", "");
  181. #if NET_2_0
  182. HtmlDiff.AssertAreEqual (
  183. "<table border=\"0\"><tr><td>&nbsp;</td><td>&nbsp;</td><td>one</td><td>two</td><td>three</td>" +
  184. "</tr><tr><td><a>Edit</a></td><td><input name=\"sucker_tbl$ctl02$ctl00\" type=\"submit\" value=\"Bearbeiten\" /></td><td>1</td><td>2</td><td>3</td>" +
  185. "</tr><tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>" +
  186. "</tr></table>", markup, "I2");
  187. #else
  188. Assert.AreEqual (
  189. "<table border=\"0\" id=\"sucker_tbl\"><tr><td>&nbsp;</td><td>&nbsp;</td><td>one</td><td>two</td><td>three</td>" +
  190. "</tr><tr><td><a>Edit</a></td><td><input name=\"sucker:_ctl1:_ctl0\" type=\"submit\" value=\"Bearbeiten\" /></td><td>1</td><td>2</td><td>3</td>" +
  191. "</tr><tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>" +
  192. "</tr></table>", markup, "I2");
  193. #endif
  194. }
  195. static void GetHierarchy (ControlCollection coll, int level, StringBuilder sb)
  196. {
  197. foreach (Control c in coll) {
  198. sb.AppendFormat ("{0}{1}\n", new string (' ', 2 * level), c.GetType ());
  199. GetHierarchy (c.Controls, level + 1, sb);
  200. }
  201. }
  202. [Test]
  203. public void InitializeEditCell () {
  204. DataGridTest p = new DataGridTest ();
  205. DataTable table = new DataTable ();
  206. EditCommandColumn e;
  207. string markup;
  208. e = new EditCommandColumn();
  209. e.ButtonType = ButtonColumnType.LinkButton;
  210. e.CancelText = "Cancel";
  211. e.EditText = "Edit";
  212. e.UpdateText = "Update";
  213. table.Columns.Add (new DataColumn ("one", typeof (string)));
  214. table.Columns.Add (new DataColumn ("two", typeof (string)));
  215. table.Columns.Add (new DataColumn ("three", typeof (string)));
  216. table.Rows.Add (new object [] { "1", "2", "3" });
  217. p.DataSource = new DataView (table);
  218. p.Columns.Add(e);
  219. e = new EditCommandColumn();
  220. e.ButtonType = ButtonColumnType.PushButton;
  221. e.CancelText = "Abbrechen";
  222. e.EditText = "Bearbeiten";
  223. e.UpdateText = "Refresh";
  224. p.Columns.Add(e);
  225. // Force the ListItemType to be EditItem so we can test rendering the UpdateText/CancelText render
  226. p.EditItemIndex = 0;
  227. // This will trigger EditCommandColumn.InitializeCell
  228. p.CreateControls (true);
  229. p.ID = "sucker";
  230. StringBuilder sb = new StringBuilder ();
  231. GetHierarchy (p.Controls, 0, sb);
  232. string h = sb.ToString ();
  233. int x = h.IndexOf (".TextBox");
  234. // These are from the BoundColumns
  235. Assert.IsTrue (x != -1, "textbox1");
  236. x = h.IndexOf (".TextBox", x + 1);
  237. Assert.IsTrue (x != -1, "textbox2");
  238. x = h.IndexOf (".TextBox", x + 1);
  239. Assert.IsTrue (x != -1, "textbox3");
  240. x = h.IndexOf (".TextBox", x + 1);
  241. Assert.IsTrue (x == -1, "textbox-end");
  242. markup = ControlMarkup (p.Controls[0]);
  243. markup = markup.Replace ("\t", "");
  244. markup = markup.Replace ("\r", "");
  245. markup = markup.Replace ("\n", "");
  246. //Console.WriteLine("Markup:>{0}<", markup);
  247. Assert.AreEqual (2, p.Columns.Count, "I1");
  248. #if NET_2_0
  249. HtmlDiff.AssertAreEqual (
  250. "<table border=\"0\"><tr><td>&nbsp;</td><td>&nbsp;</td><td>one</td><td>two</td><td>three</td>" +
  251. "</tr><tr><td><a>Update</a>&nbsp;<a>Cancel</a></td><td><input name=\"sucker$ctl02$ctl00\" type=\"submit\" value=\"Refresh\" />&nbsp;" +
  252. "<input name=\"sucker$ctl02$ctl01\" type=\"submit\" value=\"Abbrechen\" /></td>" +
  253. "<td><input name=\"sucker$ctl02$ctl02\" type=\"text\" value=\"1\" /></td>" +
  254. "<td><input name=\"sucker$ctl02$ctl03\" type=\"text\" value=\"2\" /></td>" +
  255. "<td><input name=\"sucker$ctl02$ctl04\" type=\"text\" value=\"3\" /></td>" +
  256. "</tr><tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>" +
  257. "</tr></table>", markup, "I2");
  258. #else
  259. Assert.AreEqual (
  260. "<table border=\"0\" id=\"sucker\"><tr><td>&nbsp;</td><td>&nbsp;</td><td>one</td><td>two</td><td>three</td>" +
  261. "</tr><tr><td><a>Update</a>&nbsp;<a>Cancel</a></td><td><input name=\"sucker:_ctl1:_ctl0\" type=\"submit\" value=\"Refresh\" />&nbsp;" +
  262. "<input name=\"sucker:_ctl1:_ctl1\" type=\"submit\" value=\"Abbrechen\" /></td>" +
  263. "<td><input name=\"sucker:_ctl1:_ctl2\" type=\"text\" value=\"1\" /></td>" +
  264. "<td><input name=\"sucker:_ctl1:_ctl3\" type=\"text\" value=\"2\" /></td>" +
  265. "<td><input name=\"sucker:_ctl1:_ctl4\" type=\"text\" value=\"3\" /></td>" +
  266. "</tr><tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>" +
  267. "</tr></table>", markup, "I2");
  268. #endif
  269. }
  270. [Test]
  271. [Ignore("Unfinished")]
  272. public void InitializeReadOnlyEditCell ()
  273. {
  274. DataGridTest p = new DataGridTest ();
  275. DataTable table = new DataTable ();
  276. EditCommandColumn e;
  277. string markup;
  278. e = new EditCommandColumn ();
  279. e.ButtonType = ButtonColumnType.LinkButton;
  280. e.CancelText = "Cancel";
  281. e.EditText = "Edit";
  282. e.UpdateText = "Update";
  283. table.Columns.Add (new DataColumn ("one", typeof (string)));
  284. table.Columns.Add (new DataColumn ("two", typeof (string)));
  285. table.Columns.Add (new DataColumn ("three", typeof (string)));
  286. table.Rows.Add (new object[] { "1", "2", "3" });
  287. p.DataSource = new DataView (table);
  288. p.Columns.Add (e);
  289. e = new EditCommandColumn ();
  290. e.ButtonType = ButtonColumnType.PushButton;
  291. e.CancelText = "Abbrechen";
  292. e.EditText = "Bearbeiten";
  293. e.UpdateText = "Refresh";
  294. p.Columns.Add (e);
  295. // Force the ListItemType to be EditItem so we can test rendering the UpdateText/CancelText render
  296. p.EditItemIndex = 0;
  297. // This will trigger EditCommandColumn.InitializeCell
  298. p.CreateControls (true);
  299. p.ID = "sucker";
  300. markup = ControlMarkup (p.Controls[0]);
  301. markup = markup.Replace ("\t", "");
  302. markup = markup.Replace ("\r", "");
  303. markup = markup.Replace ("\n", "");
  304. Assert.AreEqual (2, p.Columns.Count, "I1");
  305. Assert.AreEqual (
  306. "<table border=\"0\" id=\"sucker\"><tr><td>&nbsp;</td><td>&nbsp;</td><td>one</td><td>two</td><td>three</td>" +
  307. "</tr><tr><td><a>Update</a>&nbsp;<a>Cancel</a></td><td><input name type=\"submit\" value=\"Refresh\" />&nbsp;" +
  308. "<input name value=\"Abbrechen\" type=\"submit\" /></td>" +
  309. "<td><input name=\"_ctl2:_ctl0\" type=\"text\" value=\"1\" /></td>" +
  310. "<td><input name=\"_ctl2:_ctl1\" type=\"text\" value=\"2\" /></td>" +
  311. "<td><input name=\"_ctl2:_ctl2\" type=\"text\" value=\"3\" /></td>" +
  312. "</tr><tr><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>" +
  313. "</tr></table>", markup, "I2");
  314. }
  315. #if NET_2_0
  316. [Test]
  317. [Category ("NunitWeb")]
  318. public void Validation_ValidatingValid ()
  319. {
  320. WebTest t = new WebTest ();
  321. PageDelegates pd = new PageDelegates ();
  322. pd.Load = Validation_Load;
  323. pd.PreRender = Validation_PreRender;
  324. t.Invoker = new PageInvoker (pd);
  325. t.UserData = "ValidatingValid";
  326. string html = t.Run ();
  327. FormRequest fr = new FormRequest (t.Response, "form1");
  328. fr.Controls.Add ("__EVENTTARGET");
  329. fr.Controls.Add ("__EVENTARGUMENT");
  330. fr.Controls ["__EVENTTARGET"].Value = (string) t.UserData;
  331. fr.Controls ["__EVENTARGUMENT"].Value = "";
  332. t.Request = fr;
  333. t.UserData = "ValidatingValid";
  334. html = t.Run ();
  335. }
  336. [Test]
  337. [Category ("NunitWeb")]
  338. public void Validation_ValidatingInvalid () {
  339. WebTest t = new WebTest ();
  340. PageDelegates pd = new PageDelegates ();
  341. pd.Load = Validation_Load;
  342. pd.PreRender = Validation_PreRender;
  343. t.Invoker = new PageInvoker (pd);
  344. t.UserData = "ValidatingInvalid";
  345. string html = t.Run ();
  346. FormRequest fr = new FormRequest (t.Response, "form1");
  347. fr.Controls.Add ("__EVENTTARGET");
  348. fr.Controls.Add ("__EVENTARGUMENT");
  349. fr.Controls ["__EVENTTARGET"].Value = (string)t.UserData;
  350. fr.Controls ["__EVENTARGUMENT"].Value = "";
  351. t.Request = fr;
  352. t.UserData = "ValidatingInvalid";
  353. html = t.Run ();
  354. }
  355. [Test]
  356. [Category ("NunitWeb")]
  357. public void Validation_NotValidatingInvalid () {
  358. WebTest t = new WebTest ();
  359. PageDelegates pd = new PageDelegates ();
  360. pd.Load = Validation_Load;
  361. pd.PreRender = Validation_PreRender;
  362. t.Invoker = new PageInvoker (pd);
  363. t.UserData = "NotValidatingInvalid";
  364. string html = t.Run ();
  365. FormRequest fr = new FormRequest (t.Response, "form1");
  366. fr.Controls.Add ("__EVENTTARGET");
  367. fr.Controls.Add ("__EVENTARGUMENT");
  368. fr.Controls ["__EVENTTARGET"].Value = (string) t.UserData;
  369. fr.Controls ["__EVENTARGUMENT"].Value = "";
  370. t.Request = fr;
  371. t.UserData = "NotValidatingInvalid";
  372. html = t.Run ();
  373. }
  374. [Test]
  375. [Category ("NunitWeb")]
  376. public void Validation_ValidationGroupIncluded () {
  377. WebTest t = new WebTest ();
  378. PageDelegates pd = new PageDelegates ();
  379. pd.Load = Validation_Load;
  380. pd.PreRender = Validation_PreRender;
  381. t.Invoker = new PageInvoker (pd);
  382. t.UserData = "ValidationGroupIncluded";
  383. string html = t.Run ();
  384. FormRequest fr = new FormRequest (t.Response, "form1");
  385. fr.Controls.Add ("__EVENTTARGET");
  386. fr.Controls.Add ("__EVENTARGUMENT");
  387. fr.Controls ["__EVENTTARGET"].Value = (string) t.UserData;
  388. fr.Controls ["__EVENTARGUMENT"].Value = "";
  389. t.Request = fr;
  390. t.UserData = "ValidationGroupIncluded";
  391. html = t.Run ();
  392. }
  393. [Test]
  394. [Category ("NunitWeb")]
  395. public void Validation_ValidationGroupNotIncluded () {
  396. WebTest t = new WebTest ();
  397. PageDelegates pd = new PageDelegates ();
  398. pd.Load = Validation_Load;
  399. pd.PreRender = Validation_PreRender;
  400. t.Invoker = new PageInvoker (pd);
  401. t.UserData = "ValidationGroupNotIncluded";
  402. string html = t.Run ();
  403. FormRequest fr = new FormRequest (t.Response, "form1");
  404. fr.Controls.Add ("__EVENTTARGET");
  405. fr.Controls.Add ("__EVENTARGUMENT");
  406. fr.Controls ["__EVENTTARGET"].Value = (string) t.UserData;
  407. fr.Controls ["__EVENTARGUMENT"].Value = "";
  408. t.Request = fr;
  409. t.UserData = "ValidationGroupNotIncluded";
  410. html = t.Run ();
  411. }
  412. public static void Validation_Load (Page p)
  413. {
  414. string testType = (string)WebTest.CurrentTest.UserData;
  415. DataGridTest dg = new DataGridTest ();
  416. dg.ID = "mygrid";
  417. EditCommandColumn e;
  418. e = new EditCommandColumn ();
  419. e.ButtonType = ButtonColumnType.LinkButton;
  420. e.CancelText = "Cancel";
  421. e.EditText = "Edit";
  422. e.UpdateText = "Update";
  423. switch (testType) {
  424. case "ValidatingValid":
  425. case "ValidatingInvalid":
  426. case "ValidationGroupIncluded":
  427. case "ValidationGroupNotIncluded":
  428. e.CausesValidation = true;
  429. break;
  430. case "NotValidatingInvalid":
  431. e.CausesValidation = false;
  432. break;
  433. }
  434. switch (testType) {
  435. case "ValidationGroupIncluded":
  436. case "ValidationGroupNotIncluded":
  437. e.ValidationGroup = "Group1";
  438. break;
  439. default:
  440. e.ValidationGroup = "";
  441. break;
  442. }
  443. dg.Columns.Add (e);
  444. TextBox tb = new TextBox ();
  445. tb.ID = "Text1";
  446. switch (testType) {
  447. case "ValidatingValid":
  448. tb.Text = "111";
  449. break;
  450. case "ValidatingInvalid":
  451. case "NotValidatingInvalid":
  452. case "ValidationGroupIncluded":
  453. case "ValidationGroupNotIncluded":
  454. tb.Text = "";
  455. break;
  456. }
  457. RequiredFieldValidator v = new RequiredFieldValidator ();
  458. v.ControlToValidate = "Text1";
  459. switch (testType) {
  460. case "ValidationGroupIncluded":
  461. v.ValidationGroup = "Group1";
  462. break;
  463. case "ValidationGroupNotIncluded":
  464. v.ValidationGroup = "NotGroup1";
  465. break;
  466. default:
  467. v.ValidationGroup = "";
  468. break;
  469. }
  470. TemplateColumn tc = new TemplateColumn ();
  471. tc.EditItemTemplate = new ValidatingEditTemplate (tb, v);
  472. dg.Columns.Add (tc);
  473. ObjectDataSource ods = new ObjectDataSource ("MyObjectDS", "Select");
  474. ods.UpdateMethod = "Update";
  475. ods.DataObjectTypeName = "MyObjectDS";
  476. ods.ID = "MyDS";
  477. p.Form.Controls.Add (ods);
  478. dg.DataSource = ods;
  479. //dg.DataKeyField = "i";
  480. //DataTable table = new DataTable ();
  481. //table.Columns.Add (new DataColumn ("one", typeof (string)));
  482. //table.Columns.Add (new DataColumn ("two", typeof (string)));
  483. //table.Columns.Add (new DataColumn ("three", typeof (string)));
  484. //table.Rows.Add (new object [] { "1", "2", "3" });
  485. //dg.DataSource = new DataView (table);
  486. dg.EditItemIndex = 0;
  487. p.Form.Controls.Add (dg);
  488. dg.DataBind ();
  489. if (!p.IsPostBack) {
  490. WebTest.CurrentTest.UserData = dg.Items [0].Cells [0].Controls [0].UniqueID;
  491. }
  492. }
  493. public static void Validation_PreRender (Page p)
  494. {
  495. string testType = (string) WebTest.CurrentTest.UserData;
  496. if (p.IsPostBack) {
  497. switch (testType) {
  498. case "ValidatingValid":
  499. case "ValidationGroupNotIncluded":
  500. Assert.AreEqual (true, p.IsValid, "ValidatingValid");
  501. break;
  502. case "ValidatingInvalid":
  503. case "ValidationGroupIncluded":
  504. Assert.AreEqual (false, p.IsValid, "ValidatingInvalid");
  505. break;
  506. case "NotValidatingInvalid":
  507. bool isValidated = true;
  508. try {
  509. if (p.IsValid) {
  510. Assert.Fail ("NotValidatingInvalid IsValid == true");
  511. }
  512. }
  513. catch (HttpException httpException) {
  514. isValidated = false;
  515. }
  516. Assert.AreEqual(false, isValidated, "NotValidatingInvalid");
  517. break;
  518. }
  519. }
  520. }
  521. public class ValidatingEditTemplate : ITemplate
  522. {
  523. public ValidatingEditTemplate (params Control [] templateControls)
  524. {
  525. this.templateControls = new Control[templateControls.Length];
  526. templateControls.CopyTo (this.templateControls, 0);
  527. }
  528. #region ITemplate Members
  529. public void InstantiateIn (Control container)
  530. {
  531. foreach (Control c in templateControls) {
  532. container.Controls.Add (c);
  533. }
  534. }
  535. #endregion
  536. private Control[] templateControls;
  537. }
  538. #endif
  539. }
  540. }
  541. #if NET_2_0
  542. #region MyObjectDS
  543. public class MyObjectDS
  544. {
  545. public MyObjectDS () {
  546. _i = 0;
  547. }
  548. public MyObjectDS (int value) {
  549. _i = value;
  550. }
  551. private int _i;
  552. public int i {
  553. get { return _i; }
  554. set { _i = value; }
  555. }
  556. static MyObjectDS [] myData = null;
  557. public static IList Select () {
  558. if (myData == null) {
  559. myData = new MyObjectDS [] { new MyObjectDS (1), new MyObjectDS (2), new MyObjectDS (3) };
  560. }
  561. return myData;
  562. }
  563. public static void Update (MyObjectDS instance) {
  564. }
  565. }
  566. #endregion
  567. #endif