Finding Controls

Discussion in 'ASP .Net' started by Jithu, Feb 16, 2005.

  1. Jithu

    Jithu Guest

    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
     
    Jithu, Feb 16, 2005
    #1
    1. Advertising

  2. 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" <> wrote in message
    news:...
    > 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
     
    Eliyahu Goldin, Feb 16, 2005
    #2
    1. Advertising

  3. Jithu

    Karl Seguin Guest

    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" <> wrote in message
    news:...
    > 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" <> wrote in message
    > news:...
    > > 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

    >
    >
     
    Karl Seguin, Feb 16, 2005
    #3
  4. 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:%...
    > 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" <> wrote in message
    > news:...
    > > 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" <> wrote in message
    > > news:...
    > > > 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

    > >
    > >

    >
    >
     
    Eliyahu Goldin, Feb 16, 2005
    #4
  5. 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" <> wrote in message
    news:...
    > 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:%...
    > > 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" <> wrote in message
    > > news:...
    > > > 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" <> wrote in message
    > > > news:...
    > > > > 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
    > > >
    > > >

    > >
    > >

    >
    >
     
    William F. Robertson, Jr., Feb 16, 2005
    #5
  6. That sounds very good.

    Eliyahu

    "William F. Robertson, Jr." <> wrote in message
    news:%...
    > 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" <> wrote in message
    > news:...
    > > 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:%...
    > > > 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" <> wrote in message
    > > > news:...
    > > > > 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" <> wrote in message
    > > > > news:...
    > > > > > 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
    > > > >
    > > > >
    > > >
    > > >

    > >
    > >

    >
    >
     
    Eliyahu Goldin, Feb 16, 2005
    #6
  7. 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!
     
    Jithendra Balakrishnan, Mar 5, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rob
    Replies:
    3
    Views:
    462
  2. Rick Spiewak

    Finding Grid controls

    Rick Spiewak, Jul 24, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    412
    Guest
    Jul 25, 2003
  3. Elizabeth Harmon

    Finding Controls in a Place Holder

    Elizabeth Harmon, Nov 13, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    407
    George Paiva
    Nov 13, 2003
  4. =?Utf-8?B?Q0ogU21pdA==?=

    Finding Dynamically Created DataGrid Controls

    =?Utf-8?B?Q0ogU21pdA==?=, Feb 25, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    443
    Steven Cheng[MSFT]
    Feb 26, 2004
  5. Nathan Sokalski
    Replies:
    4
    Views:
    1,171
    Muhammad Naveed Yaseen
    Sep 5, 2007
Loading...

Share This Page