Norm
Norm

Reputation: 155

Call a method on Page Load

I want to make this code work on c# code behind. I know in C something similar can be done, but can it be done here?

protected void Page_Load(object sender, EventArgs e)
{
    Bar.Text = "Updating Information";
    InsertData();
    Bar.Text = "Information Updated";
}


protected void InsertData(object sender, EventArgs e)
{
  //loops and statements
}

any ideas?

Upvotes: 0

Views: 9970

Answers (4)

KeithS
KeithS

Reputation: 71591

Two things. First, you're not passing the expected parameters:

protected void Page_Load(object sender, EventArgs e)
{
    Bar.Text = "Updating Information";
    InsertData(sender, e);
    Bar.Text = "Information Updated";
}


protected void InsertData(object sender, EventArgs e)
{
    //loops and statements
}

Second, this looks like ASP.NET Web Forms. The changes to the Text property of your control will not update the UI. To understand why, you have to understand what's really going on outside the bounds of the actual code you write. ASP.NET WebForms have a very high degree of "inversion of control"; your code lives to serve the ASP engine resident in the IIS webserver, and has very little ultimate control over when it runs (or even when it exists).

This has two major consequences for you; first, the amount of state information retained by the server about each client asking for pages from it is very low; ideally, no state is saved (but realistically the server must usually keep track of some "session state", especially for secured web apps requiring a login). In fact, your codebehind class only lives as long as it takes to render a page; after that, the class (and any state data it was trying to store in memory) are disposed of and garbage-collected.

Second, and more importantly for you, anything done in the codebehind to render the next requested page will not result in real-time updates for the client; everything your code does in the codebehind, from Page_Load to Page_OnPreRender, is performed before a single bit of the resulting HTML is sent back to the client. The upshot is that changing the value of a control on the form several times during the execution of codebehind event handlers has zero bearing on what the client sees, unless you specifically set up additional communication with the client. The updating of Bar.Text before calling InsertData() will not cause the client to see the message "Updating Information". All the client will ever see is "Information Updated", because that is the text of the control as of when the page is finally rendered into HTML, after all your event handlers execute.

Problems like these are typically resolved with asynchronous architectures such as AJAX (Asynchronous Javascript And XML). JavaScript's level of control over the rendered page in the browser window rivals that of a WinForms application (enough that you can program simple games with HTML as your UI using only client-side JavaScript), and there are methods and objects JavaScript can use to send requests for data to your server. So, to keep the user's displayed page responsive during this insert operation, you could employ these tricks. You'd have a button on your page that, instead of triggering a full postback of the entire page, would instead trigger a Javascript method on the client that would send a web service request to your server, and also update the value of your Bar UI control text with the status "Updating Information". The server would perform an action (your "InsertData()" call) and then respond to the client's browser, which would trigger a second JavaScript method that would update the status again with "Information Updated".

As an aside, it is in fact possible to create entire web applications using this Javascript-based "service-oriented architecture" for all content updates, where only one initial HTML page is ever served to the client browser, and all further changes to that page's layout, content and behavior is controlled by JavaScript updating the one page's DOM based on data received from web service calls. Such architecture of a website has its downsides, but also some big advantages.

Upvotes: 2

C0L.PAN1C
C0L.PAN1C

Reputation: 12243

Do you need to know the sender or pass EventArgs? You could simple redefine the InsertData Method as

protected void InsertData()
{
  //loops and statements
}

and then call the method via

Bar.Text = "Updating Information";
InsertData();
Bar.Text = "Information Updated";

Upvotes: 0

Semih Yagcioglu
Semih Yagcioglu

Reputation: 4101

You can change your InsertData() method, since you are calling it with no parameters.

protected void InsertData()
    {
      //loops and statements
    }

Upvotes: 0

Keith Payne
Keith Payne

Reputation: 3082

Bar.Text = "Updating Information";
InsertData(this, EventArgs.Empty);
Bar.Text = "Information Updated";

Upvotes: 0

Related Questions