📄 selfile.ascx.cs
字号:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
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.IO;
//using System.Runtime.Serialization.Json;
public partial class controls_selfile : System.Web.UI.UserControl,ICallbackEventHandler
{
public controls_selfile()
{
_rootPath = "~/Portals";
}
protected void Page_Init(object sender, EventArgs e)
{
string root = Server.MapPath(_rootPath );
string[] dirs = Directory.GetDirectories(root, "*", SearchOption.AllDirectories);
dplDirs.Items.Add("/");
for (int i = 0; i < dirs.Length; i++)
{
dplDirs.Items.Add( dirs[i].Replace(root,"").Replace("\\","/") );
}
dplDirs.Value = Request[dplDirs.Name];
dplDirs_SelectedIndexChanged(this, e);
dplFiles.Value = Request[dplFiles.Name];
}
protected void dplDirs_SelectedIndexChanged(object sender, EventArgs e)
{
string path = Server.MapPath(_rootPath + dplDirs.Value );
string[] files = Directory.GetFiles(path, "*", SearchOption.TopDirectoryOnly );
dplFiles.Items.Clear();
dplFiles.Items.Add("<没有指定>");
for (int i = 0; i < files.Length; i++)
{
dplFiles.Items.Add(Path.GetFileName(files[i]));
}
}
private string _rootPath;
//根路径
public string RootPath
{
get { return _rootPath; }
set
{
_rootPath = value;
if (_rootPath.EndsWith("/"))
{
_rootPath = _rootPath.Substring(0, _rootPath.Length - 1);
}
}
}
/// <summary>
/// 选择的文件
/// </summary>
public string FileName
{
get
{
if (dplFiles.Value == "<没有指定>")
return "";
return _rootPath+ (dplDirs.Value == "/" ? "" : dplDirs.Value )+ "/" + dplFiles.Value ;
}
set
{
if (string.IsNullOrEmpty(value))
{
return;
}
string dir = value;
dir = dir.Replace(_rootPath,"");
string file = Path.GetFileName(dir);
dir = dir.Substring(0, dir.Length - file.Length-1);
for (int i = 0; i < dplDirs.Items.Count; i++)
{
if (dplDirs.Items[i].Text.Equals(dir))
{
dplDirs.SelectedIndex = i;
break;
}
}
dplDirs_SelectedIndexChanged(this, new EventArgs());
for (int i = 0; i < dplFiles.Items.Count ; i++)
{
if (dplFiles.Items[i].Text.Equals(file))
{
dplFiles.SelectedIndex = i;
break;
}
}
}
}
#region ICallbackEventHandler 成员
private string dir;
public string GetCallbackResult()
{
string path = Server.MapPath(_rootPath + dir );
string[] files = Directory.GetFiles(path, "*", SearchOption.TopDirectoryOnly);
//Hashtable tb = new Hashtable();
string json = "[";
for (int i = 0; i < files.Length; i++)
{
//tb.Add(Path.GetFileName(files[i]), Path.GetFileName(files[i]));
string value = "{{\"Key\":\"{0}\",\"Value\":\"{1}\"}}";
value = string.Format(value, Path.GetFileName(files[i]), Path.GetFileName(files[i]));
if (i > 0)
{
json += ",";
}
json += value;
}
json += "]";
return json;
}
public void RaiseCallbackEvent(string eventArgument)
{
dir = eventArgument;
}
#endregion
protected void Page_Load(object sender, EventArgs e)
{
string cbReference =
Page.ClientScript.GetCallbackEventReference(this ,
"arg", this.UniqueID + "ReceiveServerData", "context");
string callbackScript = "function "+ this.UniqueID +"selChange(arg,context) { " +
cbReference + "} ;";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(),
this.UniqueID , callbackScript, true);
dplDirs.Attributes.Add("onchange", this.UniqueID + "findserver(this)");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -