Problem with EventHandlers

  • Thread starter Thread starter matt.grande
  • Start date Start date
M

matt.grande

Please, I REALLY need help with this one. I've been banging my head
about it for three days now.

Here's the situation.

I have a form that runs some queries and functions and such. When all
is said and done, there are several new controls (LinkButtons) on the
page. Dynamicly created controls MUST be created in the Page_Load.

Now, if I click one of the LinkButtons, the page attempts to reload
before calling the button's EventHandler. This causes the
above-mentioned function to go again, before the EventHandler is
called. This causes an unnecessary slow-down.

I need to find a way to skip over the method. The best solution I've
been able to come up with is as follows:

Code:
string et = Request.Form["__EVENTTARGET"];
if ((IsPostBack) && (et == ""))
ExecuteCustomSearch();

One would think that it would work fine, but for whatever reason, if
the "if" statement is false, the EventHandler doesn't get called.

I'm at my wits end here. Any help would be greatly appreciated.

PS - I posted this in the Framework group as well, but that was
accidental. Sorry.
 
Hi Matt,

Inline:
I have a form that runs some queries and functions and such. When all
is said and done, there are several new controls (LinkButtons) on the
page. Dynamicly created controls MUST be created in the Page_Load.

All controls should be created and added to their parent's Controls
collection on or before Page.PreInit.

http://msdn2.microsoft.com/en-us/library/ms178472.aspx
Now, if I click one of the LinkButtons, the page attempts to reload
before calling the button's EventHandler. This causes the
above-mentioned function to go again, before the EventHandler is
called. This causes an unnecessary slow-down.

Review the link I posted above. Control events are always raised after the
page is loaded.
I need to find a way to skip over the method. The best solution I've
been able to come up with is as follows:

Code:
string et = Request.Form["__EVENTTARGET"];
if ((IsPostBack) && (et == ""))
ExecuteCustomSearch();

One would think that it would work fine, but for whatever reason, if
the "if" statement is false, the EventHandler doesn't get called.

Correct me if I am wrong, but I think you are asking, "How can I get a
method to be invoked once on postbacks and once, the first time the page is
created?"

Here's how:

protected override void OnLoad(EventArgs e)
{
if (!IsPostBack)
// first time page is created execute the following code:
{
DoStuff();
}
}

private void SomeControl_Click(object sender, EventArgs e)
{
// execute the following code when the Click event is raised by
SomeControl:
DoStuff();
}

HTH
 
Hi Dave,

"Correct me if I am wrong, but I think you are asking, "How can I get a
method to be invoked once on postbacks and once, the first time the
page is
created?" "

Not quite. I want that method to only run when a specific button is
pressed. For whatever reason, that button has an __EVENTTARGET value
of "", while all other buttons have a value that is the same as their
ID. Do you know of a way to check which button caused the page load?

(I thought object sender would have the value, but it didn't. It only
has the webpage).

"All controls should be created and added to their parent's Controls
collection on or before Page.PreInit."

Actually, that's not quite right. Any dynamicly created controls must
be created during the load. From your link, in the 'Page
Initialization' section:
"If the current request is a postback, the postback data has not yet
been loaded"
Therefore, you have to wait until the load to read any PostBack info.

Thanks,
- Matt.

Dave said:
Hi Matt,

Inline:
I have a form that runs some queries and functions and such. When all
is said and done, there are several new controls (LinkButtons) on the
page. Dynamicly created controls MUST be created in the Page_Load.

All controls should be created and added to their parent's Controls
collection on or before Page.PreInit.

http://msdn2.microsoft.com/en-us/library/ms178472.aspx
Now, if I click one of the LinkButtons, the page attempts to reload
before calling the button's EventHandler. This causes the
above-mentioned function to go again, before the EventHandler is
called. This causes an unnecessary slow-down.

Review the link I posted above. Control events are always raised after the
page is loaded.
I need to find a way to skip over the method. The best solution I've
been able to come up with is as follows:

Code:
string et = Request.Form["__EVENTTARGET"];
if ((IsPostBack) && (et == ""))
ExecuteCustomSearch();

One would think that it would work fine, but for whatever reason, if
the "if" statement is false, the EventHandler doesn't get called.

Correct me if I am wrong, but I think you are asking, "How can I get a
method to be invoked once on postbacks and once, the first time the page is
created?"

Here's how:

protected override void OnLoad(EventArgs e)
{
if (!IsPostBack)
// first time page is created execute the following code:
{
DoStuff();
}
}

private void SomeControl_Click(object sender, EventArgs e)
{
// execute the following code when the Click event is raised by
SomeControl:
DoStuff();
}

HTH
 
Hi Grande,
Do you know of a way to check which button caused the page load?

A post-back is a standard web <form> submission. Buttons are standard
<input type='submit'> buttons. All you must do is assign a unique name to
each button, which is done by the framework, and check
Request.Form[button.UniqueID] != null to see if a particular Button has
submitted the form. This is the same as if you were doing a classic ASP
page.

For LinkButtons, which I believe render anchor tags, the above method
doesn't work but you can check that Request.Form["__EVENTTARGET"] ==
linkButton.UniqueID to see if a particular LinkButton has submitted the
form.

I must recommend that you come up with an architecture that doesn't require
code in the Page.Load based on the ASP.NET Server Control event model since
hard-coding internal framework tokens may be problematic in the future. Try
coming up with a solution that uses the standard event handlers for the
controls instead.
"All controls should be created and added to their parent's Controls
collection on or before Page.PreInit."

Actually, that's not quite right. Any dynamicly created controls must
be created during the load.

Incorrect. It is recommended that controls are added prior to the Load
event of the Page. Look at the initialization code serialized by the
designer in the 1.0 and 1.1 framework. InitializeComponent() has always
been called in the Page.OnInit method.
From your link, in the 'Page
Initialization' section:
"If the current request is a postback, the postback data has not yet
been loaded"

Correct, as of the Page.Init event postback data has not been loaded. This
occurs in the Load event. You want your controls to be created and added to
the Controls collection of their Parents' before the Load event so that
their ViewState is populated in the Load event.
Therefore, you have to wait until the load to read any PostBack info.

Right, but for the wrong reason.

The link I posted contains a section below the section you have read that
describes each event in detail and what operations should be performed in
each event. The table recommends that controls are created and initialized
in the Page.PreInit event.

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top