home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 40 / IOPROG_40.ISO / SOFT / NETFrameworkSDK.exe / comsdk.cab / samples.exe / QuickStart / aspplus / samples / cache / datacache2.aspx < prev    next >
Encoding:
Text File  |  2000-06-01  |  5.7 KB  |  177 lines

  1. <%@ Import Namespace="System.IO" %>
  2. <%@ Import Namespace="System.Data" %>
  3.  
  4. <html>
  5.  
  6.   <script language="C#" runat="server">
  7.        
  8.     void Page_Load(Object Src, EventArgs E ) {
  9.     
  10.       if(!IsPostBack) {
  11.         LoadData();
  12.       }
  13.     }       
  14.        
  15.     void NewAuthorBtn_Click(Object sender, EventArgs E) {
  16.     
  17.       if(!Page.IsValid) {
  18.           AuthorMsg.Text = "Some required fields are missing";    
  19.           return;
  20.       }
  21.     
  22.       DataSet ds = new DataSet();
  23.  
  24.       // open the file and read the current data
  25.       FileStream fs = new FileStream(Server.MapPath("authors.xml"),FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
  26.       StreamReader reader = new StreamReader(fs);
  27.       ds.ReadXml(reader);
  28.       fs.Close();
  29.  
  30.       // append a row            
  31.       DataRow newAuthor = ds.Tables[0].NewRow();        
  32.       newAuthor["au_id"] = AuthorId.Text;
  33.       newAuthor["au_lname"] = LastName.Text;
  34.       newAuthor["au_fname"] = FirstName.Text;
  35.       newAuthor["phone"] = Phone.Text;
  36.       newAuthor["address"] = Address.Text;
  37.       newAuthor["city"] = City.Text;
  38.       newAuthor["state"] = State.Text;
  39.       newAuthor["zip"] = PostalCode.Text;
  40.       newAuthor["contract"] = Contract.Checked;
  41.       ds.Tables[0].Rows.Add(newAuthor);
  42.  
  43.       // rewrite the data file
  44.       fs = new FileStream(Server.MapPath("authors.xml"), FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
  45.       TextWriter writer = new StreamWriter(fs);
  46.       writer = TextWriter.Synchronized(writer);
  47.       ds.WriteXml(writer);
  48.       writer.Close();
  49.  
  50.       LoadData();
  51.     }
  52.  
  53.     void RefreshBtn_Click(Object sender, EventArgs e) {
  54.     
  55.       LoadData();
  56.     }
  57.  
  58.     void LoadData() {
  59.     
  60.       DataView Source = (DataView)Cache["MyData"];
  61.         
  62.       if(Source == null) {
  63.         
  64.         // read the data from the XML source
  65.         DataSet ds = new DataSet();
  66.  
  67.         FileStream fs = new FileStream(Server.MapPath("authors.xml"), FileMode.Open,FileAccess.Read);
  68.         StreamReader reader = new StreamReader(fs);
  69.         ds.ReadXml(reader);
  70.         fs.Close();
  71.             
  72.         Source = new DataView(ds.Tables[0]);
  73.         
  74.         // cache it for future use
  75.         Cache.Insert("MyData", Source, new CacheDependency(Server.MapPath("authors.xml")));    
  76.             
  77.         // we created the data explicitly, so advertise that fact
  78.         CacheMsg.Text = "Dataset created explicitly";
  79.       }
  80.       else {
  81.         CacheMsg.Text = "Dataset retrieved from cache";
  82.       }
  83.  
  84.       MyDataGrid.DataSource = Source;
  85.       MyDataGrid.DataBind(); 
  86.     }
  87.  
  88.   </script>
  89.  
  90.   <body>
  91.   
  92.     <form runat="server">
  93.  
  94.       <h3><font face="Verdana">File Dependencies</font></h3>
  95.  
  96.       <ASP:DataGrid id="MyDataGrid" runat="server"
  97.         Width="900"
  98.         BackColor="#ccccff" 
  99.         BorderColor="black"
  100.         ShowFooter="false" 
  101.         CellPadding=3 
  102.         CellSpacing="0"
  103.         Font-Name="Verdana"
  104.         Font-Size="8pt"
  105.         HeaderStyle-BackColor="#aaaadd"
  106.         />
  107.  
  108.       <hr>
  109.         
  110.       <h3><font face="Verdana">Add New Author</font></h3>
  111.         
  112.       <asp:Label ID="AuthorMsg" Text="Fill in the required fields below to add a new author" ForeColor="red" Font-Name="Verdana" Font-Size="10" runat=server />
  113.         
  114.       <p>
  115.  
  116.       <table>
  117.         <tr>
  118.           <td>Author Id:</td>
  119.           <td><ASP:TextBox id=AuthorId Text="111-11-1111" runat=server/></td>
  120.           <td><ASP:RequiredFieldValidator ControlToValidate="AuthorId" Display="Static" ErrorMessage="*" runat=server/></td>
  121.         </tr>
  122.         <tr>
  123.           <td>Last Name:</td>
  124.           <td><ASP:TextBox id=LastName Text="Doe" runat=server/></td>
  125.           <td><ASP:RequiredFieldValidator ControlToValidate="LastName" Display="Static" ErrorMessage="*" runat=server/></td>
  126.         </tr>
  127.         <tr>
  128.           <td>First Name:</td>
  129.           <td><ASP:TextBox id=FirstName Text="John" runat=server/></td>
  130.           <td><ASP:RequiredFieldValidator ControlToValidate="FirstName" Display="Static" ErrorMessage="*" runat=server/></td>
  131.         </tr>
  132.         <tr>
  133.           <td>Phone:</td>
  134.           <td><ASP:TextBox id=Phone Text="555 555-5050" runat=server/></td>
  135.           <td><ASP:RequiredFieldValidator ControlToValidate="Phone" Display="Static" ErrorMessage="*" runat=server/></td>
  136.         </tr>
  137.         <tr>
  138.           <td>Address:</td>
  139.           <td><ASP:TextBox id=Address Text="One Microsoft Way" runat=server/></td>
  140.           <td><ASP:RequiredFieldValidator ControlToValidate="Address" ErrorMessage="*" Display="Static" runat=server/></td>
  141.         </tr>
  142.         <tr>
  143.           <td>City:</td>
  144.           <td><ASP:TextBox id=City Text="Redmond" runat=server/></td>
  145.           <td><ASP:RequiredFieldValidator ControlToValidate="City" ErrorMessage="*" Display="Static" runat=server/></td>
  146.         </tr>
  147.         <tr>
  148.           <td>State:</td>
  149.           <td><ASP:TextBox id=State Text="WA" runat=server/></td>
  150.           <td><ASP:RequiredFieldValidator ControlToValidate="State" ErrorMessage="*" Display="Static" runat=server/></td>
  151.         </tr>
  152.         <tr>
  153.           <td>Postal Code:</td>
  154.           <td><ASP:TextBox id=PostalCode Text="98052" runat=server/></td>
  155.           <td><ASP:RequiredFieldValidator ControlToValidate="PostalCode" ErrorMessage="*" Display="Static" runat=server/></td>
  156.         </tr>
  157.         <tr>
  158.           <td>Contract:</td>
  159.           <td><ASP:CheckBox id=Contract Checked runat="server"/></td>
  160.           <td></td>
  161.         </tr>
  162.       </table>
  163.  
  164.       <asp:button Text="Add New Author" OnClick="NewAuthorBtn_Click" runat=server/> <asp:button Text="Refresh List" OnClick="RefreshBtn_Click" runat=server/>
  165.  
  166.       <p>
  167.  
  168.       <hr>
  169.       
  170.       <p>
  171.       
  172.       <i><asp:label id="CacheMsg" runat="server"/></i></p>
  173.  
  174.     </form>
  175.   </body>
  176. </html>
  177.