home *** CD-ROM | disk | FTP | other *** search
/ PC World 2003 September / PCWorld_2003-09_cd.bin / Software / Vyzkuste / webmatrix / WebMatrix.msi / Data1.cab / _73488959BB1349D3ABAD72898CE724BE < prev    next >
Encoding:
ASP.NET Web Form  |  2003-04-28  |  4.4 KB  |  100 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.     private void Page_Load(object sender, EventArgs e) {
  8.  
  9.         if (!Page.IsPostBack) {
  10.             // Databind the master grid on the first request only
  11.             // (viewstate will restore these values on subsequent postbacks).
  12.             
  13.             MasterGrid.SelectedIndex = 0;
  14.             BindMasterGrid();
  15.             BindDetailGrid();
  16.         }
  17.     }
  18.     
  19.     private void MasterGrid_SelectedIndexChanged(object sender, EventArgs e) {
  20.         BindDetailGrid();
  21.     }
  22.     
  23.     private void MasterGrid_PageIndexChanged(object sender, DataGridPageChangedEventArgs e) {
  24.         if (MasterGrid.SelectedIndex != -1) {
  25.             // unset the selection, details view
  26.             MasterGrid.SelectedIndex = -1;
  27.             BindDetailGrid();
  28.         }
  29.  
  30.         MasterGrid.CurrentPageIndex = e.NewPageIndex;
  31.         BindMasterGrid();
  32.     }
  33.     
  34.     private void BindMasterGrid() {
  35.  
  36.         // TODO: Update the ConnectionString and CommandText values for your application
  37.         string ConnectionString = "server=(local);database=pubs;trusted_connection=true";
  38.         string CommandText = "select au_lname as [Last Name], au_fname as [First Name], Address, City, State from Authors order by [Last Name]";
  39.         
  40.         SqlConnection myConnection = new SqlConnection(ConnectionString);
  41.         SqlDataAdapter myCommand = new SqlDataAdapter(CommandText, myConnection);
  42.  
  43.         DataSet ds = new DataSet();
  44.         myCommand.Fill(ds);
  45.  
  46.         MasterGrid.DataSource = ds;
  47.         MasterGrid.DataBind();   
  48.     }
  49.  
  50.     private void BindDetailGrid() {
  51.         // get the filter value from the master Grid's DataKeys collection
  52.         if (MasterGrid.SelectedIndex != -1) {
  53.             // TODO: update the ConnectionString value for your application
  54.             string ConnectionString = "server=(local);database=pubs;trusted_connection=true";
  55.     
  56.             // TODO: update the CommandText value for your application
  57.             string filterValue = ((string)MasterGrid.DataKeys[MasterGrid.SelectedIndex]).Replace("'", "''");
  58.             string CommandText = "select title as Title, price as Price, ytd_sales as [YTD Sales] from titleview where au_lname = '" + filterValue + "'";
  59.     
  60.             SqlConnection myConnection = new SqlConnection(ConnectionString);
  61.             SqlCommand myCommand = new SqlCommand(CommandText, myConnection);
  62.         
  63.             myConnection.Open();
  64.         
  65.             DetailsGrid.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
  66.         }
  67.         
  68.         DetailsGrid.DataBind();
  69.     }
  70.  
  71. </script>
  72.     
  73. <html>
  74.     <body style="font-family:arial">
  75.         <h2>
  76.             Master - Detail Grids
  77.         </h2>
  78.         <hr size="1">
  79.         <form runat="server">
  80.             <p>
  81.             <asp:datagrid id="MasterGrid" DataKeyField="Last Name" OnSelectedIndexChanged="MasterGrid_SelectedIndexChanged" AllowPaging="true" PageSize="6" OnPageIndexChanged="MasterGrid_PageIndexChanged" ForeColor="Black" BackColor="White" CellPadding="3" GridLines="None" CellSpacing="1" width="80%" runat="server">
  82.                 <HeaderStyle Font-Bold="True" ForeColor="white" BackColor="#4A3C8C"></HeaderStyle>
  83.                 <SelectedItemStyle ForeColor="White" BackColor="#9471DE"></SelectedItemStyle>
  84.                 <PagerStyle HorizontalAlign="Right" BackColor="#C6C3C6" Mode="NumericPages"></PagerStyle>
  85.                 <ItemStyle BackColor="#DEDFDE"></ItemStyle>
  86.                 <Columns>
  87.                     <asp:buttoncolumn text="Show details" commandname="Select" ItemStyle-Font-Bold="true" ItemStyle-Font-Size="smaller"></asp:buttoncolumn>
  88.                 </Columns>               
  89.             </asp:datagrid>
  90.             <br><br>
  91.             <asp:datagrid id="DetailsGrid" EnableViewState="False" ForeColor="Black" BackColor="White" CellPadding="3" GridLines="None" CellSpacing="1" width="80%" runat="server">
  92.                 <HeaderStyle Font-Bold="True" ForeColor="white" BackColor="#4A3C8C"></HeaderStyle>
  93.                 <PagerStyle HorizontalAlign="Right" BackColor="#C6C3C6" Mode="NumericPages"></PagerStyle>
  94.                 <ItemStyle BackColor="#DEDFDE"></ItemStyle>
  95.             </asp:datagrid>
  96.             </p>
  97.         </form>
  98.     </body>
  99. </html>
  100.