I have a problem,, I have to stop one Loading class on button click. I already checked some forums related to this. But didnt find exact solution.

for example:

Public Sub LoadDropDown()
            Dim it As Integer
            For it = 0 To 1000000
                DropDownList1.Items.Add(it)
            Next
End Sub

I have to load the drop down on Load button click, and have to cancel that on cancel button click

Help me.

Thank You

Rajs

Dani AI

Generated

This behavior is normal for classic ASP.NET: an HTTP request is handled to completion on the server, so a second postback (the Cancel button) cannot interrupt a loop that is already running. That explains why 's clear/visible approach only happens after the load finishes, and why the Page.IsPostBack suggestions from and or the ViewState idea from can prevent re-running work on later postbacks but do not stop an already-running server loop for .

Practical options (pick one depending on goals and constraints):

  • Client-side abort (fast UX): start the load via AJAX and keep the request handle; call abort when Cancel is clicked. This stops the client waiting immediately. The server may still finish processing unless the server cooperates. Example using fetch + AbortController:

    // start
    var controller = new AbortController();
    fetch('/api/StartLoad', { method: 'POST', signal: controller.signal })
      .then(r => r.json()).then(items => { /* populate select */ })
      .catch(e => { if (e.name === 'AbortError') { /* canceled client-side */ } });
    
    // cancel
    controller.abort();
  • Cooperative server cancellation (true stop): run the long work in a background Task that periodically checks a CancellationToken stored where both requests can access it (per-session cache or a concurrent dictionary keyed by SessionID). Expose a Cancel endpoint that calls Cancel() on that token source. Minimal sketch:

    [WebMethod(EnableSession=true)]
    public static string StartLoad()
    {
        var key = "Cts_" + HttpContext.Current.Session.SessionID;
        var cts = new CancellationTokenSource();
        HttpRuntime.Cache.Insert(key, cts);
        Task.Run(() => {
            for (int i=0; i<1000000; i++) {
                if (cts.Token.IsCancellationRequested) break;
                // add item to a cache/list/db for client to read
            }
        });
        return "started";
    }
    
    [WebMethod(EnableSession=true)]
    public static string CancelLoad()
    {
        var key = "Cts_" + HttpContext.Current.Session.SessionID;
        var cts = HttpRuntime.Cache.Get(key) as CancellationTokenSource;
        if (cts != null) cts.Cancel();
        return "canceled";
    }

    Important cautions: background Tasks started inside ASP.NET can be terminated by app-pool recycles; storing CancellationTokenSource in Session requires in‑proc session state. For reliable long-running jobs, use a background worker/service or a job framework.

  • Best practice (avoid the problem): do not populate a DropDownList with a million items. Provide server-side filtering/autocomplete, paging, or a virtualized list (typeahead/select2) so only a small result set is fetched at a time. This both avoids long server loops and gives far better user experience.

Recommended Answers

All 5 Replies

Hi,

Not sure what you mean

sub CancelButtononclick()
Mydropdownlist.items.clear
Mydropdownlist.visible =false
end sub

sub loadbuttonClick()
Dim it As Integer
For it = 0 To 1000000
DropDownList1.Items.Add(it)
Next
ENd Sub

Waddell,

But the cancel button event will work only after completing the load button event. So it will continue the loop

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
 {
  call ur function here
 }
}

Just call the function your want to run when page load in post back..
i.e:

 if (Page.IsPostBack == false)
        {
            // Function name
        }

or if you call it when it run after page load then

        if (Page.IsPostBack == true)
        {
            // function name
        }

Just make then code in button click a function and call it as you require

You can use the ViewState to indicates when you the page can load the drop down list and when shall not fill the drop down list
The set of the value of ViewState can be on the buttons click events of the buttons at your application

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.