How to call javascript postback codebehind with same submit button
I have a asp.net application display file names with treeview structure on the page. After the user click on the checkbox to select the download files on the treeview then they can click the submit button. First, I need to retrieve the checked(selected) node path value. Second, pass all the node path with folder/file names to client javascript to downloading the files to local machine. (we are using third party Softartisans XFile download which are using javascript function).
I was able to retrieve the nodepath valu开发者_开发技巧e with onclick at codebehind, but have problem to pass the value to javascript function.
My question is "There is any way I can call javascript function and pass the values AFTER the postback. I have used ReisterArrayDeclaration, the codes as ..
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;
}
Thans advance for your help!!
Well, I think what you need is register startup script
Lets say you have a javascript that accepts an array with filepaths, and call the function to download the file (which you said it's client-side)
function downloadFiles(filepaths){
for( var file in filepaths)
{
download(file);
}
}
in code-behind, you would do this:
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);
}
精彩评论