请教一个简单的问题!

benben4hao 2003-10-19 04:00:37
我怎样将数据库中某个字段的文本内容在网页上表示(比如只显示前100条记录),并且每条内容关联一个超链接。
...全文
72 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
zyg9108 2003-10-19
  • 打赏
  • 举报
回复
使用datagrid
应用模板列或超级联接列
绑定数剧源、
SELECT TOP 100 * FROM TABLE

具体代码想要可以提供
webdiyer 2003-10-19
  • 打赏
  • 举报
回复
如果你只是显示超链接,那么用Repeater最简单也最高效,可以这样:

从数据库中用“select top 100 url,linkname from mytable order by id”这样的方法取出前100条记录,然后填充到DataTable再把DataTable做为Repeater的数据源,或者执行ExecuteReader将取出的记录绑定到Repeater上,Repeater的代码可以这样写:

<asp:Repeater runat="server" id="Repeater1">
<ItemTemplate>
<a href='<%#DataBinder.Eval(Container.DataItem,"url")%>'><%#DataBinder.Eval(Container.DataItem,"linkname")%></a><br>
</ItemTemplate>
</asp:Repeater>
ddangerous169 2003-10-19
  • 打赏
  • 举报
回复
1.datagrid
2.response.write("<a href='"+字段+">"+字段+"</a>")
要求规整的可以用table
guoyan19811021 2003-10-19
  • 打赏
  • 举报
回复
使用datagrid
应用模板列或超级联接列
绑定数剧源、
SELECT TOP 100 * FROM TABLE
brightheroes 2003-10-19
  • 打赏
  • 举报
回复
up
qiuji 2003-10-19
  • 打赏
  • 举报
回复
string str="select top 100 * from mytable";
在DataGrid中:
使用超链接列:
<asp:HyperLinkColumn Target="_blank" DataNavigateUrlField="m_id" DataNavigateUrlFormatString="view.aspx?id={0}" DataTextField="m_title"></asp:HyperLinkColumn>
rgbcn 2003-10-19
  • 打赏
  • 举报
回复
这篇文章来自DotNetJunkie的提议。他最初写信要求我们提供一个关于如何创建在DataGrid 中使用HyperLinkColumn的例子,可以在用户点击这一列后打开一个新窗口,显示出此列的详细内容。在此之前我们曾经通过email回答他们,他建议我们将这个方法加入他们的指南中,于是,就有了这篇文章。像我们原来的文章一样,它很简单,但是简单的包含代码的方法例可以更有效地启发开发者。
这个例子包含两个WebForms和一个css文件(所有的代码都可以下载)--第一个WebForm包含一个展示从Northwind库中读出的产品列表的DataGrid,hyperlink的states设为“SeeDetails”,一旦这个链接被点击,JavaScript片段 Window.Open方法就会被调用.用户想获得的关于产品的ProductID做为参数包含在URL中.包含另一个DataGrid的第二个Webforms向用户列示他选中产品的所有具体细节。让我们来看一下WebForm1.asp 和WebFiorm1.aspx.cs.

WebForm1.aspx
<%@ Page language="c#" AutoEventWireup="false" Inherits="HowTos.DataGrid.PopupWindow.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<head>
<LINK REL="stylesheet" TYPE="text/css" HREF="StyleSheet1.css"></LINK>
</head>
<body>
<center>
<form runat="server" ID="Form1">
<asp:datagrid id="DataGrid1" runat="server" Font-Size="12" AutoGenerateColumns="False">
<Columns>
<asp:BoundColumn DataField= "ProductID" HeaderText= "Product ID" HeaderStyle-CssClass="HEADERSTYLE" ItemStyle-CssClass="ITEMSTYLEDEFAULT" />
<asp:BoundColumn DataField="ProductName" HeaderText="ProductName" HeaderStyle-CssClass="HEADERSTYLE" ItemStyle-CssClass="ITEMSTYLEDEFAULT"/>
<asp:hyperlinkcolumn DataTextFormatString="ShowDetails..." DataTextField="ProductID" DataNavigateUrlField="ProductID" DataNavigateUrlFormatString="javascript:varwin=window.open('WebForm2.aspx?ProductID={0}',null,'width=692,height=25');" HeaderText="See Details" HeaderStyle-CssClass="HEADERSTYLE" ItemStyle-CssClass="ITEMSTYLEHYPERLINK" />
</Columns>
</asp:datagrid>
</form>
</center>
</body>
</HTML>


WebForm1.aspx.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient ;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace HowTos.DataGrid.PopupWindow
{

public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;

#region User Defined Code

private void Page_Load(object sender, System.EventArgs e)
{

if ( ! this.IsPostBack )
this.BindData();

}

protected void BindData()
{

SqlCommand cmd = new SqlCommand( "SELECT TOP 10 ProductID, ProductName FROM Products", con("Server=LocalHost; DataBase=Northwind; TRUSTED_CONNECTION=TRUE"));
this.DataGrid1.DataSource = cmd.ExecuteReader(CommandBehavior.CloseConnection);
this.DataGrid1.DataBind();

}

protected SqlConnection con(System.String ConnectionString )
{

SqlConnection c = new SqlConnection( ConnectionString );
c.Open();
return c;

}

#endregion

#region Web Form Designer generated code

override protected void OnInit(EventArgs e)
{

InitializeComponent();
base.OnInit(e);

}

private void InitializeComponent()
{

this.Load += new System.EventHandler(this.Page_Load);

}

#endregion

}
}


除了DataNavigateUrlFormatString外确实没什么困难的,你可以注意到我实际上直接使用了一个javascript片段(注:你也可以简单地创建一个.js文件或在WebForm中使用<script></script>),javascript如此普及,所以这里不再详细讲解。功能上,它打开一个新的窗口,带ProductID查询字串的WebForm2.aspx,ProductID的值来自我们的数据源。我们可以看这两个文件:

WebForm2.aspx
<%@Page language="c#" AutoEventWireup="false" Inherits="HowTos.DataGrid.PopupWindow.WebForm2" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html>
<head>
<title>Product Details</title>
<LINK REL="stylesheet" TYPE="text/css" HREF="StyleSheet1.css"></LINK>
</head>
<body>
<asp:DataGrid HeaderStyle-CssClass="HEADERSTYLE" ItemStyle-CssClass="ITEMSTYLEDEFAULT" runat="server" id="DataGrid1" Font-Size="8" Height="50" Width="675"></asp:DataGrid>
<p align="center">
<a href="JavaScript:window.close()">close window</a>
</p>
</body>
</html>

WebForm2.aspx.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient ;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace HowTos.DataGrid.PopupWindow
{

public class WebForm2 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;

#region User Defined Code

private void Page_Load(object sender, System.EventArgs e)
{
if ( ! this.IsPostBack )
this.BindData();
}

protected void BindData()
{
SqlCommand cmd = new SqlCommand( "SELECT * FROM Products WHERE ProductID = @ProductID", con("Server=LocalHost; DataBase=Northwind; TRUSTED_CONNECTION=TRUE"));
cmd.Parameters.Add(new SqlParameter("@ProductID", SqlDbType.VarChar, 200));
cmd.Parameters["@ProductID"].Value = Request["ProductID"].ToString();
this.DataGrid1.DataSource = cmd.ExecuteReader(CommandBehavior.CloseConnection);
this.DataGrid1.DataBind();
}

protected SqlConnection con(System.String ConnectionString )
{

SqlConnection c = new SqlConnection( ConnectionString );
c.Open();
return c;

}

#endregion

#region Web Form Designer generated code

override protected void OnInit(EventArgs e)
{

InitializeComponent();
base.OnInit(e);

}

private void InitializeComponent()
{

this.Load += new System.EventHandler(this.Page_Load);

}

#endregion

}
}


WebForm2.aspx也很简单,只有一个绑定了SqlDataReader的DataGrid对象,这个Reader从产品库中得到数据,css文件如下:



StyleSheet1.css
/* Style Sheet */
BODY
{
margin-left: 0;
margin-top:10;
}
.HEADERSTYLE
{
background-color: #3a6ea5;
color: #FFFFFF;
font-weight:bold;
}

.ITEMSTYLEDEFAULT
{
background-color: #C0C0C0;
color: #000000;
font-weight: bold;
}

.ITEMSTYLEHYPERLINK {
background-color: #C0C0C0;
color: #000000;
font-weight: bold;
}

A:LINK
{
color: #000000;
}

A:VISITED
{
color: #000000;
}

A:HOVER
{
color: #3a6ea5;
}

rgbcn 2003-10-19
  • 打赏
  • 举报
回复
see

http://www.microsoft.com/china/msdn/technic/library/techart/datagrid.asp
qiuji 2003-10-19
  • 打赏
  • 举报
回复
使用Repeater、DataList、DataGrid都可以实现。
rgbcn 2003-10-19
  • 打赏
  • 举报
回复
用datagrid。
programmeraaron 2003-10-19
  • 打赏
  • 举报
回复
USE DATAGRID

SELECT:---->SELECT TOP 100 * FROM TABLE

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧