asp.netselectedindexchanged

dropdown list webform dynamic register event in Pageload won't fire


I need a dynamic register event handler.
I tried to do some snippet code for ASP.NET webform and realized 'something' that makes a dropdown list won't fire event SelectedIndexChanged.

First snippet(works well). I register the event handler in ASPX file.

<form id="form1" runat="server">
    <asp:Label ID="lblName" runat="server" Text="Label"></asp:Label>
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    <asp:DropDownList ID="Ddl1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="Ddl1_SelectedIndexChanged">
    </asp:DropDownList>

    <asp:DropDownList ID="Ddl2" runat="server"  AutoPostBack="true" OnSelectedIndexChanged="Ddl2_SelectedIndexChanged">
    </asp:DropDownList>
    <asp:Button ID="BtnSubmit" runat="server" Text="Submit" OnClick="BtnSubmit_Click" />
</form>

Then in CS file, I create 2 function like this

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {

            Ddl2.Items.Add(new ListItem("1", "1"));
            Ddl2.Items.Add(new ListItem("2", "2"));
            Ddl2.Items.Add(new ListItem("3", "3"));
            Ddl2.Items.Add(new ListItem("4", "4"));

            Ddl1.Items.Add(new ListItem("1", "1"));
            Ddl1.Items.Add(new ListItem("2", "2"));
            Ddl1.Items.Add(new ListItem("3", "3"));
            Ddl1.Items.Add(new ListItem("4", "4"));


            BtnSubmit.Click += new EventHandler(BtnSubmit_Click);

            Ddl1.SelectedIndex = 0;
            Ddl2.SelectedIndex = 0;
        }
    }
    public void Ddl2_SelectedIndexChanged(object sender, EventArgs e)
    {
        var st = new StackTrace();
        var sf = st.GetFrame(0);

        var currentMethodName = sf.GetMethod();
        Console.WriteLine(currentMethodName);
        Label1.Text = Ddl2.SelectedValue;
    }

    public void Ddl1_SelectedIndexChanged(object sender, EventArgs e)
    {
        var st = new StackTrace();
        var sf = st.GetFrame(0);

        var currentMethodName = sf.GetMethod();
        Console.WriteLine(currentMethodName);
        Label1.Text = Ddl1.SelectedValue;
    }

Second, I remove Autopostback and OnSelectedIndexChanged of dropwdown 1 on view and move the register handler to PageLoad code. Now It won't fire anymore

The dropdown 1 will raise refresh page. But the event handler won't fire anymore. I am using VS2017 Pro & .NET 4.7.2

<form id="form1" runat="server">
    <asp:Label ID="lblName" runat="server" Text="Label"></asp:Label>
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    <asp:DropDownList ID="Ddl1" runat="server">
    </asp:DropDownList>

    <asp:DropDownList ID="Ddl2" runat="server"  AutoPostBack="true" OnSelectedIndexChanged="Ddl2_SelectedIndexChanged">
    </asp:DropDownList>
    <asp:Button ID="BtnSubmit" runat="server" Text="Submit" OnClick="BtnSubmit_Click" />
</form>

And in CS code:

        protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {

            Ddl2.Items.Add(new ListItem("1", "1"));
            Ddl2.Items.Add(new ListItem("2", "2"));
            Ddl2.Items.Add(new ListItem("3", "3"));
            Ddl2.Items.Add(new ListItem("4", "4"));


            Ddl1.Items.Add(new ListItem("1", "1"));
            Ddl1.Items.Add(new ListItem("2", "2"));
            Ddl1.Items.Add(new ListItem("3", "3"));
            Ddl1.Items.Add(new ListItem("4", "4"));


            BtnSubmit.Click += new EventHandler(BtnSubmit_Click);

            Ddl1.SelectedIndex = 0;
            Ddl2.SelectedIndex = 0;

            Ddl1.AutoPostBack = true;
            Ddl1.SelectedIndexChanged += new System.EventHandler(Ddl1_SelectedIndexChanged);

        }
    }
    public void Ddl2_SelectedIndexChanged(object sender, EventArgs e)
    {
        var st = new StackTrace();
        var sf = st.GetFrame(0);

        var currentMethodName = sf.GetMethod();
        Console.WriteLine(currentMethodName);
        Label1.Text = Ddl2.SelectedValue;
    }

    public void Ddl1_SelectedIndexChanged(object sender, EventArgs e)
    {
        var st = new StackTrace();
        var sf = st.GetFrame(0);

        var currentMethodName = sf.GetMethod();
        Console.WriteLine(currentMethodName);
        Label1.Text = Ddl1.SelectedValue;
    }

Can anyone help me:

  1. What is the difference between registering the event handler on pageload or APSX file?
  2. What page lifecycle event can I register to make event handler fire?
  3. Is this a bug of Microsoft? If it is, where do I post this bug?

Solution

  • Long story short, every request re-creates the page and controls.

    So, "Posting Back" would load the page again but with IsPostBack set to true, and your event registration is never reached.