473,626 Members | 3,247 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2057
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(EventArg s e)
{
if (!IsPostBack)
// first time page is created execute the following code:
{
DoStuff();
}
}

private void SomeControl_Cli ck(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(EventArg s e)
{
if (!IsPostBack)
// first time page is created execute the following code:
{
DoStuff();
}
}

private void SomeControl_Cli ck(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["__EVENTTAR GET"] ==
linkButton.Uniq ueID 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. InitializeCompo nent() 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
10795
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 then like that, when clicking the LinkButton, the user can be navigated to another page, carrying a variable. I would like to use server.transfer method instead of QueryString as I don't want the carried variable to be visible for the user.
1
1481
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 Form by Reflection, it is in a ClassLibrary that has to be loaded dynamically. I first tried it without the ClassLibrary and it works. I give the OCX to the Subform, declare my Eventhandlers there and bind the ocx to these eventhandlers, no problem,...
3
1877
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. There must be a better way to code this than calling OnResetTimer in all these procedures separately. Is there not a way to use one handler for all of them? private void pnlCPI_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)...
1
1471
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 this Control But when I click on this button, the only thing I notice is there is a post, but the eventhandler is never fired. :-( Is it possible that a dynmicly created control looses his event after an PostBack??
1
1353
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 custom name/value pairs established in web.config (e.g. <add key="foo" value="bar" /> ) using ConfigurationSettings.AppSettings("foo") syntax? Thanks Timo
3
2950
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 Delegates which can get this back, but couldN#t found out how it works.
1
1203
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++) { Button b = new Button();
1
3105
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 list. The problem happens when: - I use such a BindingList as DataSource for a BindingSource. - Add this BindingSource as DataSource for a DataGridView. - And go to the NewRow and add a new row in the DataGridView.
1
2173
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 create html with the information in the responsexml. This works fine with firefox and Opera, but IE doesn't do anything with these events (onclick, onmouseover, ...) on an image or cell (td) created this way. One alternative is that I should be...
4
5044
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 value then put it back as the oncontextmenu attribute. Here's an example page if you want to try it out... <html> <head> <titletesting </title> <script><!--
0
8269
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8203
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8711
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8368
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8512
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5576
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4094
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2630
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1815
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.