default.aspx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 SortedList ProductInfo
  6. {
  7. get { return (SortedList)(ViewState["ProductInfo"] ?? new SortedList()); }
  8. set { ViewState["ProductInfo"] = value; }
  9. }
  10. protected void Category1Button_Click(object sender, EventArgs e)
  11. {
  12. SqlDataSource1.SelectParameters[0].DefaultValue = "1";
  13. }
  14. protected void Category2Button_Click(object sender, EventArgs e)
  15. {
  16. SqlDataSource1.SelectParameters[0].DefaultValue = "2";
  17. }
  18. protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
  19. {
  20. Label label = (Label)e.Item.FindControl("ProductIDLabel");
  21. Button button = (Button)e.Item.FindControl("Button1");
  22. TextBox textbox = (TextBox)e.Item.FindControl("TextBox1");
  23. button.OnClientClick = "GetQuantity(" + label.Text + ",'" +
  24. textbox.ClientID + "','" + label.ClientID + "','" + button.ClientID + "')";
  25. SortedList ProductInfo = this.ProductInfo;
  26. if (ProductInfo.ContainsKey(label.Text))
  27. {
  28. textbox.Text = ProductInfo[label.Text].ToString();
  29. }
  30. }
  31. protected void Page_Load(object sender, EventArgs e)
  32. {
  33. if (ScriptManager1.IsInAsyncPostBack)
  34. {
  35. SortedList ProductInfo = this.ProductInfo;
  36. foreach (DataListItem d in DataList1.Items)
  37. {
  38. Label label = (Label)d.FindControl("ProductIDLabel");
  39. TextBox textbox = (TextBox)d.FindControl("TextBox1");
  40. if (textbox.Text.Length > 0)
  41. {
  42. ProductInfo[label.Text] = textbox.Text;
  43. }
  44. }
  45. this.ProductInfo = ProductInfo;
  46. }
  47. }
  48. </script>
  49. <html xmlns="http://www.w3.org/1999/xhtml">
  50. <head runat="server">
  51. <title>Products Display</title>
  52. </head>
  53. <body>
  54. <form id="form1" runat="server">
  55. <div>
  56. <asp:ScriptManager ID="ScriptManager1" runat="server">
  57. <Services>
  58. <asp:ServiceReference Path="ProductQueryService.asmx" />
  59. </Services>
  60. <Scripts>
  61. <asp:ScriptReference Path="ProductQueryScript.js" />
  62. </Scripts>
  63. </asp:ScriptManager>
  64. <asp:UpdatePanel ID="UpdatePanel1" runat="server" ChildrenAsTriggers="False" UpdateMode="Conditional">
  65. <ContentTemplate>
  66. <asp:Button ID="Category1Button" runat="server" Text="Category 1" OnClick="Category1Button_Click" />
  67. <asp:Button ID="Category2Button" runat="server" OnClick="Category2Button_Click" Text="Category 2" />
  68. <asp:DataList ID="DataList1" runat="server" DataKeyField="ProductID" DataSourceID="SqlDataSource1"
  69. Width="400px" OnItemDataBound="DataList1_ItemDataBound">
  70. <ItemTemplate>
  71. ProductName:
  72. <asp:Label ID="ProductNameLabel" runat="server" Text='<%# Eval("ProductName") %>'>
  73. </asp:Label><br />
  74. ProductID:
  75. <asp:Label ID="ProductIDLabel" runat="server" Text='<%# Eval("ProductID") %>'></asp:Label><br />
  76. <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
  77. <asp:Button ID="Button1" runat="server" Text="Get Quantity from Web Service" /><br />
  78. </ItemTemplate>
  79. </asp:DataList>
  80. <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
  81. SelectCommand="SELECT [ProductName], [ProductID] FROM [Alphabetical list of products] WHERE ([CategoryID] = @CategoryID)">
  82. <SelectParameters>
  83. <asp:Parameter DefaultValue="1" Name="CategoryID" Type="Int32" />
  84. </SelectParameters>
  85. </asp:SqlDataSource>
  86. </ContentTemplate>
  87. <Triggers>
  88. <asp:AsyncPostBackTrigger ControlID="Category1Button" />
  89. <asp:AsyncPostBackTrigger ControlID="Category2Button" />
  90. </Triggers>
  91. </asp:UpdatePanel>
  92. </div>
  93. </form>
  94. </body>
  95. </html>