ASP.NET - How to update UI inside DataReceivedHandler using ASP.NET?

Asked By sruthy ck on 25-Apr-17 02:29 AM

I have an eventhandler named DataRecievedHandler(This event fires when we scan the barcode with the device (BarCode Scanner) inside my web application.

This is my eventhandler

public void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)


inside this function i have to update my UI.

Following are the methods I have already tried:-

  1. Backgroundworker - create a backgroundworker object and inside backgroundworker_Dowork() I updated the UI and call this method inside the DataReceivedHandler()

    private BackgroundWorker bw = new BackgroundWorker();
    `private void bw_DoWork(object sender, DoWorkEventArgs e)
                Label1.Text = "HI";

    inside the DataReceivedHandler() :-

    bw.DoWork += new DoWorkEventHandler(bw_DoWork);
  2. create one button and inside that buttonclick event, i have updated the UI and call that button dynamically inside the DataReceivedHandler().

    1. Call the page load method inside the DataReceivedHandler() and update the UI inside the page load()

public void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e) { page_load(sender,EventArgs.Empty); } inside the page_load ()

protected  void Page_Load(object sender, EventArgs e)

                if (IsPostBack==false)
                        lblMessage.Text = "Scaned";

  1. Using UpdatePanel.

These 4 methods are working fine in UI thread(Button_click) not for DataReceivedHandler().

so can anyone help me on this?

Robbe Morris replied to sruthy ck on 25-Apr-17 08:08 AM
A BackgroundWorker isn't going to help you in ASP.NET.  It is a stateless model, if you are posting your form back your UI will be updated with the response back to the browser.  You don't have an automatic refresh/update back to the page prior to the postback's response to the browser.  There is no direct network connection that always stays active between a browser page your server side code.