我有一个 asp.net 应用程序在页面上显示具有 TreeView 结构的文件名。 用户单击复选框以选择 TreeView 上的下载文件后,他们可以单击提交按钮。 首先,我需要检索选中(选中)的节点路径值。 其次,将所有带有文件夹/文件名的节点路径传递给客户端 javascript,以将文件下载到本地机器。 (我们正在使用使用 javascript 函数的第三方 Softartisans XFile 下载)。
我能够在代码隐藏处使用 onclick 检索节点路径值,但无法将值传递给 javascript 函数。
我的问题是“我可以通过任何方式调用 javascript 函数并在回发后传递值。我使用了 ReisterArrayDeclaration,代码为 ..
if (!IsPostBack)
{
dnlFile = getDownloadFile();
ClientScriptManager csm = Page.ClientScript;
csm.RegisterArrayDeclaration("dnlFile", dnlFile);
btnDownLink.Attributes.Add("OnClick", "btnFileDown_Click('" + dnlFile + "')");
}
protected void btnDownLoad_Click(object sender, EventArgs e)
{
TreeView tv = tvFileDown;
if (tv.CheckedNodes.Count > 0)
{
foreach (TreeNode node in tv.CheckedNodes)
{
string strFilePath = Server.MapPath(initFolderPath + node.ValuePath);
if (Directory.Exists(strFilePath))
{
lblSelectedNode.Text = node.ValuePath + ", ";
}
else
{
if (File.Exists(strFilePath))
{
dnFile.Add(node.ValuePath);
}
}
}
dnlFile = getDownloadFile();
}
}
private string getDownloadFile()
{
string downLoadFile = "";
if (dnFile.Count > 0)
{
for (var i = 0; i < dnFile.Count; i++)
{downLoadFile += dnFile[i].ToString() + ", ";}
}
lblFinalFilePath.Text = downLoadFile;
return downLoadFile;
}
感谢您的帮助!!
最佳答案
嗯,我想你需要的是注册启动脚本
假设你有一个 javascript 接受一个带有文件路径的数组,并调用函数来下载文件(你说它是客户端)
function downloadFiles(filepaths){
for( var file in filepaths)
{
download(file);
}
}
在代码隐藏中,你会这样做:
protected void btnDownload_Click(object sender, EventArgs e)
{
TreeView tv = tvFileDown;
String filepaths;
if (tv.CheckedNodes.Count > 0)
{
//build the string with js array syntax
}
//then you call the javascript function
this.Page.ClientScript.RegisterStartupScript(this.GetType(),
"CallDownloadFIlesFunction",
"downloadFiles('" + filepaths + "');", // here you call the javascript function
true);
}
关于javascript - 如何使用相同的提交按钮调用 javascript 回发代码隐藏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5543785/