Groups | Blog | Home
all groups > dotnet faqs > november 2003 >

dotnet faqs : vb.net - event handler



Bob
11/21/2003 2:31:45 PM
In the VB.NET Page_Load() function , how can I know which control causes the
page postback?



Bob
11/21/2003 4:22:14 PM
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.

[quoted text, click to view]

Kevin Spencer
11/21/2003 4:53:24 PM
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.

[quoted text, click to view]

Ray Cassick (home)
11/21/2003 11:10:15 PM
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.


[quoted text, click to view]

Kevin Spencer
11/22/2003 9:44:08 AM
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.

[quoted text, click to view]

Cor
11/22/2003 10:15:19 AM
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

[quoted text, click to view]

Bob
11/24/2003 2:33:43 PM
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

Scott M.
11/24/2003 11:55:27 PM
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.


[quoted text, click to view]

Bob
11/25/2003 9:37:08 AM
That is also what I got in webform. SO back to the question. How can I know
which button invoked the button event?

[quoted text, click to view]

Cor
11/25/2003 10:00:09 AM
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

[quoted text, click to view]

Scott M.
11/25/2003 10:26:23 AM
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.

[quoted text, click to view]

Scott M.
11/25/2003 1:28:37 PM
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.


[quoted text, click to view]

Cor
11/25/2003 9:17:57 PM
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

AddThis Social Bookmark Button