javascript - 如何使用相同的提交按钮调用 javascript 回发代码隐藏

标签 javascript asp.net

我有一个 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/

相关文章:

javascript - 不知道停止这个无限循环的语法

javascript - nodejs 作为 http 服务器有多强大?

asp.net - 添加文本时防止多行文本框拉伸(stretch)

c# - 登录成功后User.Identity.IsAuthenticated为false

c# - 如何读取包含未编码数据的查询字符串?

javascript - div 中的文本在 safari 8.2、chrome 39 中显示,但在 firefox 34 中不显示。为什么?

javascript - Node.js 如何使用 require() 处理多次包含的大数组和对象?

javascript - 为什么无法使用 nextElementSibling.focus() 和parentNode.children[0].focus() 将焦点设置在元素上?

jquery - ASP.NET 服务器端表单验证 + jQuery 验证

asp.net - 图像按钮间隙