473,395 Members | 1,460 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,395 software developers and data experts.

Finding Controls

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
Nov 19 '05 #1
6 1128
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" <b.*********@gmail.com> wrote in message
news:a7**************************@posting.google.c 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

Nov 19 '05 #2
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" <re*************@monarchmed.com> wrote in message
news:OC**************@tk2msftngp13.phx.gbl...
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" <b.*********@gmail.com> wrote in message
news:a7**************************@posting.google.c 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


Nov 19 '05 #3
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:%2****************@tk2msftngp13.phx.gbl...
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" <re*************@monarchmed.com> wrote in message
news:OC**************@tk2msftngp13.phx.gbl...
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" <b.*********@gmail.com> wrote in message
news:a7**************************@posting.google.c 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



Nov 19 '05 #4
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" <re*************@monarchmed.com> wrote in message
news:e6**************@TK2MSFTNGP15.phx.gbl...
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:%2****************@tk2msftngp13.phx.gbl...
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" <re*************@monarchmed.com> wrote in message
news:OC**************@tk2msftngp13.phx.gbl...
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" <b.*********@gmail.com> wrote in message
news:a7**************************@posting.google.c 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



Nov 19 '05 #5
That sounds very good.

Eliyahu

"William F. Robertson, Jr." <th****@nameht.org> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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" <re*************@monarchmed.com> wrote in message
news:e6**************@TK2MSFTNGP15.phx.gbl...
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:%2****************@tk2msftngp13.phx.gbl...
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" <re*************@monarchmed.com> wrote in message
news:OC**************@tk2msftngp13.phx.gbl...
> 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" <b.*********@gmail.com> wrote in message
> news:a7**************************@posting.google.c 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
>
>



Nov 19 '05 #6
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!
Nov 19 '05 #7

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

Similar topics

3
by: Rob | last post by:
You can find datagrid in page by refering the form. Gatagrid is a child control of Form. Here is the code ----------------- Dim ctl As New Control For Each ctl In...
1
by: Rick Spiewak | last post by:
I have been unable to find an elegant way to navigate to the controls in a DataGrid in edit mode in order to get the changed values. I end up with a lot of code like: ...
3
by: Simon Harvey | last post by:
Hi All. In one of my user controls I add a textbox to a placeholder sitting on the user control. txtUsername = new TextBox(); txtUsername.ID = "txtUsername";...
7
by: BradC | last post by:
(VB.NET 2002, Windows app). I'm going to be provided a two-letter string like "BV" or "TP" that represents a location. I then need to perform some actions on several form controls that have...
3
by: Tor Inge Rislaa | last post by:
Finding name and type In the activate procedure of a form I want to write to the debug window, name and type of all controls at that actual form. Is there a smart way to do that? Allso for...
5
by: Tosch | last post by:
Is there a way to find out which events are attached to a control (say 'validating' event for a textbox) and call the event? Tosch
2
by: Alex Maghen | last post by:
I want to create a utility function that will seach the current page for one of my UserControls by it's type. So, let's say that I have a UserControl whose class I defined as follows: namespace...
7
by: Brad Baker | last post by:
I am trying to programmatically set a placeholder control in csharp which is nested inside a repeater control between <ItemTemplateand </ItemTemplate> tags, however I am running into problems. I've...
1
by: Mufasa | last post by:
I have a page that has a master page. I'm writing generic code to find multiple controls on the page (I have a number of controls called tbName1, tbName2, tbName3, ..., tbName20) and rather than...
4
by: karthik25 | last post by:
Hi All, I have a problem in finding control in a dynamically created updated panel. I have given the code below. Following is just a starting effort in a completely dynamic user control. I am...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.