当前位置:编程学习 > C#/ASP.NET >>

ASP.NET 2.0轻松实现数据库应用开发(4)

答案:     完成上面的所以步骤后,我们在主页面上的右键菜单上选择"查看代码",可以看到如下的代码:
  
  <%@ Page Language="C#" %>
  <html>
   <head id="Head1" runat="server">
    <title>GridView Bound Fields</title>
   </head>
   <body>
    <form id="form1" runat="server">
     <ASP:GridView ID="GridView1" DataSourceID="SqlDataSource1"AutoGenerateColumns="False" runat="server">
     <Columns>
      <asp:BoundField HeaderText="ID" DataField="au_id" ReadOnly="true" />
      <asp:BoundField HeaderText="Last Name" DataField="au_lname" />
      <asp:BoundField HeaderText="First Name" DataField="au_fname" />
      <asp:BoundField HeaderText="Phone" DataField="phone" />
      <asp:BoundField HeaderText="Address" DataField="address" />
      <asp:BoundField HeaderText="City" DataField="city" />
      <asp:BoundField HeaderText="State" DataField="state" />
      <asp:BoundField HeaderText="Zip Code" DataField="zip" />
      <asp:CheckBoxField HeaderText="Contract" DataField="contract" />
     </Columns>
     </asp:GridView>
     <asp:SqlDataSource ID="SqlDataSource1" runat="server" SelectCommand="SELECT [au_id], [au_lname], [au_fname], [phone],[address],[city], [state], [zip], [contract] FROM [authors]" ConnectionString="<%$ ConnectionStrings:Pubs %>" />
    </form>
   </body>
  </html>
  
    Web.Config中的代码如下:
  
  <?XML version="1.0"?>
  <configuration XMLns="http://schemas.microsoft.com/.netConfiguration/v2.0">
  <appSettings/>
  <connectionStrings>
  <add name="Pubs" connectionString="Data Source=hoowoo;Initial Catalog=pubs;Integrated Security=True" providerName="System.Data.SqlClient" />
  </connectionStrings>
  <system.web>
  <compilation debug="false"/>
  <authentication mode="Windows"/>
  </system.web>
  </configuration>
  
    现在来重点分析这些代码的意义:
  
  "<asp:GridView ID="GridView1" DataSourceID="SqlDataSource1"
  AutoGenerateColumns="False" runat="server">"
  
    数据绑定控件通过其 DataSourceID 属性连接到数据源控件,从而我们可以进行排序、分页、筛选、更新、删除和插入等一系列的操作。
  
  "<Columns>
  <asp:BoundField HeaderText="ID" DataField="au_id" ReadOnly="true" />
  <asp:BoundField HeaderText="Last Name" DataField="au_lname" />
  <asp:BoundField HeaderText="First Name" DataField="au_fname" />
  <asp:BoundField HeaderText="Phone" DataField="phone" />
  <asp:BoundField HeaderText="Address" DataField="address" />
  <asp:BoundField HeaderText="City" DataField="city" />
  <asp:BoundField HeaderText="State" DataField="state" />
  <asp:BoundField HeaderText="Zip Code" DataField="zip" />
  <asp:CheckBoxField HeaderText="Contract" DataField="contract" />
  </Columns>"
  
    "BoundField"和"CheckBoxField"均为要绑定的控件类型,"HeaderText"是将要显示在表格上字段的名称,而"DataField"则是我们要进行绑定的数据字段。
  
  <asp:SqlDataSource ID="SqlDataSource1" runat="server"
  SelectCommand="SELECT [au_id], [au_lname], [au_fname],
  [phone],[address],[city], [state], [zip], [contract] FROM [authors]"
  ConnectionString="<%$ ConnectionStrings:Pubs %>" />
  
    SqlDataSource控件中我们设置了数据库的SelectCommand命令为"SELECT [au_id],[au_lname],[au_fname],[phone],[address] [city], [state], [zip], [contract] FROM [authors]"这正好和GridView所要绑定的控件一一对应,这充分说明了数据绑定控件和数据源控件的紧密联系。
  
    细心的读者可能会奇怪了,ConnectionString="<%$ ConnectionStrings:Pubs %>在SqlDataSource是表示什么呢?这个问题就和我们为什么需要Web.Config配置文件有很大的关联了。Web.Config中设置了如下的节点:
  
  <connectionStrings>
  <add name="Pubs" connectionString="Data Source=hoowoo;Initial Catalog=pubs;Integrated Security=True" providerName="System.Data.SqlClient" />
  </connectionStrings>
  
    我们可以通过检索Web.Config配置文件来取得数据库连接字符串别名"Pubs"的真正的含义是
  "Data Source=hoowoo;Initial Catalog=pubs;Integrated Security=True" providerName="System.Data.SqlClient" Initial Catalog表明我们使用的是"pubs"数据库。Integrated Security说明了我们采用的是Windows验证方式。
  
    最后的显示如下:
  
  

上一个:ASP.NET设计网络硬盘之查看文件夹(1)
下一个:ASP.NET 2.0轻松实现数据库应用开发(3)

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,