Finding Controls

J

Jithu

hi,

I have a dropdownlist and a button in my web page. Both controls
fire the page_load event on postback. I want to find out which control
does the postback?

I have a function in the page_load event which should be executed only
when the button is clicked and not on the selectedindexChanged event
of the DDL. Also the same function should be executed on Not
Page.ispostback.

thanks in advance,
jithu
 
E

Eliyahu Goldin

With autopostback property set to true, there will be a hidden variable
__EVENTTARGET in your form. You can access it in Page_Load event to get the
name of the control which caused the postback.

Eliyahu
 
K

Karl Seguin

Jithu:

__EVENTTARGET will be blank when a button causes the postback...this is all
fine and dandy when you only have a single button, because you can do:

if Page.IsPostBack AndAlso Request.Form("__EVENTTARGET") is nothing then
'you know the button was pressed
end if

but if you have 2+ buttons, it gets to be a pretty big problem.

The question should be, why do you need to know in Page_Load which event
caused postback? Why can't you just use the normal event paradigm:

Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles MyBase.Load

If Not Page.IsPostBack Then
CallYourFunction()
End If
End Sub

Private Sub button_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button.Click
CallYourFunction()
End Sub

Private Sub dropdown_SelectedIndexChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles dropdown.SelectedIndexChanged
'Do something else
End Sub

I'm not saying this works 100% of the time, but it should be the rule...and
I'd like to know more about why an exception is needed in this case.

Karl
 
E

Eliyahu Goldin

Karl,

Questions on how to know what caused postback are asked here so often that
you can't call this need an exception anymore. I think the major reason for
that is databinding. Very often you need to know what caused postback to
modify your select statement or to perform some other database operations
like Delete. And OnLoad event is the most common place to call DataBind
method. As you know, OnClick event is processed after OnLoad, it's already
too late.

I personally don't use __EVENTTARGET much. Rather my client-side code sets
required action code in a hidden text control.

Eliyahu
 
W

William F. Robertson, Jr.

In my development group I have created an event called AfterEvents. This
event fires just before PreRender. All of my DataBinding logic goes in
there. I can determine what click events have fired, as AfterEvents fires
after Load and PostBackEvents.
 
E

Eliyahu Goldin

That sounds very good.

Eliyahu

William F. Robertson said:
In my development group I have created an event called AfterEvents. This
event fires just before PreRender. All of my DataBinding logic goes in
there. I can determine what click events have fired, as AfterEvents fires
after Load and PostBackEvents.
 
J

Jithendra Balakrishnan

Hi group,

Thanks a lot for the replies and sorry for the delayed response.

As Eliyahu rightly said, the reason for me to find the controls is
for databinding. And i have done it using client side scripts by having
a hidden input control and setting its values accordingly. In the
pageload event i find the value of the input control and do the binding
accordingly.

Karl and Eliyahu, thanks for telling me about this _eventtarget
attribute as i think i can use it coz i have only one button in the
page.

To William: Would love to know much about the afterevents event as i
believe would be of help to most of us rookies.

Again thanks a lot for the replies and Happy .neting!
 

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,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top