How to Set Page Event in ASP.NET MVC with C# on Page_Load

269 Views Asked by At

I am working with ASP.NET MVC and C#. I want to fire card inserted/ejected event in my application. How would that be possible? It works perfectly fine in Windows Forms by just adding events in Form_Load(). But in ASP.NET MVC, where do I have to set these handlers ?

The Index method is :

public ActionResult Index()
{
   return View("Index");
}

The events that to be fire. Note: if I put these events in Index() before return View() then I get an error

Exception ( Async Function )

Code:

 f.CardInserted += new f.CardEventHandler(Card_Inserted);
 f.CardEjected += new f.CardEventHandler(Card_Ejected);
 f.Watch();

Card inserted / eject event handler definition:

public void Card_Inserted()
{
    try
    {
        if (f.Connect())
        {
             String x = f.GetCardUID();
             TempData["message"] =  x.ToUpper();
            //TempData["message"] = "Connection Established";
        }
        else
        {
            TempData["message"] = "Error During Connection";
        }
    }
    catch (Exception)
    {
        TempData["message"] = "Error During Try/Catch";
    }
}

public void Card_Ejected()
{
    TempData["message"] = "Card Removed";
    f.Disconnect();
}

Watch method:

public void Watch()
{
    this.RdrState = new Card.SCARD_READERSTATE();
    readername = GetReadersList()[0];
    this.RdrState.RdrName = readername;
    states = new Card.SCARD_READERSTATE[1];
    states[0] = new Card.SCARD_READERSTATE();
    states[0].RdrName = readername;
    states[0].UserData = 0;
    states[0].RdrCurrState = Card.SCARD_STATE_EMPTY;
    states[0].RdrEventState = 0;
    states[0].ATRLength = 0;
    states[0].ATRValue = null;
    this._worker = new BackgroundWorker();
    this._worker.WorkerSupportsCancellation = true;
    this._worker.DoWork += WaitChangeStatus;
    this._worker.RunWorkerAsync();
    return;
}

Where do I have to set these event handlers to work?

0

There are 0 best solutions below