web打印控件:DataGrid Web控件来源: 发布时间:星期四, 2009年2月12日 浏览:315次 评论:0
这篇文章是系列有关使用DataGrid WebControl控件文章第部分ASP.Net DataGrid WebControl控件可将数据库信息显示在HTML表格中并且功能强大在最简单情形下DataGrid显示HTML表格框架但是它可被增强以显示丰富用户界面可根据数据库列进行排序甚至允许对数据库结果进行分页!所有这些有趣主题将在今后系列文章中涉及 从数据库中获取表格信息并将其显示在个HTML表格中是传统ASP编程中最普通任务的在传统ASP编程中需要通过多行交织HTML和代码实现上述功能下面原形代码显示了这些代码通常形式 Create Database Connection Populate a record based _disibledevent=>sub Page_Load(sender as Object, e as EventArgs) lblMessage.Text = \"Hello, World!\" end sub </script> <asp:label runat=\"server\" id=\"lblMessage\" /> 这里带有runat=”server”属性(类似于HTML标记)lblMessage WebControl控件被放置在HTML中然后在Page_Load事件处理中(该事件处理在每次页面装载时被)lblMessageText属性被设置为”Hello World”此处对于WebControl控件使用实现了代码和内容分离在传统ASP中需要将<%=\"Hello, World!\"%>放置在HTML中合适位置才能达到同样效果 DataGrid基础 要在ASP.Net Web页面中加入DataGrid,只需执行如下代码: <asp:datagrid runat=\"server\" id=\"ID_of_DataGrid\" /> 这里id值将作为在服务器端代码中使用DataGrid名称我们通过将上述语法放置在HTML中来使用DataGrid但是为了让DataGrid显示任何有用信息我们需要将DataGrid绑定到些信息集合这些信息集合可以是任何支持IEnumerable接口对象它包括Arrays,集合类(ArrayList ,Hashtable等)Datas和其它很多对象由于希望集中精力显示数据库信息因此在本文中我们仅关注将DataGrid绑定至DatareaderDatareader类似于传统ADO/ASP中顺序(forward-only)记录集(如需了解在ADO.Net中读取数据库结果至Datareaders中请阅读Efficiently Iterating Through Results from a Database Query using ADO.NET ) 那么如何将数据绑定至DataGrid?其实出奇简单第件事是提取数据库数据至datareader.对于本例我使用ASPFAQs.com数据库并且提取最受欢迎10个问题旦将数据提取至datareader,将datareader绑定至DataGrid只需两行代码第行将DataGridDatasource属性设置为Datareader;第 2行DataGridDataBind思路方法代码如下所示: <% @Import Namespace=\".Data\" %> <% @Import Namespace=\".Data.SqlClient\" %> <script language=\"vb\" runat=\"server\"> Sub Page_Load(sender as Object, e as EventArgs) BindData End Sub Sub BindData \'1. Create a connection Dim myConnection as New SqlConnection( ConfigurationSettings.AppSettings(\"connectionString\")) \'2. Create the command object, passing in the SQL Const strSQL as String = \"sp_Popularity\" Dim myCommand as New SqlCommand(strSQL, myConnection) \'Set the datagrid\'s datasource to the datareader and databind myConnection.Open dgPopularFAQs.DataSource = myCommand.ExecuteReader( CommandBehavior.CloseConnection) dgPopularFAQs.DataBind End Sub </script> <asp:datagrid id=\"dgPopularFAQs\" runat=\"server\" /> 运行结果如下: Simple DataGrid Demo This demo shows how to bind the results of a query to an unformatted DataGrid. FAQID Description ViewCount SubmittedByName Submitted ByEmail Date Entered CatName 144 Where can I host my ASP Web site for free (similar to GeoCities or Tripod or any of the many other free Web site sites)? 161056 Scott Mitchell [email protected] 3/20/2001 2:53:45 AM Getting Started 181 How can I format numbers and date/times using ASP.NET? For example, I want to format a number as a currency. 123888 Scott Mitchell [email protected] 1/19/2002 3:12:07 PM ASP.NET … 首先注意用于编写数据绑定代码数量不多我们创建个连接指定个SQL命令(这里使用个存储过程sp_Popularity)打开数据库连接设定DataGridDataSource属性为Datareader最后DataGridDataBind思路方法这种做法完全将代码从内容分离没有像在传统ASP中混合HTML表格和DataReader输出语法 花些时间看下运行结果你会发现DataGrid使用HTML表格显示数据库内容尽管并不美观虽然我们完成了显示数据这主要工作但用户界面方面还有很多工作幸运是美化DataGrid结果出奇简单遗憾是需要等到下篇文章中作介绍 整理总结 这是系列有关DataGrid使用文章部分我们研究了DataGrid最基本功能:熟悉ASP.Net Web页面和显示绑定数据库结果遗憾是DataGrid输出并不美观但是我们不久会看到美化DataGrid结果很简单另外我们还将会在接下来文章中看到更多用户界面高级选项如数据库结果分页显示DataGrid结果排序和其它功能 0
相关文章读者评论发表评论 |