abcd abcd - 10 days ago 5
ASP.NET (C#) Question

How to copy the files based on the users confirmation in c# asp .net

Below is my code which copies files from one folder to another folder but what i need is while copying the files from TempFiles to TempFiles1 if there is a file with that name in TempFiles1 it should ask the user that does he wants to replace,if yes it should get copied else it should not get copied.How can i do this.

protected void Page_Load(object sender, EventArgs e)
{
btn1_Click(btn1, null);
}
protected void btn1_Click(object sender, EventArgs e)
{
//readfiles();
string sourcePath = Server.MapPath(".") + "\\TempFiles\\";
string targetPath = Server.MapPath(".") + "\\TempFiles1\\";
if (!Directory.Exists(targetPath))
{
Directory.CreateDirectory(targetPath);
}
foreach (var srcPath in Directory.GetFiles(sourcePath))
{
//Copy the file from sourcepath and place into mentioned target path,
//Overwrite the file if same file is exist in target path
File.Copy(srcPath, srcPath.Replace(sourcePath, targetPath), true);
}
}

Answer

you can manage this by adding simple jquery method to your html attribute:

<asp:Button id="btn" runat="server" onclick="return confirm('Are you sure?');" />

And here is the Script:

    <script>
        $('#btn').click(function () {
            // escape here if the confirm is false;
            if (!confirm('Are you sure?'))
                return false;
        });
    </script>

UPDATE: How to get the postback going:

<script>
    $('#btn').click(function () {
        // escape here if the confirm is false;
        if (!confirm('Are you sure?'))
            return false;
        else {
            $('FormId').submit();
        }
    });
</script>
Comments