DataGrid如何绑定一个超级链接列

回答一个朋友的问题:DataGrid如何绑定一个超级链接列
 原创  smallfools2006-04-18 15:09:18查看评论  
  
    一位初学朋友提出了一个问题,题目如下:“我为学校做的网页原平是用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
     }
}
 

标签:   asp.net  

票数:0
我顶什么是“我顶”?

【发起辩论】  [手机订阅]  [收藏到我的网摘]
[推荐] | [评论] | [投诉] | [打印]
本文章被推荐到了0个圈子
点击数: 520
评论数: 3

本文章引用通告地址(TrackBack Ping URL)为:
http://post.blog.hexun.com/smallfools/trackback.aspx?articleid=3216620
本文章尚未被引用。
 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值