vb.net - event handler (follow-up)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bob

    #1

    vb.net - event handler (follow-up)

    Hello,

    For web controls which can invoke __doPostBack. At the page load, I
    supposedly can know who caused the PostBack by checking
    Request.Form("_ _EVENTTARGET"). It does work for the dropdownlist, however
    it doesn't work for the button. When the button is clicked, on page load,
    Request.Form("_ _EVENTTARGET") ="". If I call
    Page.RegisterHi ddenField("__EV ENTTARGET", "FakeButton "), then when the
    button is clicked, Request.Form("_ _EVENTTARGET") ="FakeButton ". It looks
    like when the button on the page is clicked, ASP.NET does NOT ignore the
    hidden field in favor of the actual button click.

    Is there something I missed? Thanks for your input.

    Here is the code snippets.

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArg s) Handles MyBase.Load
    'Put user code to initialize the page here
    Page.RegisterHi ddenField("__EV ENTTARGET", "FakeButton ")

    If Request.Form("_ _EVENTTARGET") = "Button1" Then
    'do one sth
    ElseIf Request.Form("_ _EVENTTARGET") = "Button2" Then
    'do another sth
    End If

    End Sub

    Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
    System.EventArg s) Handles Button1.Click
    Dim b As Button = CType(sender, Button)
    Label1.Text = "You clicked " & b.ID
    End Sub

    Private Sub Button2_Click(B yVal sender As System.Object, ByVal e As
    System.EventArg s) Handles Button2.Click
    Label1.Text = "You clicked " & sender.ID
    End Sub


  • Scott M.

    #2
    Re: vb.net - event handler (follow-up)

    Why not just check the "sender" event argument of the Page_Load event? This
    is what it is there for. Just check sender.getType. toString and then you'll
    know what kind of control caused the postback. Then you could cast sender
    into that type and check its name.


    "Bob" <bobjiang@yahoo .com> wrote in message
    news:ebrJCpssDH A.2392@TK2MSFTN GP10.phx.gbl...[color=blue]
    > Hello,
    >
    > For web controls which can invoke __doPostBack. At the page load, I
    > supposedly can know who caused the PostBack by checking
    > Request.Form("_ _EVENTTARGET"). It does work for the dropdownlist, however
    > it doesn't work for the button. When the button is clicked, on page load,
    > Request.Form("_ _EVENTTARGET") ="". If I call
    > Page.RegisterHi ddenField("__EV ENTTARGET", "FakeButton "), then when the
    > button is clicked, Request.Form("_ _EVENTTARGET") ="FakeButton ". It looks
    > like when the button on the page is clicked, ASP.NET does NOT ignore the
    > hidden field in favor of the actual button click.
    >
    > Is there something I missed? Thanks for your input.
    >
    > Here is the code snippets.
    >
    > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    > System.EventArg s) Handles MyBase.Load
    > 'Put user code to initialize the page here
    > Page.RegisterHi ddenField("__EV ENTTARGET", "FakeButton ")
    >
    > If Request.Form("_ _EVENTTARGET") = "Button1" Then
    > 'do one sth
    > ElseIf Request.Form("_ _EVENTTARGET") = "Button2" Then
    > 'do another sth
    > End If
    >
    > End Sub
    >
    > Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
    > System.EventArg s) Handles Button1.Click
    > Dim b As Button = CType(sender, Button)
    > Label1.Text = "You clicked " & b.ID
    > End Sub
    >
    > Private Sub Button2_Click(B yVal sender As System.Object, ByVal e As
    > System.EventArg s) Handles Button2.Click
    > Label1.Text = "You clicked " & sender.ID
    > End Sub
    >
    >[/color]


    Comment

    • Cor

      #3
      Re: vb.net - event handler (follow-up)

      Hi Scott,

      While I did not check all the other methods, because I think it becomes a
      lot of rubish in the load event, while there is a good methode, that only
      needs three lines.
      In VB.net as example that is
      \\\
      sub from page control event
      todo
      end sub
      ///

      Did I test your sample because I could not believe it, but you never knows.
      (getting the type of sender is in webforms not that simple as in
      windowforms)

      I did try it with only one serverside button on the page
      \\\
      Private Sub Page_Load(ByVal sender As System.Object, _
      ByVal e As System.EventArg s) Handles MyBase.Load
      Dim a As String = sender.GetType. ToString
      'returns always "asp.webform1.a spx"
      End Sub
      ///

      When I did understand something wrong let me know?

      Cor
      [color=blue]
      > Why not just check the "sender" event argument of the Page_Load event?[/color]
      This[color=blue]
      > is what it is there for. Just check sender.getType. toString and then[/color]
      you'll[color=blue]
      > know what kind of control caused the postback. Then you could cast sender
      > into that type and check its name.
      >[/color]


      Comment

      • Scott M.

        #4
        Re: vb.net - event handler (follow-up)

        No, you are right Cor. I have used sender in Windows Forms projects without
        a problem and just assumed that it would work the same way in on a Web Form.

        Thanks.

        "Cor" <non@non.com> wrote in message
        news:%2307UqKzs DHA.2448@TK2MSF TNGP09.phx.gbl. ..[color=blue]
        > Hi Scott,
        >
        > While I did not check all the other methods, because I think it becomes a
        > lot of rubish in the load event, while there is a good methode, that only
        > needs three lines.
        > In VB.net as example that is
        > \\\
        > sub from page control event
        > todo
        > end sub
        > ///
        >
        > Did I test your sample because I could not believe it, but you never[/color]
        knows.[color=blue]
        > (getting the type of sender is in webforms not that simple as in
        > windowforms)
        >
        > I did try it with only one serverside button on the page
        > \\\
        > Private Sub Page_Load(ByVal sender As System.Object, _
        > ByVal e As System.EventArg s) Handles MyBase.Load
        > Dim a As String = sender.GetType. ToString
        > 'returns always "asp.webform1.a spx"
        > End Sub
        > ///
        >
        > When I did understand something wrong let me know?
        >
        > Cor
        >[color=green]
        > > Why not just check the "sender" event argument of the Page_Load event?[/color]
        > This[color=green]
        > > is what it is there for. Just check sender.getType. toString and then[/color]
        > you'll[color=green]
        > > know what kind of control caused the postback. Then you could cast[/color][/color]
        sender[color=blue][color=green]
        > > into that type and check its name.
        > >[/color]
        >
        >[/color]


        Comment

        • Bob

          #5
          Re: vb.net - event handler (follow-up)

          That is also what I got in webform. SO back to the question. How can I know
          which button invoked the button event?

          "Scott M." <s-mar@badspamsnet .net> wrote in message
          news:#uKU9i2sDH A.2348@TK2MSFTN GP09.phx.gbl...[color=blue]
          > No, you are right Cor. I have used sender in Windows Forms projects[/color]
          without[color=blue]
          > a problem and just assumed that it would work the same way in on a Web[/color]
          Form.[color=blue]
          >
          > Thanks.
          >
          > "Cor" <non@non.com> wrote in message
          > news:%2307UqKzs DHA.2448@TK2MSF TNGP09.phx.gbl. ..[color=green]
          > > Hi Scott,
          > >
          > > While I did not check all the other methods, because I think it becomes[/color][/color]
          a[color=blue][color=green]
          > > lot of rubish in the load event, while there is a good methode, that[/color][/color]
          only[color=blue][color=green]
          > > needs three lines.
          > > In VB.net as example that is
          > > \\\
          > > sub from page control event
          > > todo
          > > end sub
          > > ///
          > >
          > > Did I test your sample because I could not believe it, but you never[/color]
          > knows.[color=green]
          > > (getting the type of sender is in webforms not that simple as in
          > > windowforms)
          > >
          > > I did try it with only one serverside button on the page
          > > \\\
          > > Private Sub Page_Load(ByVal sender As System.Object, _
          > > ByVal e As System.EventArg s) Handles MyBase.Load
          > > Dim a As String = sender.GetType. ToString
          > > 'returns always "asp.webform1.a spx"
          > > End Sub
          > > ///
          > >
          > > When I did understand something wrong let me know?
          > >
          > > Cor
          > >[color=darkred]
          > > > Why not just check the "sender" event argument of the Page_Load event?[/color]
          > > This[color=darkred]
          > > > is what it is there for. Just check sender.getType. toString and then[/color]
          > > you'll[color=darkred]
          > > > know what kind of control caused the postback. Then you could cast[/color][/color]
          > sender[color=green][color=darkred]
          > > > into that type and check its name.
          > > >[/color]
          > >
          > >[/color]
          >
          >[/color]


          Comment

          • Scott M.

            #6
            Re: vb.net - event handler (follow-up)

            You could add a custom value to VIEWSTATE that differs depending on what
            control was interacted with:

            ViewState.Add(" EventStartedBy" , controlName)

            You could then check the "EventStartedBy " key in ViewState on the PostBack
            via a Select statement to see what its value is.

            I'm sure there's got to be another way, but this should work.


            "Bob" <bobjiang@yahoo .com> wrote in message
            news:OTA79n2sDH A.2004@TK2MSFTN GP10.phx.gbl...[color=blue]
            > That is also what I got in webform. SO back to the question. How can I[/color]
            know[color=blue]
            > which button invoked the button event?
            >
            > "Scott M." <s-mar@badspamsnet .net> wrote in message
            > news:#uKU9i2sDH A.2348@TK2MSFTN GP09.phx.gbl...[color=green]
            > > No, you are right Cor. I have used sender in Windows Forms projects[/color]
            > without[color=green]
            > > a problem and just assumed that it would work the same way in on a Web[/color]
            > Form.[color=green]
            > >
            > > Thanks.
            > >
            > > "Cor" <non@non.com> wrote in message
            > > news:%2307UqKzs DHA.2448@TK2MSF TNGP09.phx.gbl. ..[color=darkred]
            > > > Hi Scott,
            > > >
            > > > While I did not check all the other methods, because I think it[/color][/color][/color]
            becomes[color=blue]
            > a[color=green][color=darkred]
            > > > lot of rubish in the load event, while there is a good methode, that[/color][/color]
            > only[color=green][color=darkred]
            > > > needs three lines.
            > > > In VB.net as example that is
            > > > \\\
            > > > sub from page control event
            > > > todo
            > > > end sub
            > > > ///
            > > >
            > > > Did I test your sample because I could not believe it, but you never[/color]
            > > knows.[color=darkred]
            > > > (getting the type of sender is in webforms not that simple as in
            > > > windowforms)
            > > >
            > > > I did try it with only one serverside button on the page
            > > > \\\
            > > > Private Sub Page_Load(ByVal sender As System.Object, _
            > > > ByVal e As System.EventArg s) Handles MyBase.Load
            > > > Dim a As String = sender.GetType. ToString
            > > > 'returns always "asp.webform1.a spx"
            > > > End Sub
            > > > ///
            > > >
            > > > When I did understand something wrong let me know?
            > > >
            > > > Cor
            > > >
            > > > > Why not just check the "sender" event argument of the Page_Load[/color][/color][/color]
            event?[color=blue][color=green][color=darkred]
            > > > This
            > > > > is what it is there for. Just check sender.getType. toString and[/color][/color][/color]
            then[color=blue][color=green][color=darkred]
            > > > you'll
            > > > > know what kind of control caused the postback. Then you could cast[/color]
            > > sender[color=darkred]
            > > > > into that type and check its name.
            > > > >
            > > >
            > > >[/color]
            > >
            > >[/color]
            >
            >[/color]


            Comment

            • Cor

              #7
              Re: vb.net - event handler (follow-up)

              Hi Bob,

              Do you want to know what button did do the post back without to test them
              all, or do you want absolute to test them in the load event.

              For the first you can use dynamically made buttons and test it only once
              (and I have made an example if you want) for the second I have no solution
              and I think that will absolute become a big hash in the load event (I was
              also thinking like Scott on the viewstate for that).

              Cor


              Comment

              Working...