473,386 Members | 1,846 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 (follow-up)

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
Jul 21 '05 #1
6 1638
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

Jul 21 '05 #2
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.

Jul 21 '05 #3
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.


Jul 21 '05 #4
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.



Jul 21 '05 #5
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.
>



Jul 21 '05 #6
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
Jul 21 '05 #7

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

Similar topics

7
by: Pavils Jurjans | last post by:
Hallo, I have been programming for restricted environments where Internet Explorer is a standard, so I haven't stumbled upon this problem until now, when I need to write a DOM-compatible code. ...
1
by: Earl Teigrob | last post by:
I did a ton of searching to try and find a simple solution to this issue and finally wrote my own, which I am sharing with everyone. In my searching, I did find a very complete and robust solution at...
15
by: Amit D.Shinde | last post by:
I am adding a new picturebox control at runtime on the form How can i create click event handler for this control Amit Shinde
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: james | last post by:
Hello, I am having a little trouble creating an event handler for a context menu toolstripmenuitem. I've seen various tutorials and so on, but I keep getting a bit stuck! So far I have a second...
6
by: Joseph Geretz | last post by:
I'm porting a C# Outlook Addin originally engineered as a COM Addin over to use VSTO. I've gotten this to the point where my VSTO Addin installs its Menu items and Toolbar buttons when Outlook...
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.
4
by: =?Utf-8?B?TmF2YW5lZXRoLksuTg==?= | last post by:
Hi all, Recently I found an interesting question on C# forums about clearing event handlers of an event. I tried to give it a solution, but failed. I am interested to know how you guys take...
2
by: wolverine | last post by:
Hi All, In Mozilla Firefox, to onblur and onfocus event of each and every html element, the browser itself will attach a native event handler. I mean if you type,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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
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,...

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.