473,405 Members | 2,176 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Problem with EventHandlers

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:

Expand|Select|Wrap|Line Numbers
  1. string et = Request.Form["__EVENTTARGET"];
  2. if ((IsPostBack) && (et == ""))
  3. ExecuteCustomSearch();
  4.  
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.

Jun 21 '06 #1
3 2045
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:

Expand|Select|Wrap|Line Numbers
  1.  string et = Request.Form["__EVENTTARGET"];
  2.  if ((IsPostBack) && (et == ""))
  3.     ExecuteCustomSearch();
  4.  

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
Jun 21 '06 #2
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 Sexton wrote:
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:

Expand|Select|Wrap|Line Numbers
  1.  > string et = Request.Form["__EVENTTARGET"];
  2.  > if ((IsPostBack) && (et == ""))
  3.  >    ExecuteCustomSearch();
  4.  > 

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


Jun 21 '06 #3
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
Jun 21 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Kiyomi | last post by:
Hello, I create a Table1 dynamically at run time, and at the same time, I would like to create LinkButton controls, also dynamically, and insert them into each line in my Table1. I would...
1
by: Matthias Pieroth | last post by:
Hi NG, I have an OCX (VC++ 6.0) that fires some events. I have to use it on a form an have to catch the events in event-handlers in another form (MDI-Child). BUT: I have to create the other...
3
by: Robert | last post by:
I need some assistance doing some "right way to do it" coding. The following are EventHandlers associated with Delegates in a child form that call a procedure in the MDI form that resets a timer....
1
by: Kristof Pauwels | last post by:
Hellow, I have this peace of code from an ASP.NET page for a Mobile device, but i have a small problem. In this first SUB I dynmamicly create a MobileControls.Command. I add an EventHandler to...
1
by: Timo | last post by:
I haven't tried coding eventhandlers in Global.asax yet -- any "gotchas" I should be aware of? Do programming errors there require bouncing IIS? Will handlers in Global.asax be able to access...
3
by: Armin | last post by:
Hello I have a UserControl with a Click Event. Is it possible to find out the List of all Delegates/Eventhandlers using the Event. I read something about a "getinvocationlist" Methode for...
1
by: Kasper Birch Olsen | last post by:
Hi NG Im adding a bunch of linkbuttons to a page, in a for loop, but I cant get the eventhandlers to work. A simplyfied version of the code looks like this: for (int i = 0; i<10; i++) {...
1
by: Pieter | last post by:
Hi, I have a custom List that inherits from BindingList. It has some methods overloaded, like the Add/Insert/etc to add and remove some eventhandlers when adding or removing an item T of the...
1
by: Pugi! | last post by:
Currently I am using a responsexml for an xmlhttprequest. The information is then processed by javascript : elements are created with attributes incl events using setAttribute; in other words I...
4
by: ICPooreMan | last post by:
I've got some code which works in firefox that's giving me fits in IE7 (maybe other versions too I haven't tested it). What I want to do is get the oncontextmenu attribute of something, change the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.