How to read which control has been clicked, in the Page.Postback event

H

Halvor Nyberg

I have some buttons on a page and want to test in the
postback event, which of them that has been clicked.
Thanks.
 
S

Saravana

From the Page_Load event , you cant directly find control that caused the
postback. But if your control has autopostback property set to true . Then
there will be two hidden variable in your form __EVENTTARGET and
__EVENTARGUMENT which you can access in Page_Load event to get the control
name which caused the post back. __EVENTTARGET will have the object which
fired the post back event.
 
H

Halvor Nyberg

Thanks
I'm a beginner and I use Web matrix to create asp files.

the page load procedure has this syntax:

Sub Page_Load(Sender As Object, E As EventArgs)
If Page.IsPostBack Then
msgbox(E.tostring())
'displays System.Eventargs
end if
end sub

Can I access E to find out which control caused the
Postback?
 
S

Saravana

No, you cant find control in this way. If you try to get e.tostring, it will
give the page class only not the control that caused the postback.
 
A

Abhijit

Hi,
In extension of this, is the Autopostback property
available for buttons, text boxes etc. I believe it is not
so. How does one find the control's Id for common controls
like the button. Also, I was not able to use __EVENTTARGET
in my code, gave build error, i guess it must be bcos
buttons don't support autopostback.
 
K

Keith Smith [MSFT]

To determine which button fired the event from within the eventhandler you must cast the "Sender" parameter to type Button and then access the "ID" property:

Sub Page_Load(Sender As Object, E As EventArgs)
If Page.IsPostBack Then
Dim sourceButton as System.Web.UI.WebControls.Button
sourceButton = CType(Sender, System.Web.UI.WebControls.Button)
MsgBox(sourceButton.ID)
end if
end sub

--------------------
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top