home *** CD-ROM | disk | FTP | other *** search
/ PC World 2003 September / PCWorld_2003-09_cd.bin / Software / Vyzkuste / webmatrix / WebMatrix.msi / Data1.cab / _5729A55DDA1B472A995A157A0144C56A < prev    next >
Encoding:
ASP.NET Web Form  |  2003-04-28  |  3.2 KB  |  81 lines

  1. <%@ Page language="C#"%%ClassName, ClassName="{0}"%% %>
  2. <%@ Import Namespace="System.Data" %>
  3. <%@ Import Namespace="System.Data.SqlClient" %>
  4.  
  5. <script runat="server">
  6.  
  7.     void Page_Load(object sender, EventArgs e) {
  8.  
  9.         if (!Page.IsPostBack) {
  10.     
  11.             // Databind the filter dropdown on the first request only
  12.             // (viewstate will restore these values on subsequent postbacks).
  13.     
  14.             // TODO: update the ConnectionString and CommandText values for your application
  15.             string ConnectionString = "server=(local);database=pubs;trusted_connection=true";
  16.             string CommandText = "select distinct au_lname from Authors";
  17.     
  18.             SqlConnection myConnection = new SqlConnection(ConnectionString);
  19.             SqlCommand myCommand = new SqlCommand(CommandText, myConnection);
  20.     
  21.             // TODO: Update the DataTextField value
  22.             DropDownList1.DataTextField = "au_lname";
  23.     
  24.             myConnection.Open();
  25.     
  26.             DropDownList1.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
  27.             DropDownList1.DataBind();
  28.     
  29.             // insert an "All" item at the beginning of the list
  30.             DropDownList1.Items.Insert(0, "-- All Authors --");
  31.         }
  32.     }
  33.     
  34.     void ApplyFilter_Click(Object sender, EventArgs e) {
  35.     
  36.         // TODO: update the ConnectionString value for your application
  37.         string ConnectionString = "server=(local);database=pubs;trusted_connection=true";
  38.         string CommandText;
  39.     
  40.         // get the filter value from the DropDownList
  41.         string filterValue = DropDownList1.SelectedItem.Text.Replace("'", "''");
  42.     
  43.         // TODO: update the CommandText value for your application
  44.         if (filterValue == "-- All Authors --")
  45.             CommandText = "select distinct title as Title, price as Price, ytd_sales as [YTD Sales] from titleview";
  46.         else
  47.             CommandText = "select title as Title, price as Price, ytd_sales as [YTD Sales] from titleview where au_lname = '" + filterValue + "'";
  48.     
  49.         SqlConnection myConnection = new SqlConnection(ConnectionString);
  50.         SqlCommand myCommand = new SqlCommand(CommandText, myConnection);
  51.     
  52.         myConnection.Open();
  53.     
  54.         DataGrid1.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
  55.         DataGrid1.DataBind();
  56.     }
  57.  
  58.  
  59. </script>
  60.     
  61. <html>
  62.     <body style="font-family:arial">
  63.         <h2>
  64.             Filtered Data Report
  65.         </h2>
  66.         <hr size="1">
  67.         <form runat="server">
  68.             <p>
  69.                 Select an Author: 
  70.                 <asp:DropDownList id="DropDownList1" runat="server"></asp:DropDownList>
  71.                  
  72.                 <asp:Button id="Button1" onclick="ApplyFilter_Click" Text="Show Titles" runat="server"></asp:Button>
  73.             </p>
  74.             <asp:datagrid id="DataGrid1" EnableViewState="False" runat="server" ForeColor="Black" BackColor="White" CellPadding="3" GridLines="None" CellSpacing="1">
  75.                 <HeaderStyle Font-Bold="True" ForeColor="white" BackColor="#4A3C8C"></HeaderStyle>
  76.                 <ItemStyle BackColor="#DEDFDE"></ItemStyle>
  77.             </asp:datagrid>
  78.         </form>
  79.     </body>
  80. </html>
  81.