.NET 读取本地文件绑定到GridViewRow
wjgl.aspx.cs:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using DBUtility; namespace qhsy.DWJSC
{
public partial class wjgl : System.Web.UI.Page
{ protected string File_List = ""; protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack == false)
{ if (Request.UrlReferrer.ToString().Contains("wjsc.aspx")==false)
{
//保存上一页的url传递到wjsc.aspx
HttpCookie cookie = new HttpCookie("back");
cookie.Values.Add("url", Request.UrlReferrer.ToString());
Response.AppendCookie(cookie);
} lb_title.Text = "文 件 管 理"; //是否查询
string sfcx = Request.QueryString["sfcx"];
if (sfcx == "")
{
//gridFileList.Columns[0].Visible = false;
bg.Visible = false; } ////是否图片
//string sftp = Request.QueryString["sftp"];
//if (sftp == "1")
//{
// //GridView中添加图片缩略图
// gridFileList.Columns[2].Visible = true;
//} //操作模块id,参数
ViewState["shmkid"] = Request.QueryString["shmkid"];
//路径信息 fj = "~/File/JYWJ/8910427356/5209168734/";
ViewState["fj"] = Request.QueryString["fj"]; string path = string.Format("{0}", (string)ViewState["fj"]);
path = Server.MapPath(path);
ixExit(path); //设定初始排序字段为文件修改时间FileLastWriteTime
gridFileList.Attributes.Add("SortExpression", "FileLastWriteTime");
gridFileList.Attributes.Add("SortDirection", "DESC");
BindGrid(); } } //绝对路径(目录)是否存在,不存在则创建
private void ixExit(string path)
{
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
} } //读取本地文件绑定到GridViewRow
protected void gridFileList_RowDataBound(object sender, GridViewRowEventArgs e)
{ if (e.Row.RowType == DataControlRowType.DataRow)
{
Image Img = (Image)e.Row.Cells[].FindControl("ImgPath");
Img.ImageUrl = e.Row.Cells[].Text;
e.Row.Cells[].Text = ""; }
DataClass.Comm.gv_hbs(sender, e);
} //分页
protected void gridFileList_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gridFileList.PageIndex = e.NewPageIndex;
this.BindGrid();
} //绑定gridview
private void BindGrid()
{
//获取数据源
DataTable dtb = FileDataTable(); //排序
string SortDirection = gridFileList.Attributes["SortDirection"].ToString();
string SortExpression = gridFileList.Attributes["SortExpression"].ToString();
dtb.DefaultView.Sort = string.Format("{0} {1}", SortExpression, SortDirection); //赋数据源并绑定
gridFileList.DataSource = dtb;
gridFileList.DataBind();
gridFileList.Visible = gridFileList.Rows.Count > ;
gridFileList.Attributes.Add("BorderColor", "#000000"); } private DataTable FileDataTable()
{
string fj = (string)ViewState["fj"];
string path = Server.MapPath(fj);
GetFiles(path); //构造DataTABLE
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("FilePath", typeof(string)));
dt.Columns.Add(new DataColumn("FileName", typeof(string)));
dt.Columns.Add(new DataColumn("FileLength", typeof(string)));
dt.Columns.Add(new DataColumn("FileLastWriteTime", typeof(string)));
DataRow dr; //将文件数组集合切割到数组 string[] dtaArry = File_List.Split(','); for (int i = ; i < dtaArry.Length; i++)
{ if (dtaArry[i].Trim() != "")//判断是否为空
{ dr = dt.NewRow();
dr[] = dtaArry[i];//文件路径
dr[] = Path.GetFileName(dtaArry[i]);//文件名 //获取文件大小(Decimal.Round(Convert.ToDecimal(FI.Length), 2) string FIer = dtaArry[i].ToString();
FileInfo FI = new FileInfo(FIer);
dr[] = Convert.ToString(Decimal.Round(Convert.ToDecimal(FI.Length / ), )) + "KB";//获取的是字节byte,还需要转换为千字节KB dr[] = FI.LastWriteTime;
dt.Rows.Add(dr); } } return dt; } //遍文件夹下的所有子文件夹下的文件
public void GetFiles(string ObjDirPath)
{
DirectoryInfo SourceDir = new DirectoryInfo(ObjDirPath);
foreach (FileSystemInfo FSI in SourceDir.GetFileSystemInfos())
{ if (FSI is DirectoryInfo)
{ //如果是文件夹则递归
GetFiles(FSI.FullName);
} else
{
string FilePath = "";
FilePath = FSI.FullName.ToLower(); string fj = (string)ViewState["fj"];
string mc = fj.Substring(, ); //FilePath = FilePath.Substring(FilePath.LastIndexOf(mc));
File_List += FilePath + ","; } } } //排序
protected void gridFileList_Sorting(object sender, GridViewSortEventArgs e)
{ string sortExpression = e.SortExpression.ToString();
string sortdirection = "ASC"; if (sortExpression == gridFileList.Attributes["SortExpression"])
{ sortdirection = (gridFileList.Attributes["SortDirection"].ToString() == sortdirection ? "DESC" : "ASC"); } gridFileList.Attributes["SortExpression"] = sortExpression;
gridFileList.Attributes["SortDirection"] = sortdirection;
this.BindGrid(); } protected void btnDel_Click(object sender, EventArgs e)
{
try
{ FileInfo fileinfo;
bool IsSelect = false; for (int i = ; i < gridFileList.Rows.Count; i++)
{ GridViewRow row = gridFileList.Rows[i]; if (((CheckBox)row.FindControl("chkDel")).Checked)
{ IsSelect = true;
Image Img = (Image)gridFileList.Rows[i].Cells[].FindControl("ImgPath");
fileinfo = new FileInfo(Img.ImageUrl);
if (fileinfo.Exists)
{
fileinfo.Delete();
//根据ccid删除表jy_wj_wjccb中的数据
string str = string.Format("delete from jy_wj_wjccb where fj='{0}'", Img.ImageUrl.Substring(, Img.ImageUrl.Length-));
DbHelperSQL.Query(str);
}
}
}
if (IsSelect)
{
DataClass.Comm.Alert(Page, "删除成功!"); }
else
{ DataClass.Comm.Alert(Page, "请选择要删除的行!"); }
}
catch (Exception ex)
{
//DataClass.Comm.Alert(Page, ex.ToString());
} BindGrid();
} protected void btnAdd_Click(object sender, EventArgs e)
{ string s = string.Format("~/DWJSC/wjsc.aspx?shmkid={0}&&fj={1}&&sfcg={2}",(string)ViewState["shmkid"], (string)ViewState["fj"], );
Response.Redirect(s); } protected void btBack_Click(object sender, EventArgs e)
{ Response.Redirect((Request.Cookies["back"].Values["url"]).ToString()); //清空Cookies["back"]
Response.Cookies["back"].Expires = DateTime.Now.AddDays(-); } }
}
wjgl.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="wjgl.aspx.cs" Inherits="qhsy.DWJSC.wjgl" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>文件管理</title>
<link href="../COMM/CSS/CSS.css" rel="stylesheet" type="text/css" />
<style type="text/css"> #wjgl
{
background:#F2FBE5; text-align:center; vertical-align:middle;
width:%;
height: 714px;
}
#div
{
height: 701px;
}
</style>
<script type="text/javascript">
function SelAllClick() {
var isSelAll = document.getElementById("gridFileList_ctl01_CheckAll").checked;
for (var i = ; i < ; i++) {
if (i < ) checkBoxId = "gridFileList_ctl0" + i + "_chkDel";
else checkBoxId = "gridFileList_ctl" + i + "_chkDel";
var checkBoxObj = document.getElementById(checkBoxId);
if (checkBoxObj == undefined || checkBoxObj == null) break;
checkBoxObj.checked = isSelAll;
}
}
</script>
</head> <body class="body">
<form id="form1" runat="server">
<div id="div" class="div"> <div id="wjgl"> <table width="100%"> <tr> <td> <div class="divtitle" >
<asp:Panel ID="Panel4" CssClass="pan" runat="server" >
<asp:Label ID="lb_title" runat="server" CssClass="lab" Text=""></asp:Label>
</asp:Panel>
</div> </td> </tr> <tr> <td> <asp:GridView runat="server" ID="gridFileList"
Width="100%" HeaderStyle-Height="" OnRowDataBound="gridFileList_RowDataBound" AllowPaging="True" PageSize="" OnPageIndexChanging="gridFileList_PageIndexChanging" onsorting="gridFileList_Sorting" BorderStyle="None"
AutoGenerateColumns="false" > <Columns> <asp:TemplateField ItemStyle-CssClass="divcheck" > <ItemStyle HorizontalAlign="Center" /> <HeaderTemplate> <input id="CheckAll" type="checkbox" onclick="SelAllClick();" runat="server"/></HeaderTemplate> <ItemTemplate> <asp:CheckBox runat="server" ID="chkDel" /> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="序号" InsertVisible="False"> <ItemStyle HorizontalAlign="Center" /> <ItemTemplate> <asp:Label ID="Label2" runat="server" Text='<%# this.gridFileList.PageIndex * this.gridFileList.PageSize + this.gridFileList.Rows.Count + 1%>'/> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="图片" Visible="false" InsertVisible="False" > <ItemStyle HorizontalAlign="Center" /> <ItemTemplate> <asp:Image ID="ImgPath" runat="server" Width="" Height="" /> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="FileName" HeaderText="名称" SortExpression="FileName" > <ItemStyle HorizontalAlign="Center" /> </asp:BoundField> <asp:BoundField DataField="FileLength" HeaderText="大小" SortExpression="FileLength" > <ItemStyle HorizontalAlign="Center" /> </asp:BoundField> <asp:BoundField DataField="FilePath" SortExpression="FilePath"> <ItemStyle HorizontalAlign="Center" /> </asp:BoundField> <asp:BoundField DataField="FileLastWriteTime" HeaderText="修改时间" SortExpression="FileLastWriteTime" > <ItemStyle HorizontalAlign="Center" /> </asp:BoundField> </Columns> </asp:GridView> </td> </tr> <tr id="bg" runat="server">
<td align="center" class="sub_bg">
<asp:Button runat="server" ID="btnAdd" Text="添加文件" OnClick="btnAdd_Click" style="height: 26px" />
<asp:Button runat="server" ID="btnDel" Text="删除文件" OnClick="btnDel_Click" OnClientClick="return confirm('提示:确定要删除吗?');" />
<asp:Button ID="btBack" runat="server" Text="返回上一页" onclick="btBack_Click" /> </td>
</tr>
</table> </div>
</div>
</form>
</body>
</html>
.NET 读取本地文件绑定到GridViewRow的更多相关文章
- 【转】flash air中读取本地文件的三种方法
actionscript中读取本地文件操作有两种代码如下 1.使用File和FileStream两个类,FileStream负责读取数据的所以操作:(同步操作) var stream:FileStre ...
- 前台JS(type=‘file’)读取本地文件的内容,兼容各种浏览器
[自己测了下,能兼容各种浏览器,但是读取中文会出现乱码.自己的解决方法是用notepad++把txt文件编码改为utf-8(应该是和浏览器编码保持一致吧?..)] 原文 http://blog.cs ...
- 手工创建tomcat应用,以及实现js读取本地文件内容
手工创建tomcat应用: 1.在webapps下面新建应用目录文件夹 2.在文件夹下创建或是从其他应用中复制:META-INF,WEB-INF这两个文件夹, 其中META-INF清空里面,WEB-I ...
- H5读取本地文件操作
H5读取本地文件操作 本文转自:转:http://hushicai.com/2014/03/29/html5-du-qu-ben-di-wen-jian.html感谢大神分享. 常见的语言比如php. ...
- python 读取本地文件批量插入mysql
Uin_phone.txt 本地文件内容 有1000条,这里只是展示前几条,供参考 133584752 133584759 133584764 133584773 133584775 13358477 ...
- FileReader读取本地文件
FileReader是一种异步读取文件机制,结合input:file可以很方便的读取本地文件. 一.input:type[file] file类型的input会渲染为一个按钮和一段文字.点击按钮可打开 ...
- spark读取本地文件
/** * Read a text file from HDFS, a local file system (available on all nodes), or any * Hadoop-supp ...
- 让Chrome支持Ajax/$http方式读取本地文件
在开发中经常写些小demo调试一下插件什么的 数据源又经常手动构造分离为一个单独的文件.用ajax或$http去访问时总是拒绝访问.这个时候可以给Chrome的快捷图标加启动参数,让浏览器允许js访问 ...
- android红米等关于读取本地文件夹图片获取路径的问题的解决
在Android开发中,有从本地文件夹中读取图片的功能,使用一下代码打开图片选择列表: Intent intent = new Intent(); intent.setAction(Intent. ...
随机推荐
- Android 项目结构图
src:存放Java源代码 gen:存放系统自动生成的配置文件 Android 4.4.2:包含Android.jar文件,包含构建应用程序所需的所有Android SDK库 asssets:存放资源 ...
- php php-5.6.4.tar.bz2 apache 兼容问题 child pid 27858 exit signal Segmentation fault
环境 [root envirotar]# uname -a Linux i2..el6.x86_64 # SMP Thu Jul :: UTC x86_64 x86_64 x86_64 GNU/Lin ...
- overload、overwrite、override
1.重载 overload 函数名一样,参数不同(类型.顺序,与返回值类型无关),重载的函数一般在同一个类中 class A { public: void test() {} void test(in ...
- Oracle:试图访问正在使用的事务临时表
处理步骤为 1.找到表ID select * from dba_objects where object_name like 'TPT_RPWORPA1_QRY' 2.通过表ID查找正在使用的事务 s ...
- yii2图片处理扩展yii2-imagine的使用
示例控制器: <?php /** * 图片常用处理 * * 需要 yii/yii2-imagine 的支持 * php composer.phar require --prefer-dist y ...
- filter : progid:DXImageTransform.Microsoft.AlphaImageLoader ( enabled=bEnabled , sizingMethod=sSize , src=sURL )
很多时候需要将图片显示在网页上,一般都会这样做,如下: <img src="xxx.jpg"/> 是的,这样是可以做到,但是如果我要将本地的图片显示到页面上呢?你可能会 ...
- ios htttp网络请求cookie的读取与写入(NSHTTPCookieStorage)
当你访问一个网站时,NSURLRequest都会帮你主动记录下来你访问的站点设置的Cookie,如果 Cookie 存在的话,会把这些信息放在 NSHTTPCookieStorage 容器中共享,当你 ...
- [图像]判断图片是PNG还是JPG格式
typedef NS_ENUM(NSInteger, NSPUIImageType) { NSPUIImageType_JPEG, NSPUIImageType_PNG, NSPUIImageType ...
- Jquery-zTree的用法
[部分函数和属性介绍] 核心:zTree(setting, [zTreeNodes]) 这个函数接受一个JSON格式的数据对象setting和一个JSON格式的数据对象zTreeNodes,从而建立 ...
- popupwindow的基本使用以及基本动画效果
1.创建一个popupwindow view的布局文件自己写一个就好了,这里就不说了 View view= LayoutInflater.from(context).inflate(R.layout. ...