一位初学朋友提出了一个问题,题目如下:“我为学校做的网页原平是用ASP做的,现想改成ASP.NET,但我不知道怎样将原数据库(ACCESS)中的记录中的文章标题写在表格中,还要为他们加上链接传递到另一个页面来显示它们的具体内容。”
如果我没有理解错误的话,应该是问在DataGrid里如何绑定一个超级链接列。
先在项目里建一个DataGridLink.aspx,然后拖入一个DataGrid控件,在控件上点右键,选择“属性生成器”。
0 && image.height>0){if(image.width>=510){this.width=510;this.height=image.height*510/image.width;}}" border=0>
如上图所示,选择“列”,将“在运行时自动创建列”前的勾给去掉。然后在添加一个“超级链接列”,在下面的属性里,按图添加。因为我用的是Office示例里的数据库,你也可以用自己的数据库,根据自己的情况改一下。“文本字段”,就是你在DataGrid里要显示出来的文本的那个字段。“URL格式字符串”是点击“文本字段”的超链接。其中参数用“{0}”来表示。而“{0}”所代表的字段在“URL字段”里表示。一点就是ID。
确定后进入DataGridList.aspx.cs页面。先添加“using System.Data.OleDb;”引用。再在“Page_Load”事件前添加“protected OleDbConnection conn = new OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=C://Program Files//Microsoft Office//OFFICE11//SAMPLES//Northwind.mdb");”
在“Page_Load”事件里添加以下代码:
if (!this.IsPostBack)
{
DataGridBind();
}
再建立一个DataGridBind方法
private void DataGridBind()
{
string strSql = "select * from 产品";
DataSet ds = new DataSet();
conn.Open();
OleDbDataAdapter myAdapter = new OleDbDataAdapter(strSql,conn);
myAdapter.Fill(ds,"ds");
conn.Close();
DataView dv = ds.Tables[0].DefaultView;
this.DataGrid1.DataSource = dv;
this.DataGrid1.DataBind();
}
编译生成看一下。完整代码如下:
DataGridLink.aspx:
<%@ Page language="c#" Codebehind="DataGridLink.aspx.cs" AutoEventWireup="false" Inherits="test.DataGridLink" %>
<!
DOCTYPE
HTML
PUBLIC
"-//W3C//DTD HTML 4.0 Transitional//EN"
>
<
HTML
>
<HEAD>
<title>DataGridLink</title>
<metaname="GENERATOR"Content="Microsoft Visual Studio .NET 7.1">
<metaname="CODE_LANGUAGE"Content="C#">
<metaname="vs_defaultClientScript"content="JavaScript">
<metaname="vs_targetSchema"content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<formid="Form1"method="post"runat="server">
<asp:DataGridid="DataGrid1"runat="server"AutoGenerateColumns="False">
<Columns>
<asp:HyperLinkColumnTarget="_blank"DataNavigateUrlField="
产品ID"
DataNavigateUrlFormatString
="Read.aspx?ID=(0)"
DataTextField
="
产品名称"
HeaderText
="
标题"></
asp:HyperLinkColumn
>
</Columns>
</asp:DataGrid>
</form>
</body>
</
HTML
>
DataGridLink.aspx.cs:
using
System;
using
System.Collections;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Web;
using
System.Web.SessionState;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.HtmlControls;
using
System.Data.OleDb;
namespace
test
{
///<summary>
/// DataGridLink
的摘要说明。
///</summary>
public class DataGridLink : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected OleDbConnection conn = new OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=C://Program Files//Microsoft Office//OFFICE11//SAMPLES//Northwind.mdb");
private void Page_Load(object sender, System.EventArgs e)
{
if (!this.IsPostBack)
{
DataGridBind();
}
}
private void DataGridBind()
{
string strSql = "select * from
产品";
DataSet ds = new DataSet();
conn.Open();
OleDbDataAdapter myAdapter = new OleDbDataAdapter(strSql,conn);
myAdapter.Fill(ds,"ds");
conn.Close();
DataView dv = ds.Tables[0].DefaultView;
this.DataGrid1.DataSource = dv;
this.DataGrid1.DataBind();
}
#region
Web
窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:
该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
///<summary>
///
设计器支持所需的方法 - 不要使用代码编辑器修改
///
此方法的内容。
///</summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}