SearchResultsWithUpdatePanelCS.aspx 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <%@ Page Language="C#" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  3. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4. <script runat="server">
  5. protected void Search_Click(object sender, EventArgs e)
  6. {
  7. SqlDataSource1.SelectParameters["SearchTerm"].DefaultValue =
  8. Server.HtmlEncode(SearchField.Text);
  9. Label1.Text = "Searching for '" +
  10. Server.HtmlEncode(SearchField.Text) + "'";
  11. }
  12. protected void ExampleProductSearch_Click(object sender, EventArgs e)
  13. {
  14. SqlDataSource1.SelectParameters["SearchTerm"].DefaultValue =
  15. Server.HtmlEncode(ExampleProductSearch.Text);
  16. Label1.Text = "Searching for '" +
  17. Server.HtmlEncode(ExampleProductSearch.Text) + "'";
  18. SearchField.Text = ExampleProductSearch.Text;
  19. UpdatePanel2.Update();
  20. }
  21. </script>
  22. <html xmlns="http://www.w3.org/1999/xhtml">
  23. <head id="Head1" runat="server">
  24. <title>UpdatePanel Trigger Example</title>
  25. <style type="text/css">
  26. body {
  27. font-family: Lucida Sans Unicode;
  28. font-size: 10pt;
  29. }
  30. button {
  31. font-family: tahoma;
  32. font-size: 8pt;
  33. }
  34. </style>
  35. </head>
  36. <body>
  37. <form id="form1" runat="server"
  38. defaultbutton="SearchButton" defaultfocus="SearchField">
  39. <div>
  40. <asp:ScriptManager ID="ScriptManager1" runat="server" />
  41. Search for products in the Northwind database. For example,
  42. find products with
  43. <asp:LinkButton ID="ExampleProductSearch" Text="Louisiana" runat="server" OnClick="ExampleProductSearch_Click">
  44. </asp:LinkButton> in the title.&nbsp;<br /><br />
  45. <asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
  46. <ContentTemplate>
  47. <asp:TextBox ID="SearchField" runat="server"></asp:TextBox>
  48. <asp:Button ID="SearchButton" Text="Submit" OnClick="Search_Click"
  49. runat="server" />
  50. </ContentTemplate>
  51. </asp:UpdatePanel>
  52. <fieldset>
  53. <legend>Northwind Products</legend>
  54. <asp:UpdatePanel ID="UpdatePanel1"
  55. runat="server">
  56. <Triggers>
  57. <asp:AsyncPostBackTrigger ControlID="SearchButton" />
  58. <asp:AsyncPostBackTrigger ControlID="ExampleProductSearch" />
  59. </Triggers>
  60. <ContentTemplate>
  61. <asp:Label ID="Label1" runat="server"/>
  62. <br />
  63. <asp:GridView ID="GridView1" runat="server" AllowPaging="True"
  64. AllowSorting="True" DataSourceID="SqlDataSource1">
  65. <EmptyDataTemplate>
  66. No results to display.
  67. </EmptyDataTemplate>
  68. </asp:GridView>
  69. <asp:SqlDataSource ID="SqlDataSource1" runat="server"
  70. ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
  71. SelectCommand="SELECT [ProductName], [UnitsInStock] FROM
  72. [Alphabetical list of products] WHERE ([ProductName] LIKE
  73. '%' + @SearchTerm + '%')">
  74. <SelectParameters>
  75. <asp:Parameter Name="SearchTerm" Type="String" />
  76. </SelectParameters>
  77. </asp:SqlDataSource>
  78. </ContentTemplate>
  79. </asp:UpdatePanel>
  80. </fieldset>
  81. </div>
  82. </form>
  83. </body>
  84. </html>