Page_Init Event Occurs Twice but the message is printed only once ...Y?

Posted by Nkkppp under ASP.NET on 6/16/2012 | Points: 10 | Views : 1753 | Status : [Member] | Replies : 1
1) When we run the below code message within Page_Init event is raised once.
2) During the button click event...again the Page_Init event is raised but the message is not printed...Y??


namespace eventdemo
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
lblmessage.Text += "Page load event handled. <br />";
if (Page.IsPostBack)
{
lblmessage.Text += "Page post back event handled.<br/>";
}
}
protected void Page_Init(object sender, EventArgs e)
{
lblmessage.Text += "Page initialization event handled.<br/>";
}
protected void Page_PreRender(object sender, EventArgs e)
{
lblmessage.Text += "Page prerender event handled. <br/>";
}
protected void btnclick_Click(object sender, EventArgs e)
{
lblmessage.Text += "Button click event handled. <br/>";
}
}
}




Responses

Posted by: Nkkppp on: 6/21/2012 [Member] Starter | Points: 25

Up
0
Down
Init event is called twice..but the message is overwritten.

So,I have made a few changes to the above as below....and confirmed that Init event is called twice and message is printed

protected void Page_Load(object sender, EventArgs e)
{
label2.Text = "pageload";
if (IsPostBack)
{
label5.Text = "postbackpageload";
}
}
protected void Page_Init(object sender, EventArgs e)
{
label1.Text = "Initialize";
if (IsPostBack)
{
label6.Text = "postbackInitialize";
}
}
protected void Page_PreRender(object sender, EventArgs e)
{
label3.Text = "render";
if (IsPostBack)
{
label7.Text = "postbackPrerender";
}

}

protected void btn_Click(object sender, EventArgs e)
{
label4.Text = "buttonclick";

}


Nkkppp, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response