0

What I am trying to achieve is : when a file is downloaded from FTP server it should add 2% to bootstrap progress-bar.

C# code is ok and files get downloaded but the progress-bar is stuck at 2%.

HTML and Javascript is ok and working, but when called from code behind it's executed only once. i.e. bootstrap progress-bar moves only to 2%.

C#

int count=2;
using (WebClient ftpClient = new WebClient())
{
            ftpClient.Credentials = new System.Net.NetworkCredential("win10", "zzzz");

            for (int i = 0; i <= directories.Count - 1; i++)
            {

                if (directories[i].Contains("."))
                {

                    string path = "ftp://192.168.0.120/" + directories[i].ToString();
                    string trnsfrpth = @"E:\\ProjectLocalPath\" + directories[i].ToString();
                    ftpClient.DownloadFile(path, trnsfrpth);
                    ClientScript.RegisterStartupScript(this.GetType(), "lol", "javascript: updateProgress('" + count2 + "')", true);
                    count2 += 2;

                }



          }
}

HTML

<div id="div3" runat="server" class="progress progress-danger progress-striped progress progress_sm active" style="width: 425%;">
    <div id="e3" style="width: 0%;" class="bar" runat="server" role="progressbar">
    </div>
</div>

Javascript

var _prevent;
    var percentage;
    function updateProgress(percentage) {
        _prevent = setInterval(function () {
            var $bar = $('.bar');
            if (percentage > 100) {
                percentage = 100;
                clearInterval(_prevent);
            }
            $bar.width(percentage);
            $bar.text(percentage + "%");
        },800);
    }
    window.clearInterval(_prevent);
nApSt3r
  • 53
  • 1
  • 1
  • 4

1 Answers1

0

Information from MSDN:

A startup script is uniquely identified by its key and its type. Scripts with the same key and type are considered duplicates. Only one script with a given type and key pair can be registered with the page. Attempting to register a script that is already registered does not create a duplicate of the script.

In others words, you need to create a different key for each of your loop's iteration (instead of just "lol").

Eddy Garcia
  • 96
  • 1
  • 4
  • Tried this " ' " + count2 + " ' " instead of " lol " still it didn't solve. – nApSt3r Mar 17 '17 at 12:49
  • Take a look at Dalorzo's answer: http://stackoverflow.com/questions/20872019/scriptmanager-registerstartupscript-inside-while-loop-only-executed-after-breaki . – Eddy Garcia Mar 17 '17 at 19:35
  • Can I update progress-bar by javascript while the C# function is being executed (calling javascript function in for loop, So that progress-bar value should be changed everytime) ? – nApSt3r Mar 18 '17 at 07:14
  • No, @nApSt3r, you can't. Once you use ClientScript to register a javascript code, you need to finish your C# code and post back your page for the javascript to be executed. If you want such feature in your website, you'll have to use another approach. – Eddy Garcia Mar 20 '17 at 17:28
  • To achieve that, Can I use update panel ? If not, Is there any other option ? – nApSt3r Mar 20 '17 at 17:32