c# 4.0 - Best way to refresh a Web.UI.Page in response to a callback from another thread -
what best way accomplish following in web page lifecycle?
protected void btntestasync_click(object sender, eventargs e) { this.mainthreadid = thread.currentthread.managedthreadid; testbll bl = new testbll(); bl.onbeginwork += onbeginwork; bl.onendwork += onendwork; bl.onprogressupdate += onwork; threadstart threaddelegate = new threadstart(bl.performbeginwork); thread newthread = new thread(threaddelegate); newthread.start(); }
then on onworkevent enter:
private void onwork(asyncprogress workprogress, ref bool abortprocess) { string s = string.format("main tread: {0} worker thread: {1} count :{2} complete: {3} remaining: {4}", this.mainthreadid, workprogress.threadid, workprogress.numberofoperationstotal, workprogress.numberofoperationscompleted, workprogress.numberofoperationsremaining); lbl.text = s; lb.items.add(s); //.processmessages(); response.redirect???<-- here want rfresh page. during debug test variables proper }
please excuse ignorance; have never done web.ui.page. best way update ui delegate callback in thread?
thanks,
i suggest ajax.
your button click cause browser postback. point there nothing "force" browser (client side) postback unless user "does something"
using ajax can async call respond when call complete.
there multiple ways this, use jquery.
here example of possible ajax call using jquery:
$.ajax({ url: "../ajax/backgroundworker.ashx", data: 'element=' + $(this).parent().siblings('.datarow').children('.dataelement').text(), datatype: "text", success: function(data) { var tadata = data.split("|"); if (tadata[0] != "-1") { $(".dataelement:contains('" + tadata[0] + "')").parent().siblings().children('.displayfield').text(tadata[1]); $(".dataelement:contains('" + tadata[0] + "')").parent().siblings().children('.img_throbber').css('visibility', 'hidden'); } else { alert("there problem accessing background service responsible data processing."); $('.do_work_button').css("visibility", "hidden"); $(".dataelement").parent().siblings().children('.dataelement').text("n/a"); $(".dataelement").parent().siblings().children('.img_throbber').css('visibility', 'hidden'); } }, error: function(xhr, status, error) { displayajaxerror(xhr); $(".dataelement").parent().siblings().children('.img_throbber').css('visibility', 'hidden'); }
the $.ajax command called click event on page. , .ashx (asp.net web handler file) kinda vehichle can use data client side server side. can reference server side objects , code in .ashx use data client side ajax call return results via http context.
Comments
Post a Comment