Default.aspx 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <%@ Page Language="C#" %>
  2. <%@ Import Namespace="System.Web.Script.Serialization" %>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4. <script runat="server">
  5. JavaScriptSerializer serializer;
  6. protected void Page_Load(object sender, EventArgs e)
  7. {
  8. serializer = new JavaScriptSerializer();
  9. // Register the custom converter.
  10. serializer.RegisterConverters(new JavaScriptConverter[] {
  11. new System.Web.Script.Serialization.CS.ListItemCollectionConverter() });
  12. this.SetFocus(TextBox1);
  13. }
  14. protected void saveButton_Click(object sender, EventArgs e)
  15. {
  16. // Save the current state of the ListBox control.
  17. SavedState.Text = serializer.Serialize(ListBox1.Items);
  18. recoverButton.Enabled = true;
  19. Message.Text = "State saved";
  20. }
  21. protected void recoverButton_Click(object sender, EventArgs e)
  22. {
  23. //Recover the saved items of the ListBox control.
  24. ListItemCollection recoveredList = serializer.Deserialize<ListItemCollection>(SavedState.Text);
  25. ListItem[] newListItemArray = new ListItem[recoveredList.Count];
  26. recoveredList.CopyTo(newListItemArray, 0);
  27. ListBox1.Items.Clear();
  28. ListBox1.Items.AddRange(newListItemArray);
  29. Message.Text = "Last saved state recovered.";
  30. }
  31. protected void clearButton_Click(object sender, EventArgs e)
  32. {
  33. // Remove all items from the ListBox control.
  34. ListBox1.Items.Clear();
  35. Message.Text = "All items removed";
  36. }
  37. protected void ContactsGrid_SelectedIndexChanged(object sender, EventArgs e)
  38. {
  39. // Get the currently selected row using the SelectedRow property.
  40. GridViewRow row = ContactsGrid.SelectedRow;
  41. // Get the ID of item selected.
  42. string itemId = ContactsGrid.DataKeys[row.RowIndex].Value.ToString();
  43. ListItem newItem = new ListItem(row.Cells[4].Text, itemId);
  44. // Check if the item already exists in the ListBox control.
  45. if (!ListBox1.Items.Contains(newItem))
  46. {
  47. // Add the item to the ListBox control.
  48. ListBox1.Items.Add(newItem);
  49. Message.Text = "Item added";
  50. }
  51. else
  52. Message.Text = "Item already exists";
  53. }
  54. protected void ContactsGrid_PageIndexChanged(object sender, EventArgs e)
  55. {
  56. //Reset the selected index.
  57. ContactsGrid.SelectedIndex = -1;
  58. }
  59. protected void searchButton_Click(object sender, EventArgs e)
  60. {
  61. //Reset indexes.
  62. ContactsGrid.SelectedIndex = -1;
  63. ContactsGrid.PageIndex = 0;
  64. //Set focus on the TextBox control.
  65. ScriptManager1.SetFocus(TextBox1);
  66. }
  67. protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
  68. {
  69. // Show/hide the saved state string.
  70. SavedState.Visible = CheckBox1.Checked;
  71. StateLabel.Visible = CheckBox1.Checked;
  72. }
  73. </script>
  74. <html xmlns="http://www.w3.org/1999/xhtml" >
  75. <head runat="server">
  76. <title>Save/Recover state</title>
  77. <style type="text/css">
  78. body { font: 11pt Trebuchet MS;
  79. font-color: #000000;
  80. padding-top: 72px;
  81. text-align: center }
  82. .text { font: 8pt Trebuchet MS }
  83. </style>
  84. </head>
  85. <body>
  86. <form id="form1" runat="server" defaultbutton="searchButton" defaultfocus="TextBox1">
  87. <h3>
  88. <span style="text-decoration: underline">
  89. Contacts Selection</span><br />
  90. </h3>
  91. <asp:ScriptManager runat="server" ID="ScriptManager1" />
  92. <asp:UpdatePanel ID="UpdatePanel1" runat="server">
  93. <ContentTemplate>
  94. Type contact's first name:
  95. <asp:TextBox ID="TextBox1" runat="server" />
  96. <asp:Button ID="searchButton" runat="server" Text="Search" OnClick="searchButton_Click" />&nbsp;
  97. <br />
  98. <br />
  99. <table border="0" width="100%">
  100. <tr>
  101. <td style="width:50%" valign="top" align="center">
  102. <b>Search results:</b><br />
  103. <asp:GridView ID="ContactsGrid" runat="server" AutoGenerateColumns="False"
  104. CellPadding="4" DataKeyNames="ContactID" DataSourceID="SqlDataSource1"
  105. OnSelectedIndexChanged="ContactsGrid_SelectedIndexChanged" ForeColor="#333333" GridLines="None" AllowPaging="True" PageSize="7" OnPageIndexChanged="ContactsGrid_PageIndexChanged">
  106. <Columns>
  107. <asp:CommandField ShowSelectButton="True" ButtonType="Button" />
  108. <asp:BoundField DataField="ContactID" HeaderText="ContactID" SortExpression="ContactID" Visible="False" />
  109. <asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />
  110. <asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
  111. <asp:BoundField DataField="EmailAddress" HeaderText="EmailAddress" SortExpression="EmailAddress" />
  112. </Columns>
  113. <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
  114. <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
  115. <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
  116. <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
  117. <EditRowStyle BackColor="#999999" />
  118. <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
  119. <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
  120. <EmptyDataTemplate>No data found.</EmptyDataTemplate>
  121. </asp:GridView>
  122. <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:AdventureWorksConnectionString %>"
  123. SelectCommand="SELECT ContactID, FirstName, LastName, EmailAddress FROM Person.Contact WHERE (UPPER(FirstName) = UPPER(@FIRSTNAME))" >
  124. <SelectParameters>
  125. <asp:ControlParameter Name="FIRSTNAME" ControlId="TextBox1" Type="String" />
  126. </SelectParameters>
  127. </asp:SqlDataSource>
  128. </td>
  129. <td valign="top">
  130. <b>Contacts list:</b><br />
  131. <asp:ListBox ID="ListBox1" runat="server" Height="200px" Width="214px" /><br />
  132. <asp:Button ID="saveButton" runat="server" Text="Save state" OnClick="saveButton_Click" ToolTip="Save the current state of the list" />
  133. <asp:Button ID="recoverButton" runat="server" Text="Recover saved state" OnClick="recoverButton_Click" Enabled="false" ToolTip="Recover the last saved state" />
  134. <asp:Button ID="clearButton" runat="server" Text="Clear" OnClick="clearButton_Click" ToolTip="Remove all items from the list" /><br />
  135. <br />
  136. <asp:CheckBox ID="CheckBox1" runat="server" Checked="True" OnCheckedChanged="CheckBox1_CheckedChanged"
  137. Text="Show saved state" AutoPostBack="True" /></td>
  138. </tr>
  139. <tr>
  140. <td colspan="2">
  141. <br />
  142. <br />
  143. &nbsp;&nbsp;
  144. <hr />
  145. Message: <asp:Label ID="Message" runat="server" ForeColor="SteelBlue" />&nbsp;&nbsp;<br />
  146. <asp:Label ID="StateLabel" runat="server" Text="State:"></asp:Label>
  147. <asp:Label ID="SavedState" runat="server"/><br />
  148. </td>
  149. </tr>
  150. </table>
  151. </ContentTemplate>
  152. </asp:UpdatePanel>
  153. <asp:UpdateProgress ID="UpdateProgress1" runat="server">
  154. <ProgressTemplate>
  155. <asp:Image ID="Image1" runat="server" ImageUrl="..\images\spinner.gif" />&nbsp;Processing...
  156. </ProgressTemplate>
  157. </asp:UpdateProgress>
  158. </form>
  159. </body>
  160. </html>