ASP.NET Web Matrix 提供的新服务器控件(二)

翻译|其它|编辑:郝浩|2005-03-15 11:17:00.000|阅读 1131 次

概述:

# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>


现在 DataGrid 已准备就绪,您需要将 DataGrid 绑定到此数据源。您可以通过 Page_Load 事件轻松完成此操作。ASP.NET 页面的最终代码应与代码示例 1 类似。

代码示例 1:Visual Basic .NET 中的 Page_Load 事件

<%@ Page Language="VB" %>
<%@ Register TagPrefix="wmx"
Namespace="Microsoft.Matrix.Framework.Web.UI"
Assembly="Microsoft.Matrix.Framework, Version=0.6.0.0,
Culture=neutral, PublicKeyToken=6f763c9966660626" %>

<script runat="server">

Sub Page_Load(sender As Object, e As EventArgs)
DataGrid1.DataBind
End Sub

</script>

<html>
<head>
</head>
<body>
<form runat="server">
<p>
<asp:DataGrid id="DataGrid1" runat="server"
DataSource="<%# SqlDataSourceControl1 %>"
BorderStyle="Solid" GridLines="Vertical"
BorderWidth="1px" BorderColor="#999999" BackColor="White"
CellPadding="3" ForeColor="Black">
<FooterStyle backcolor="#CCCCCC"></FooterStyle>
<HeaderStyle font-bold="True" forecolor="White"
backcolor="Black"></HeaderStyle>
<PagerStyle horizontalalign="Center" forecolor="Black"
backcolor="#999999"></PagerStyle>
<SelectedItemStyle font-bold="True" forecolor="White"
backcolor="#000099"></SelectedItemStyle>
<AlternatingItemStyle
backcolor="#CCCCCC"></AlternatingItemStyle>
</asp:DataGrid>
</p>
<p>
<wmx:SqlDataSourceControl id="SqlDataSourceControl1"
runat="server" ConnectionString="server='localhost';
trusted_connection=true;
Database='Northwind'" SelectCommand="Select * From
Customers"></wmx:SqlDataSourceControl>
</p>
<p>
<asp:DropDownList id="DropDownList1" runat="server"
DataSource="<%# SqlDataSourceControl1 %>"
DataTextField="CompanyName"
DataValueField="CustomerID"></asp:DropDownList>
</p>
</form>
</body>
</html>
 

代码示例 1:Visual C# 中的 Page_Load 事件

<%@ Page Language="C#" %>
<%@ Register TagPrefix="wmx"
Namespace="Microsoft.Matrix.Framework.Web.UI"
Assembly="Microsoft.Matrix.Framework, Version=0.6.0.0,
Culture=neutral, PublicKeyToken=6f763c9966660626" %>

<script runat="server">

void Page_Load(Object sender,EventArgs e) {
DataGrid1.DataBind();
}

</script>

<html>
<head>
</head>
<body>
<form runat="server">
<p>
<asp:DataGrid id="DataGrid1" runat="server"
DataSource="<%# SqlDataSourceControl1 %>"
BorderStyle="Solid" GridLines="Vertical"
BorderWidth="1px" BorderColor="#999999" BackColor="White"
CellPadding="3" ForeColor="Black">
<FooterStyle backcolor="#CCCCCC"></FooterStyle>
<HeaderStyle font-bold="True" forecolor="White"
backcolor="Black"></HeaderStyle>
<PagerStyle horizontalalign="Center" forecolor="Black"
backcolor="#999999"></PagerStyle>
<SelectedItemStyle font-bold="True" forecolor="White"
backcolor="#000099"></SelectedItemStyle>
<AlternatingItemStyle
backcolor="#CCCCCC"></AlternatingItemStyle>
</asp:DataGrid>
</p>
<p>
<wmx:SqlDataSourceControl id="SqlDataSourceControl1"
runat="server" ConnectionString="server='localhost';
trusted_connection=true;
Database='Northwind'" SelectCommand="Select * From
Customers"></wmx:SqlDataSourceControl>
</p>
<p>
<asp:DropDownList id="DropDownList1" runat="server"
DataSource="<%# SqlDataSourceControl1 %>"
DataTextField="CompanyName"
DataValueField="CustomerID"></asp:DropDownList>
</p>
</form>
</body>
</html>
 

在 Web Matrix 中按 F5 键运行此 ASP.NET 页面后,您将获得图 2 所示的结果。



图 2:在 Internet Explorer 中显示 SQL Server 数据库

在上面的代码示例中,我们要注意的一个主要问题是:SqlDataSourceControl 自身有一个不同于其他控件的标记前缀。您会发现,只有在 Web Matrix 中才能找到的特定 ASP.NET 控件的标记前缀为 wmx,而不是通常的 asp。

最后,您会发现与常规做法(不使用 SqlDataSourceControl)相比,这种从 SQL 中获取 Customers 表数据并将其显示在 DataGrid 中的过程十分简单。

您可以使用 SqlDataSourceControl 来填充可与数据源绑定的任何控件。例如,您可以很容易地使用 SqlDataSourceControl 动态填充 DropDownList 服务器控件。为此,请将 SelectCommand 属性的值更改为 Select CustomerID, CompanyName From Customers。然后将 DropDownList 服务器控件拖到设计界面上,并将以下属性更改为此处所列的值:

DataSource: SqlDataSourceControl1
DataTextField: CompanyName
DataValueField: CustomerID

然后,在 ASP.NET 页面的 Page_Load 事件中使用以下代码:

Visual Basic .NET

DropDownList1.DataBind

C#

DropDownList1.DataBind();

运行此页面将获得一个下拉列表(如图 3 所示),其中直观地显示了 SQL Server 的 Northwind 数据库中的 Customers 表中的所有公司名称。如果查看该页面的源代码(在浏览器中右键单击该页面,然后单击 View Source[查看源文件]),您会发现下拉列表中列出的每一项的值都是该项的 CustomerID 值。



图 3:在下拉列表中显示 CustomerID

我们将在后面学习如何使用 MxDataGrid 服务器控件时进一步学习如何使用 SqlDataSourceControl
 


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com


为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP