绑定txt文件到DataGrid

王朝asp·作者佚名  2006-01-09
宽屏版  字体: |||超大  

将文本文件和aspx文件放在统一文件夹下即可。

ReportFile.txt

站点名称,网站地址,创建日期

【孟宪会之精彩世界】之DHTML版本,http://lucky.myrice.com,2000-1-1

【孟宪会之精彩世界】之ASP版本,http://sz.luohuedu.net/xml/,2003-12-12

【孟宪会之精彩世界】之.NET版本,http://dotnet.aspx.cc/,2004-1-1

【孟子E章】专栏,http://blog.csdn.net/net_lover/,2004-1-6

【孟子E章】BLOG,http://ms.mblogger.cn/net_lover,2004-1-6

ReportFile.aspx

<%@ Page Language="C#" AutoEventWireup="True" %>

<html>

<head>

<script runat="server">

void Page_Load(Object sender, EventArgs e)

{

string ConnectionString;

string SQLString;

ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath(".") + ";Extended Properties=\"Text;HDR=yes;FMT=Delimited\"";

SQLString = "Select * from ReportFile.txt";

System.Data.OleDb.OleDbConnection ConnectionText = new System.Data.OleDb.OleDbConnection();

ConnectionText.ConnectionString = ConnectionString;

ConnectionText.Open();

System.Data.OleDb.OleDbDataAdapter AdapterText = new System.Data.OleDb.OleDbDataAdapter(SQLString, ConnectionText);

System.Data.DataSet DataSetText = new System.Data.DataSet("TextFiles");

AdapterText.Fill(DataSetText, "TextFile");

DataGrid1.DataSource = DataSetText;

DataGrid1.DataBind();

ConnectionText.Close();

}

void DG_ItemDataBind(Object sender, DataGridItemEventArgs e)

{

if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem )

{

e.Item.Attributes.Add("title",e.Item.Cells[0].Text);

e.Item.Cells[0].Text = "<a target=_blank href=" + e.Item.Cells[1].Text + ">" + e.Item.Cells[0].Text + "</a>";

}

else

e.Item.Attributes.Add("style","background-color:#dedede");

}

</script>

</head>

<body>

<form runat="server">

<asp:DataGrid id="DataGrid1" runat="server" OnItemDataBound="DG_ItemDataBind" Style="font-size:9pt"/>

</form>

</body>

</html>

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有