Why is EventHandler not triggered in the following asp.net code?

IDE: Visual Studio 2010

.net version: 3.5

OS: Windows 7

Web Server: Visual Studio 2010 Development Server

Below is the asp.net C # code. This is the code behind an otherwise empty, out of the box, web form. I don't understand why the testClick event doesn't fire when I click on the button, but if I comment out the next line so that the button is made on the back, it fires.

if (!IsPostBack)

      

Obviously it has to do with the page lifecycle and how / when the controls are rendered, but I don't understand. When the page returns, isn't the btnTest button a new instance of btnTest? Why does the page care if it actually exists after the postback? An event handler exists. It seems like it should be important. I hope someone can break the order in which everything happens and explain this (clearly correct and intentional) behavior for me.

Thanks for reading.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page 
{
    Button btnTest = new Button();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            btnTest.Text = "TEST";
            this.Form.Controls.Add(btnTest);
            btnTest.Click += new EventHandler(testClick);
        }
    }

    protected void testClick(Object sender, EventArgs e)
    {
        Response.Write("Test button event has been handled");
    }
}

      

Ok, now I'm TOTALLY confused! In the following code, btnTest and btnTest2 are not the same button. When the page returns messages, it fires an event handler for btnTest2, as if btnTest2 was pressed but btnTest2 was not pressed. btnTest was. I don't understand this behavior. I have read the page Lifecycle article at https://msdn.microsoft.com/en-us/library/ms178472(v=vs.80).aspx as suggested, but I don't think it adequately explains this behavior.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Button btnTest = new Button();
            btnTest.Text = "TEST";
            this.Form.Controls.Add(btnTest);
            btnTest.Click += new EventHandler(testClick);
        }       

        if (IsPostBack)
        {
            Button btnTest2 = new Button();
            btnTest2.Text = "TEST";
            this.Form.Controls.Add(btnTest2);
            btnTest2.Click += new EventHandler(testClick_Postback);
        }        
    }

    protected void testClick(Object sender, EventArgs e)
    {
        Response.Write("Test button event has been handled by original handler");
    }

    protected void testClick_Postback(Object sender, EventArgs e)
    {
        Response.Write("Test button event was handled by new handler assigned on postback");
    }
}

      

+3


source to share


2 answers


Because this code only executes if the request is not inverse, so no control is created and no event handler is connected. Either put the control (and event handler) in the markup, or add / attach it to every request, not just open loop messages.



Controls and their events are not persisted across requests - they are either created from markup or in the code behind. When you do feedback, the control does not exist on the page and therefore there is no event to trigger.

+4


source


By dynamically attaching an event handler to Page_Load

, the event handler will only be subscribed if IsPostBack

it is false, that is, the first time the page is viewed before postbacks (for example, before Buttons

et al).

Unlike other management properties (e.g. .Text

, .Color

etc.) on the button event handlers can not be serialized through ViewState

and thus must be connected independently from PostBack or not.



For design time control, it is best to leave the event explorer in the actual control definition .ASPX

.

However, since you created a dynamic one Button

, you have no choice but to attach a handler every time.

+2


source







All Articles