473,386 Members | 1,828 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,386 software developers and data experts.

vb.net - event handler

Bob
In the VB.NET Page_Load() function , how can I know which control causes the
page postback?


Nov 20 '05 #1
12 2632
Assuming that the Control caused a PostBack because of an event, you should
know by which Event Handler is called. For example, you could have the Event
Handler for each Control set a field or property in your class.

--
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"Bob" <bo******@yahoo.com> wrote in message
news:OV**************@TK2MSFTNGP10.phx.gbl...
In the VB.NET Page_Load() function , how can I know which control causes the page postback?

Nov 20 '05 #2
Bob
Your assumption is correct. However, when the control's Event Handler set
class's field or property, it is supposed to be AFTER the Page_Load function
is called during a postback, isn't it? So in Page_Load function, we still
don't know which control causes the PostBack.

Thanks.

"Kevin Spencer" <ke***@takempis.com> wrote in message
news:#2**************@TK2MSFTNGP12.phx.gbl...
Assuming that the Control caused a PostBack because of an event, you should know by which Event Handler is called. For example, you could have the Event Handler for each Control set a field or property in your class.

--
Kevin Spencer
.Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"Bob" <bo******@yahoo.com> wrote in message
news:OV**************@TK2MSFTNGP10.phx.gbl...
In the VB.NET Page_Load() function , how can I know which control causes

the
page postback?


Nov 20 '05 #3
I don't think that is available when the page returns from the postback.

Keeping a simple var should do the trick though. Track it your self.
"Bob" <bo******@yahoo.com> wrote in message
news:#w**************@tk2msftngp13.phx.gbl...
Your assumption is correct. However, when the control's Event Handler set
class's field or property, it is supposed to be AFTER the Page_Load function is called during a postback, isn't it? So in Page_Load function, we still
don't know which control causes the PostBack.

Thanks.

"Kevin Spencer" <ke***@takempis.com> wrote in message
news:#2**************@TK2MSFTNGP12.phx.gbl...
Assuming that the Control caused a PostBack because of an event, you

should
know by which Event Handler is called. For example, you could have the

Event
Handler for each Control set a field or property in your class.

--
Kevin Spencer
.Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"Bob" <bo******@yahoo.com> wrote in message
news:OV**************@TK2MSFTNGP10.phx.gbl...
In the VB.NET Page_Load() function , how can I know which control
causes the
page postback?



Nov 20 '05 #4
Cor
Hi Ray,

Keeping it simple is what I thought also.

Why would you need that if you have a "not ispostback" and can catch all
events with the same structure if you want together in one sub"

But tracking it yourself is in my idea impossible (if there is more than one
control on a page), the idea of an "event" is that you cannot track it in
the future. It happens mostly because the user fires it. I find the
mechanisme to catch that well done made.

Just a thought,

Cor
I don't think that is available when the page returns from the postback.
Keeping a simple var should do the trick though. Track it your self.

Nov 20 '05 #5
Well, you didn't specify WHEN you wanted to get the data. If you need to
find out before the event is fired and handled, you can check
Request.Form("__EVENTTARGET") - this will have the ID of the control which
caused the PostBack in it.

--
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"Bob" <bo******@yahoo.com> wrote in message
news:#w**************@tk2msftngp13.phx.gbl...
Your assumption is correct. However, when the control's Event Handler set
class's field or property, it is supposed to be AFTER the Page_Load function is called during a postback, isn't it? So in Page_Load function, we still
don't know which control causes the PostBack.

Thanks.

"Kevin Spencer" <ke***@takempis.com> wrote in message
news:#2**************@TK2MSFTNGP12.phx.gbl...
Assuming that the Control caused a PostBack because of an event, you

should
know by which Event Handler is called. For example, you could have the

Event
Handler for each Control set a field or property in your class.

--
Kevin Spencer
.Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"Bob" <bo******@yahoo.com> wrote in message
news:OV**************@TK2MSFTNGP10.phx.gbl...
In the VB.NET Page_Load() function , how can I know which control
causes the
page postback?



Nov 20 '05 #6
Bob
Hello,

For web controls which can invoke __doPostBack. At the page load, I
supposedly can know who caused the PostBack by checking
Request.Form("__EVENTTARGET"). It does work for the dropdownlist, however
it doesn't work for the button. When the button is clicked, on page load,
Request.Form("__EVENTTARGET") ="". If I call
Page.RegisterHiddenField("__EVENTTARGET", "FakeButton"), then when the
button is clicked, Request.Form("__EVENTTARGET") ="FakeButton". It looks
like when the button on the page is clicked, ASP.NET does NOT ignore the
hidden field in favor of the actual button click.

Is there something I missed? Thanks for your input.

Here is the code snippets.

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Page.RegisterHiddenField("__EVENTTARGET", "FakeButton")

If Request.Form("__EVENTTARGET") = "Button1" Then
'do one sth
ElseIf Request.Form("__EVENTTARGET") = "Button2" Then
'do another sth
End If

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim b As Button = CType(sender, Button)
Label1.Text = "You clicked " & b.ID
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Label1.Text = "You clicked " & sender.ID
End Sub
Nov 20 '05 #7
Why not just check the "sender" event argument of the Page_Load event? This
is what it is there for. Just check sender.getType.toString and then you'll
know what kind of control caused the postback. Then you could cast sender
into that type and check its name.
"Bob" <bo******@yahoo.com> wrote in message
news:eb**************@TK2MSFTNGP10.phx.gbl...
Hello,

For web controls which can invoke __doPostBack. At the page load, I
supposedly can know who caused the PostBack by checking
Request.Form("__EVENTTARGET"). It does work for the dropdownlist, however
it doesn't work for the button. When the button is clicked, on page load,
Request.Form("__EVENTTARGET") ="". If I call
Page.RegisterHiddenField("__EVENTTARGET", "FakeButton"), then when the
button is clicked, Request.Form("__EVENTTARGET") ="FakeButton". It looks
like when the button on the page is clicked, ASP.NET does NOT ignore the
hidden field in favor of the actual button click.

Is there something I missed? Thanks for your input.

Here is the code snippets.

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Page.RegisterHiddenField("__EVENTTARGET", "FakeButton")

If Request.Form("__EVENTTARGET") = "Button1" Then
'do one sth
ElseIf Request.Form("__EVENTTARGET") = "Button2" Then
'do another sth
End If

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim b As Button = CType(sender, Button)
Label1.Text = "You clicked " & b.ID
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Label1.Text = "You clicked " & sender.ID
End Sub

Nov 20 '05 #8
Cor
Hi Scott,

While I did not check all the other methods, because I think it becomes a
lot of rubish in the load event, while there is a good methode, that only
needs three lines.
In VB.net as example that is
\\\
sub from page control event
todo
end sub
///

Did I test your sample because I could not believe it, but you never knows.
(getting the type of sender is in webforms not that simple as in
windowforms)

I did try it with only one serverside button on the page
\\\
Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim a As String = sender.GetType.ToString
'returns always "asp.webform1.aspx"
End Sub
///

When I did understand something wrong let me know?

Cor
Why not just check the "sender" event argument of the Page_Load event? This is what it is there for. Just check sender.getType.toString and then you'll know what kind of control caused the postback. Then you could cast sender
into that type and check its name.

Nov 20 '05 #9
No, you are right Cor. I have used sender in Windows Forms projects without
a problem and just assumed that it would work the same way in on a Web Form.

Thanks.

"Cor" <no*@non.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Hi Scott,

While I did not check all the other methods, because I think it becomes a
lot of rubish in the load event, while there is a good methode, that only
needs three lines.
In VB.net as example that is
\\\
sub from page control event
todo
end sub
///

Did I test your sample because I could not believe it, but you never knows. (getting the type of sender is in webforms not that simple as in
windowforms)

I did try it with only one serverside button on the page
\\\
Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim a As String = sender.GetType.ToString
'returns always "asp.webform1.aspx"
End Sub
///

When I did understand something wrong let me know?

Cor
Why not just check the "sender" event argument of the Page_Load event?

This
is what it is there for. Just check sender.getType.toString and then

you'll
know what kind of control caused the postback. Then you could cast sender into that type and check its name.


Nov 20 '05 #10
Bob
That is also what I got in webform. SO back to the question. How can I know
which button invoked the button event?

"Scott M." <s-***@badspamsnet.net> wrote in message
news:#u**************@TK2MSFTNGP09.phx.gbl...
No, you are right Cor. I have used sender in Windows Forms projects without a problem and just assumed that it would work the same way in on a Web Form.
Thanks.

"Cor" <no*@non.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Hi Scott,

While I did not check all the other methods, because I think it becomes a
lot of rubish in the load event, while there is a good methode, that only needs three lines.
In VB.net as example that is
\\\
sub from page control event
todo
end sub
///

Did I test your sample because I could not believe it, but you never

knows.
(getting the type of sender is in webforms not that simple as in
windowforms)

I did try it with only one serverside button on the page
\\\
Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim a As String = sender.GetType.ToString
'returns always "asp.webform1.aspx"
End Sub
///

When I did understand something wrong let me know?

Cor
Why not just check the "sender" event argument of the Page_Load event?

This
is what it is there for. Just check sender.getType.toString and then

you'll
know what kind of control caused the postback. Then you could cast

sender into that type and check its name.



Nov 20 '05 #11
You could add a custom value to VIEWSTATE that differs depending on what
control was interacted with:

ViewState.Add("EventStartedBy", controlName)

You could then check the "EventStartedBy" key in ViewState on the PostBack
via a Select statement to see what its value is.

I'm sure there's got to be another way, but this should work.
"Bob" <bo******@yahoo.com> wrote in message
news:OT**************@TK2MSFTNGP10.phx.gbl...
That is also what I got in webform. SO back to the question. How can I know which button invoked the button event?

"Scott M." <s-***@badspamsnet.net> wrote in message
news:#u**************@TK2MSFTNGP09.phx.gbl...
No, you are right Cor. I have used sender in Windows Forms projects without
a problem and just assumed that it would work the same way in on a Web

Form.

Thanks.

"Cor" <no*@non.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Hi Scott,

While I did not check all the other methods, because I think it becomes a
lot of rubish in the load event, while there is a good methode, that only needs three lines.
In VB.net as example that is
\\\
sub from page control event
todo
end sub
///

Did I test your sample because I could not believe it, but you never

knows.
(getting the type of sender is in webforms not that simple as in
windowforms)

I did try it with only one serverside button on the page
\\\
Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim a As String = sender.GetType.ToString
'returns always "asp.webform1.aspx"
End Sub
///

When I did understand something wrong let me know?

Cor

> Why not just check the "sender" event argument of the Page_Load

event? This
> is what it is there for. Just check sender.getType.toString and then you'll
> know what kind of control caused the postback. Then you could cast

sender
> into that type and check its name.
>



Nov 20 '05 #12
Cor
Hi Bob,

Do you want to know what button did do the post back without to test them
all, or do you want absolute to test them in the load event.

For the first you can use dynamically made buttons and test it only once
(and I have made an example if you want) for the second I have no solution
and I think that will absolute become a big hash in the load event (I was
also thinking like Scott on the viewstate for that).

Cor
Nov 20 '05 #13

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

Similar topics

10
by: tony kulik | last post by:
This code works fine in ie and opera but not at all in Mozilla. Anybody got a clue as to how to get it right? <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <script...
18
by: Christopher W. Douglas | last post by:
I am writing a VB.NET application in Visual Studio 2003. I have written a method that handles several events, such as closing a form and changing the visible status of a form. I have some code...
8
by: Mark | last post by:
Hi, I'm looking for some ideas on how to build a very simple Event processing framework in my C++ app. Here is a quick background ... I'm building a multithreaded app in C++ (on Linux) that...
6
by: vbMark | last post by:
If I have a control, for example a CheckedListBox, how do I add and event to code, for example that a box has been checked by the user? Thanks
13
by: Charles Law | last post by:
Mr "yEaH rIgHt" posted the following link about a week ago in answer to my question about removing event handlers. > http://www.vbinfozine.com/t_bindevt.shtml Following on from that post, the...
3
by: Beth | last post by:
in the following: this.ExitButton.Click += new System.EventHandler(this.ExitButton_Click); if I saw an equation, such as y +=x; then y = y+x. But what is the meaning in the event handler. I...
5
by: Richard Grant | last post by:
Hi, I need to "save" in a variable the event handler sub of a control's event, then perform some process, and finally "restore" the originally saved event handler. Example in pseudo-code: 1)...
1
by: tdan | last post by:
I do not know how to get Event.stopObserving() to work in the context I am using it. I am displaying a Color Selection Table and attaching 2 events: 1. onmouseover to display the color to the user...
2
by: John Kotuby | last post by:
Hi guys, I am converting a rather complicated database driven Web application from classic ASP to ASP.NET 2.0 using VB 2005 as the programming language. The original ASP application works quite...
2
by: =?Utf-8?B?UlJK?= | last post by:
Hello, I would like to add the Paint handler to the form. How to do it? Thanks in advance.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...

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.