vb.net - event handler

B

Bob

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

Kevin Spencer

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.
 
B

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.
 
R

Ray Cassick \(home\)

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.
 
C

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
 
K

Kevin Spencer

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.
 
B

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
 
S

Scott M.

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.
 
C

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
 
S

Scott M.

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.
 
B

Bob

That is also what I got in webform. SO back to the question. How can I know
which button invoked the button event?
 
S

Scott M.

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.
 
C

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
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top