Advertisement
Guest User

Untitled

a guest
Dec 31st, 2015
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.49 KB | None | 0 0
  1. [DefaultProperty("Contacts"),
  2. ParseChildren(true, "Contacts"),
  3. ToolboxData("<{0}:QuickContacts runat=server></{0}:QuickContacts>")]
  4. public class QuickContacts : WebControl
  5. {
  6. private List<Contact> contactsList;
  7.  
  8. [Category("Behavior"),
  9. Description("The contacts collection."),
  10. DesignerSerializationVisibility(
  11. DesignerSerializationVisibility.Content),
  12. PersistenceMode(PersistenceMode.InnerDefaultProperty)]
  13. public List<Contact> Contacts
  14. {
  15. get
  16. {
  17. if (contactsList == null)
  18. {
  19. contactsList = new List<Contact>();
  20. }
  21. return contactsList;
  22. }
  23. }
  24.  
  25. public class ContactList<T> : IList<T>, IStateManager
  26. {
  27.  
  28. [DefaultProperty("Contacts"),
  29. ParseChildren(true, "Contacts"),
  30. ToolboxData("<{0}:QuickContacts runat=server></{0}:QuickContacts>")]
  31. public class QuickContacts : WebControl
  32. {
  33. private ContactList<Contact> contactsList;
  34.  
  35. [Category("Behavior"),
  36. Description("The contacts collection."),
  37. DesignerSerializationVisibility(
  38. DesignerSerializationVisibility.Content),
  39. PersistenceMode(PersistenceMode.InnerDefaultProperty)]
  40. public ContactList<Contact> Contacts
  41. {
  42. get
  43. {
  44. if (contactsList == null)
  45. {
  46. contactsList = new ContactList<Contact>();
  47. }
  48. return contactsList;
  49. }
  50. }
  51.  
  52. <asp:Content ID="Content2" ContentPlaceHolderID="Main" runat="server">
  53. <cc1:QuickContacts ID="QuickContacts1" runat="server" BorderStyle="Solid" BorderWidth="1px">
  54. <cc1:Contact Name="someone" Email="someone@example.com"
  55. Phone="(555) 555-0100" />
  56.  
  57. [DebuggerTypeProxy(typeof(Mscorlib_CollectionDebugView<>))]
  58. [DebuggerDisplay("Count = {Count}")]
  59. [Serializable]
  60. public class List<T> : IList<T>, System.Collections.IList, IReadOnlyList<T>
  61. {
  62.  
  63. [DefaultProperty("Contacts"),
  64. ParseChildren(true, ChildrenAsProperties = true, DefaultProperty = "Contacts"),
  65. ToolboxData("<{0}:QuickContacts runat=server></{0}:QuickContacts>")]
  66. public class QuickContacts : WebControl
  67. {
  68. private StateManagedCollection<Contact> contactsList;
  69.  
  70. [Category("Behavior"),
  71. Description("The contacts collection."),
  72. DesignerSerializationVisibility(
  73. DesignerSerializationVisibility.Content),
  74. PersistenceMode(PersistenceMode.InnerDefaultProperty)]
  75. public StateManagedCollection<Contact> Contacts
  76. {
  77. get
  78. {
  79. if (contactsList == null)
  80. {
  81. contactsList = new StateManagedCollection<Contact>();
  82. }
  83. return contactsList;
  84. }
  85. }
  86.  
  87. protected override void RenderContents(HtmlTextWriter writer)
  88. {
  89.  
  90. Table t = CreateContactsTable();
  91. if (t != null)
  92. {
  93. t.RenderControl(writer);
  94. }
  95. }
  96.  
  97. private Table CreateContactsTable()
  98. {
  99. Table t = null;
  100.  
  101. if (contactsList != null && contactsList.Count > 0)
  102. {
  103. t = new Table();
  104.  
  105. foreach (Contact item in contactsList)
  106. {
  107. Contact aContact = item as Contact;
  108.  
  109. if (aContact != null)
  110. {
  111. TableRow row = new TableRow();
  112.  
  113. TableCell c1 = new TableCell();
  114. c1.Text = aContact.Name;
  115. row.Controls.Add(c1);
  116.  
  117. TableCell c2 = new TableCell();
  118. c2.Text = aContact.Email;
  119. row.Controls.Add(c2);
  120.  
  121. TableCell c3 = new TableCell();
  122. c3.Text = aContact.Phone;
  123. row.Controls.Add(c3);
  124.  
  125. t.Controls.Add(row);
  126. }
  127. }
  128. }
  129.  
  130. return t;
  131. }
  132.  
  133. protected override void LoadViewState(object savedState)
  134. {
  135. if (savedState != null)
  136. {
  137. Pair p = savedState as Pair;
  138. base.LoadViewState(p.First);
  139. contactsList.LoadViewState(p.Second);
  140. }
  141. }
  142.  
  143. protected override object SaveViewState()
  144. {
  145. Pair p = new Pair(base.SaveViewState(), contactsList.SaveViewState());
  146. return p;
  147. }
  148.  
  149. protected override void TrackViewState()
  150. {
  151. base.TrackViewState();
  152. contactsList.TrackViewState();
  153. }
  154. }
  155.  
  156. public class StateManagedCollection<T> : IList<T>, System.Collections.IList, IReadOnlyList<T>, IStateManager
  157. where T : StateManagedClass, new()
  158. {
  159. private List<T> lst = default(List<T>);
  160. private Boolean isTrackingViewState = default(Boolean);
  161. private Boolean saveAll = default(Boolean);
  162.  
  163. public StateManagedCollection()
  164. {
  165. lst = new List<T>();
  166. isTrackingViewState = false;
  167. saveAll = false;
  168. }
  169.  
  170. public void SetMarked()
  171. {
  172. for (int i = 0; i < lst.Count; i++)
  173. {
  174. lst[i].SetMarked();
  175. }
  176. }
  177.  
  178. public bool IsTrackingViewState
  179. {
  180. get { return isTrackingViewState; }
  181. }
  182.  
  183. public void LoadViewState(object state)
  184. {
  185. if (state != null)
  186. {
  187.  
  188. if (state is List<Object>)
  189. {
  190. // all items were saved
  191.  
  192. List<Object> allItems = (List<Object>)state;
  193. saveAll = true;
  194. Int32 count = lst.Count;
  195. for (int i = 0; i < allItems.Count; i++)
  196. {
  197. if (i < count)
  198. lst[i].LoadViewState(allItems[i]);
  199. else
  200. {
  201. T item = new T();
  202. item.LoadViewState(allItems[i]);
  203. lst.Add(item);
  204. }
  205. }
  206. }
  207. else if (state is Dictionary<Int32, Object>)
  208. {
  209. Dictionary<Int32, Object> changedItems = (Dictionary<Int32, Object>)state;
  210. foreach (KeyValuePair<Int32, Object> item in changedItems)
  211. {
  212. if (item.Key < lst.Count)
  213. lst[item.Key].LoadViewState(item.Value);
  214. }
  215. }
  216. }
  217. }
  218.  
  219. public object SaveViewState()
  220. {
  221. if (saveAll)
  222. {
  223. List<Object> allItems = new List<Object>();
  224. foreach (var item in lst)
  225. {
  226. item.SetMarked();
  227. allItems.Add(item.SaveViewState());
  228. }
  229. return allItems;
  230. }
  231. else
  232. {
  233. Dictionary<Int32, Object> changedItems = new Dictionary<Int32, Object>();
  234. for (int i = 0; i < lst.Count; i++)
  235. {
  236. Object state = lst[i].SaveViewState();
  237. if (state != null)
  238. {
  239. changedItems.Add(i, state);
  240. }
  241. }
  242. return changedItems;
  243. }
  244. }
  245.  
  246. public void TrackViewState()
  247. {
  248. isTrackingViewState = true;
  249. for (int i = 0; i < lst.Count; i++)
  250. {
  251. lst[i].TrackViewState();
  252. }
  253. }
  254.  
  255. public int IndexOf(T item)
  256. {
  257. return lst.IndexOf(item);
  258. }
  259.  
  260. public void Insert(int index, T item)
  261. {
  262. lst.Insert(index, item);
  263. if (isTrackingViewState)
  264. saveAll = true;
  265. }
  266.  
  267. public void RemoveAt(int index)
  268. {
  269. lst.RemoveAt(index);
  270. if (isTrackingViewState)
  271. saveAll = true;
  272. }
  273.  
  274. public T this[int index]
  275. {
  276. get
  277. {
  278. return lst[index];
  279. }
  280. set
  281. {
  282. lst[index] = value;
  283. }
  284. }
  285.  
  286. public void Add(T item)
  287. {
  288. lst.Add(item);
  289. if (isTrackingViewState)
  290. saveAll = true;
  291. }
  292.  
  293. public void Clear()
  294. {
  295. lst.Clear();
  296. if (isTrackingViewState)
  297. saveAll = true;
  298. }
  299.  
  300. public bool Contains(T item)
  301. {
  302. return lst.Contains(item);
  303. }
  304.  
  305. public void CopyTo(T[] array, int arrayIndex)
  306. {
  307. lst.CopyTo(array, arrayIndex);
  308. }
  309.  
  310. public int Count
  311. {
  312. get { return lst.Count; }
  313. }
  314.  
  315. public bool IsReadOnly
  316. {
  317. get { return false; }
  318. }
  319.  
  320. public bool Remove(T item)
  321. {
  322. Boolean rslt = lst.Remove(item);
  323. if (isTrackingViewState)
  324. saveAll = true;
  325. return rslt;
  326. }
  327.  
  328. public IEnumerator<T> GetEnumerator()
  329. {
  330. return lst.GetEnumerator();
  331. }
  332.  
  333. IEnumerator IEnumerable.GetEnumerator()
  334. {
  335. return GetEnumerator();
  336. }
  337.  
  338. public int Add(object value)
  339. {
  340. Add((T)value);
  341. return Count - 1;
  342. }
  343.  
  344. public bool Contains(object value)
  345. {
  346. return Contains((T)value);
  347. }
  348.  
  349. public int IndexOf(object value)
  350. {
  351. return IndexOf((T)value);
  352. }
  353.  
  354. public void Insert(int index, object value)
  355. {
  356. Insert(index, (T)value);
  357. }
  358.  
  359. public bool IsFixedSize
  360. {
  361. get { return ((IList)lst).IsFixedSize; }
  362. }
  363.  
  364. public void Remove(object value)
  365. {
  366. Remove((T)value);
  367. }
  368.  
  369. object IList.this[int index]
  370. {
  371. get
  372. {
  373. return lst[index];
  374. }
  375. set
  376. {
  377. lst[index] = (T)value;
  378. }
  379. }
  380.  
  381. public void CopyTo(Array array, int index)
  382. {
  383. CopyTo((T[])array, index);
  384. }
  385.  
  386. public bool IsSynchronized
  387. {
  388. get { return ((ICollection)lst).IsSynchronized; }
  389. }
  390.  
  391. public object SyncRoot
  392. {
  393. get { return ((ICollection)lst).SyncRoot; }
  394. }
  395. }
  396.  
  397. public abstract class StateManagedClass : IStateManager
  398. {
  399. private Boolean isTrackingViewState = default(Boolean);
  400. private StateBag viewState = default(StateBag);
  401.  
  402. public StateManagedClass()
  403. {
  404. isTrackingViewState = false;
  405. viewState = new StateBag(false);
  406. }
  407.  
  408. public virtual StateBag ViewState
  409. {
  410. get
  411. {
  412. return viewState;
  413. }
  414. }
  415.  
  416. public bool IsTrackingViewState
  417. {
  418. get { return isTrackingViewState; }
  419. }
  420.  
  421. public void LoadViewState(object state)
  422. {
  423. if (state != default(object))
  424. {
  425. ((IStateManager)viewState).LoadViewState(state);
  426. }
  427. }
  428.  
  429. public object SaveViewState()
  430. {
  431. Object savedState = default(Object);
  432.  
  433. if (viewState != null)
  434. {
  435. savedState =
  436. ((IStateManager)viewState).SaveViewState();
  437. }
  438.  
  439. return savedState;
  440. }
  441.  
  442. public void TrackViewState()
  443. {
  444. isTrackingViewState = true;
  445.  
  446. if (viewState != default(StateBag))
  447. {
  448. ((IStateManager)viewState).TrackViewState();
  449. }
  450. }
  451.  
  452. public void SetMarked()
  453. {
  454. viewState.SetDirty(true);
  455. }
  456. }
  457.  
  458. public class Contact : StateManagedClass
  459. {
  460. [
  461. Category("Behavior"),
  462. DefaultValue(""),
  463. Description("Name of contact"),
  464. NotifyParentProperty(true)
  465. ]
  466. public String Name
  467. {
  468. get
  469. {
  470. Object s = ViewState["Name"];
  471. return (s == null) ? String.Empty : (String)s;
  472. }
  473. set
  474. {
  475. ViewState["Name"] = value;
  476. }
  477. }
  478.  
  479. [
  480. Category("Behavior"),
  481. DefaultValue(""),
  482. Description("Email address of contact"),
  483. NotifyParentProperty(true)
  484. ]
  485. public String Email
  486. {
  487. get
  488. {
  489. Object s = ViewState["Email"];
  490. return (s == null) ? String.Empty : (String)s;
  491. }
  492. set
  493. {
  494. ViewState["Email"] = value;
  495. }
  496. }
  497.  
  498. [
  499. Category("Behavior"),
  500. DefaultValue(""),
  501. Description("Phone number of contact"),
  502. NotifyParentProperty(true)
  503. ]
  504. public String Phone
  505. {
  506. get
  507. {
  508. Object s = ViewState["Phone"];
  509. return (s == null) ? String.Empty : (String)s;
  510. }
  511. set
  512. {
  513. ViewState["Phone"] = value;
  514. }
  515. }
  516.  
  517. }
  518.  
  519. <%@ Page Title="" Language="C#" MasterPageFile="~/RTL.Master" AutoEventWireup="true" CodeBehind="WebForm15.aspx.cs" Inherits="Controls.WebForm15" %>
  520.  
  521. <%@ Register Assembly="Controls" Namespace="Controls" TagPrefix="cc1" %>
  522.  
  523. <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
  524. </asp:Content>
  525. <asp:Content ID="Content2" ContentPlaceHolderID="Main" runat="server">
  526. <cc1:QuickContacts ID="QuickContacts1" runat="server" BorderStyle="Solid" BorderWidth="1px">
  527. <cc1:Contact Name="someone" Email="someone@example.com"
  528. Phone="(555) 555-0100" />
  529. <cc1:Contact Name="jae" Email="jae@fourthcoffee.com"
  530. Phone="(555) 555-0101" />
  531. <cc1:Contact Name="lene" Email="lene@contoso.com"
  532. Phone="(555) 555-0102" />
  533. </cc1:QuickContacts>
  534. <br />
  535. <asp:Button runat="server" ID="Button1" Text="Add" OnClick="Button1_Click"></asp:Button>
  536. <asp:Button runat="server" Text="Refresh"></asp:Button>
  537. <br />
  538. <br />
  539. <asp:HyperLink ID="HyperLink1" NavigateUrl="~/WebForm15.aspx"
  540. runat="server">
  541. Reload Page</asp:HyperLink>
  542. </asp:Content>
  543. <asp:Content ID="Content3" ContentPlaceHolderID="script" runat="server">
  544. </asp:Content>
  545.  
  546. using System;
  547. using System.Collections.Generic;
  548. using System.Linq;
  549. using System.Web;
  550. using System.Web.UI;
  551. using System.Web.UI.WebControls;
  552.  
  553. namespace Controls
  554. {
  555. public partial class WebForm15 : System.Web.UI.Page
  556. {
  557. protected void Page_Load(object sender, EventArgs e)
  558. {
  559.  
  560. }
  561.  
  562. protected void Button1_Click(object sender, EventArgs e)
  563. {
  564. SkolaControlsWorkings.Controls.Contact contact = new SkolaControlsWorkings.Controls.Contact();
  565. contact.Name = "Name";
  566. contact.Email = "Email@mai.com";
  567. contact.Phone = "(111) 111-1111";
  568. QuickContacts1.Contacts.Add(contact);
  569. Button1.Visible = false;
  570. }
  571. }
  572. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement