初始化时,需要实现下面几个功能:
(1)获取参数DirID和ParentID的值。
(2)显示DirList控制的目录信息。
(3)显示当前目录下的硬盘信息。如果参数DirID大于-1,则显示DirID目录下的信息;如果ParentID大于-1,则显示ParentID目录下的信息;否则显示根目录下的信息。
int nFileID=-1;
private int nParentID=-1;
protected void Page_Load(object sender,EventArgs e)
{//获取参数DirID的值
if(Request.Params["DirID"]!=null)
{
if(Int32.TryParse(Request.Params["DirID"].ToString(),out nDirID)==false){return;}
}
if(Request.Params["ParentID"]!=null)
{
if(Int32.TryParse(Request.Params["ParentID"].ToString(),out nParentID)==false){return;}
}
if(!Page.IsPostBack)
{//显示目录列表的信息
BindDirectoryData();
if(nDirID>-1) //存在DirID>-1的情况
{
BindDirectoryData(nDirID);
SystemTools.SetListBoxItem(DirList,nDirID.ToString());
return;
}
if(nDirID<=-1&&nParentID>-1) //存在ParentID>-1的情况
{
BindDirectoryData(nParentID);
SystemTools.SetListBoxItem(DirList,nParentID.ToString());
return;
}
if(DirList.Items.Count>0)
{
BindDirectoryData(Int32.Parse(DirList.SelectedValue));
}
}
}
函数BindDirectoryData()显示控件DirList和MoveDirList的目录信息,这些功能都是函数ShowDirectory(DropdownList dDirList,int nParentID)实现。
private void BindDirectoryData()
{//显示目录列表信息
Disk disk=new Disk();
disk.ShowDirectory(DirList,-1);
if(DirList.Items.Count>0)
{
DirList.SelectedIndex=0;
}
disk.ShowDirectory(MoveDirList,-1);
}
private void BindDirectoryData(int nParentID)
{
//显示目录列表信息
IDisk disk=new Disk();
SqlDataReader dr=disk.GetDirectoryFile(nParentID);
//绑定控件的数据
DiskView.DataSource=dr;
DiskView.DataBind();
dr.Close();
ReturnBtn.Visible=nParentID>0?true:false;
}
protected string FormatImageUrl(bool bFlag,string sType)
{//文件类型
if(bFlag==true)
{
return("~/Images/folder.gif");
}
else
{
switch(sType)
{//bmp文件
case "image/bmp":return ("~/Images/exe.bmp");
//exe文件
case "application/octet-stream":return("~/Images/exe.bmp");
default:return("~/Images/other.gif");
}
}
return("");
}
//页面在显示目录,文件时,需要使用不同的链接地址。如果是目录,则需要使用页面ShowDisk.aspx;如果是文件,则需要使用页面ViewDisk.aspx。因此,页面使用函数格式化
链接地址,由函数FormatHerf(int nDirID,int nParentID,bool bFlag)实现,该函数根据参数bFlag返回不同的链接地址。
protected string FormatHerf(int nDirID,int nParentID,bool bFlag)
{
if(bFlag==true)
{
return("ShowDisk.aspx?DirID="+nDirID.ToString()+"&ParentID="+nParentID.ToString());
}
else
{
return("ViewDisk.aspx?DirID="+nDirID.ToString()+"&ParentID="+nParentID.ToString());
}
}
//改变DirList控件的选择时,需要重新绑定数据
protected void DirList_SelectedIndexChanged(object sender,EventArgs e)
{
BindDirectoryData(Int32.Parse(DirList.SelectedValue));
}
//返回
protected void ReturnBtn_Click(object sender,EventArgs e)
{Response.Redirect("~/ShowDisk.aspx?ParentID="+nParentID.ToString());}
//确认删除
protected void DiskView_RowDataBound(object sender,GridViewRowEventArgs e)
{
ImageButton deleteBtn=(ImageButton)e.Row.FindControl("DeleteBtn");
if(deleteBtn!=null)
{
deleteBtn.Attributes.Add("onclick","return confirm('你确定要删除所选择的数据项吗?');");
}
}
protected void DiskView_RowCommand(object sender,GridViewCommandEventArgs e)
{
if(e.CommandName=="delete")
{
try
{//删除数据
IDisk disk=new Disk();
disk.DeleteFile(Int32.Parse(DirList.SelectedValue));
Response.Write("<script>alert('"+"删除数据成功,请妥善保管好你的数据!"+"');</script>");
}
catch(Exception ex)
{//跳转到异常错误处理页面
Response.Redirect("ErrorPage.aspx?ErrorMsg="+ex.Message.Replace("<br>","").Replace("/n","")+"&ErrorUrl="+Request.Url.ToString().Replace
("<br>","").Replace("/n",""));
}
}
}
//移动文件或文件夹
移动文件或文件夹步骤具体如下:
(1)选择移动的文件或者文件夹
(2)选择移动到的文件夹
(3)单击“移动到”按钮移动文件或文件夹
protected void MoveBtn_Click(object sender,EventArgs e)
{
try
{
IDisk disk=new Disk();
foreach(GridViewRow row in DiskView.Rows)
{
CheckBox dirCheck=(CheckBox)row.FindControl("DirCheck");
if(dirCheck!=null)
{
if(dirCheck.Checked==true)
{
disk.MoveDirectory(Int32.Parse(DiskView.DataKeys[row.RowIndex].Value.ToString()),Int32.Parse(MoveDirList.SelectedValue));
}
}
}
BindDirectoryData(Int32.Parse(DirList.SelectedValue))
Response.Write("<script>alert('"+"修改数据成功,请妥善保管好你的数据!"+"');</script>");
}
catch(Exception ex)
{//跳转到异常错误处理页面
Response.Redirect("ErrorPage.aspx?ErrorMsg="+ex.Message.Replace("<br>","").Replace("/n","")+"&ErrorUrl="+Request.Url.ToString().Replace
("<br>","").Replace("/n",""));
}
}