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.
<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;
}
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:
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.