先在项目里建一个DataGridLink.aspx,然后拖入一个DataGrid控件,在控件上点右键,选择“属性生成器”。


如上图所示,选择“列”,将“在运行时自动创建列”前的勾给去掉。然后在添加一个“超级链接列”,在下面的属性里,按图添加。因为我用的是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" %>
<!DOCTYPEHTMLPUBLIC"-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>DataGridLink</title>
<metaname="GENERATOR"C>
<metaname="CODE_LANGUAGE"C>
<metaname="vs_defaultClientScript"c>
<metaname="vs_targetSchema"chttp://schemas.microsoft.com/intellisense/ie5">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
}
}
可能有朋友要问了,这只是绑定一个URL字段,如果我要绑定多个呢?请看如何将一个超级链接列绑定多个字段。
| 友情提示:此文并不表示本站肯定持有相同观点,转载请注明出处。 |