Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. Article Writing
  4. Add Event handlers Issue at Page Load

Add Event handlers Issue at Page Load

Scheduled Pinned Locked Moved Article Writing
helpquestion
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    shecool
    wrote on last edited by
    #1

    I have following code snippet at page load event. protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Button1.Attributes.Add("onclick", "alert('hello')"); Button2.Click += new EventHandler(Button2_Click); } } button1 display the alert message when we click on that. But when i click on button2, it doesn't fire Button2_Click event. Why? Althouh if i comment "if (!IsPostBack)" line, the code working fine.

    hello

    P P 2 Replies Last reply
    0
    • S shecool

      I have following code snippet at page load event. protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Button1.Attributes.Add("onclick", "alert('hello')"); Button2.Click += new EventHandler(Button2_Click); } } button1 display the alert message when we click on that. But when i click on button2, it doesn't fire Button2_Click event. Why? Althouh if i comment "if (!IsPostBack)" line, the code working fine.

      hello

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      Arrgggh. This is an ASP.NET question, yet this is PLAINLY NOT an ASP.NET forum? Are you planning on writing an article on this, or did you just randomly click on the links. Anyway, because I'm still in vaguely a good mood, I'll answer it. The simple fact is that a web form is stateless, so you need to reattach the event handlers EACH TIME you post back.

      Deja View - the feeling that you've seen this post before.

      My blog | My articles

      S 1 Reply Last reply
      0
      • P Pete OHanlon

        Arrgggh. This is an ASP.NET question, yet this is PLAINLY NOT an ASP.NET forum? Are you planning on writing an article on this, or did you just randomly click on the links. Anyway, because I'm still in vaguely a good mood, I'll answer it. The simple fact is that a web form is stateless, so you need to reattach the event handlers EACH TIME you post back.

        Deja View - the feeling that you've seen this post before.

        My blog | My articles

        S Offline
        S Offline
        shecool
        wrote on last edited by
        #3

        then why Button1 click event fire without attaching again?

        hello

        P V 2 Replies Last reply
        0
        • S shecool

          then why Button1 click event fire without attaching again?

          hello

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          shecool wrote:

          then why Button1 click event fire without attaching again?

          That's not a server side event handler.

          Deja View - the feeling that you've seen this post before.

          My blog | My articles

          1 Reply Last reply
          0
          • S shecool

            I have following code snippet at page load event. protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Button1.Attributes.Add("onclick", "alert('hello')"); Button2.Click += new EventHandler(Button2_Click); } } button1 display the alert message when we click on that. But when i click on button2, it doesn't fire Button2_Click event. Why? Althouh if i comment "if (!IsPostBack)" line, the code working fine.

            hello

            P Offline
            P Offline
            Paul Conrad
            wrote on last edited by
            #5

            Wrong forum. For potentially better success in the future, pick the correct forum :|

            "The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

            1 Reply Last reply
            0
            • S shecool

              then why Button1 click event fire without attaching again?

              hello

              V Offline
              V Offline
              Vasudevan Deepak Kumar
              wrote on last edited by
              #6

              shecool wrote:

              then why Button1 click event fire without attaching again?

              Because you are not capable of (including to make a good choice of correct technical forum) programming.

              Vasudevan Deepak Kumar Personal Homepage
              Tech Gossips
              All the world's a stage, And all the men and women merely players. They have their exits and their entrances; And one man in his time plays many parts... --William Shakespeare

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups