Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Finding Controls

Reply
Thread Tools

Finding Controls

 
 
Jithu
Guest
Posts: n/a
 
      02-16-2005
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
 
Reply With Quote
 
 
 
 
Eliyahu Goldin
Guest
Posts: n/a
 
      02-16-2005
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

"Jithu" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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



 
Reply With Quote
 
 
 
 
Karl Seguin
Guest
Posts: n/a
 
      02-16-2005
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
--
MY ASP.Net tutorials
http://www.openmymind.net/


"Eliyahu Goldin" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
> "Jithu" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) om...
> > 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

>
>



 
Reply With Quote
 
Eliyahu Goldin
Guest
Posts: n/a
 
      02-16-2005
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

"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:%(E-Mail Removed)...
> 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
> --
> MY ASP.Net tutorials
> http://www.openmymind.net/
>
>
> "Eliyahu Goldin" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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
> >
> > "Jithu" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed) om...
> > > 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

> >
> >

>
>



 
Reply With Quote
 
William F. Robertson, Jr.
Guest
Posts: n/a
 
      02-16-2005
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.



"Eliyahu Goldin" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
> "Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
> wrote in message news:%(E-Mail Removed)...
> > 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
> > --
> > MY ASP.Net tutorials
> > http://www.openmymind.net/
> >
> >
> > "Eliyahu Goldin" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed)...
> > > 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
> > >
> > > "Jithu" <(E-Mail Removed)> wrote in message
> > > news:(E-Mail Removed) om...
> > > > 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
> > >
> > >

> >
> >

>
>



 
Reply With Quote
 
Eliyahu Goldin
Guest
Posts: n/a
 
      02-16-2005
That sounds very good.

Eliyahu

"William F. Robertson, Jr." <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> 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.
>
>
>
> "Eliyahu Goldin" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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
> >
> > "Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
> > wrote in message news:%(E-Mail Removed)...
> > > 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
> > > --
> > > MY ASP.Net tutorials
> > > http://www.openmymind.net/
> > >
> > >
> > > "Eliyahu Goldin" <(E-Mail Removed)> wrote in message
> > > news:(E-Mail Removed)...
> > > > 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
> > > >
> > > > "Jithu" <(E-Mail Removed)> wrote in message
> > > > news:(E-Mail Removed) om...
> > > > > 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
> > > >
> > > >
> > >
> > >

> >
> >

>
>



 
Reply With Quote
 
Jithendra Balakrishnan
Guest
Posts: n/a
 
      03-05-2005
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!





*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Controls.Count, Controls.IsSynchronized, and Controls.SyncRoot Nathan Sokalski ASP .Net 4 09-05-2007 03:27 AM
Finding Server... Finding Host.... enough already!!! Leesa_Tay@softhome.net Computer Support 2 01-20-2006 10:23 AM
Finding Controls in a Place Holder Elizabeth Harmon ASP .Net 1 11-13-2003 03:23 PM
Finding Grid controls Rick Spiewak ASP .Net 1 07-24-2003 11:12 PM
Problem finding datagrid in Page.controls collection Rob ASP .Net 3 07-19-2003 05:17 PM



Advertisments