Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

ASP.NET Server control events - Part 7

Suggested Videos
Part 4 - Events in the life cycle of a web application 
Part 5 - Difference between ViewState, SessionState and ApplicationState

Part 6 - ASP.NET Page Life Cycle Events

In the previous parts of this video series, we have discussed that events can occur at application, page and control levels. In Part 4, we discussed application level events, and in Part 6, we discussed about Page Level events.

In this session, we will discuss about control level events. ASP.NET server controls, such as TextBox, Button, and DropDownList has their own events. For example, Button has a click event. TextBox has TextChanged event, and DropDownList has SelectedIndexChanged event. We have a set of asp.net validation controls, that has validation events. The events that all these controls expose, can be broadly divided into 3 categories.

Postback events - These events submit the Web page, immediately to the server for processing. Click event of a button control is an example for PostBack event.

Cached events - These events are saved in the page’s view state to be processed when a postback event occurs. TextChanged event of TextBox control, and SelectedIndexChanged event of a DropDownList control are examples of cached events. Cached events can be converted into postback events, by setting the AutoPostBack property of the control to true.

Validation events - These events occur on the client, before the page is posted back to the server. All validation controls use these type of events.



In Part 6, of this video series, we have understood that control events are processed after the PageLoad event. The picture below depicts the same. Among the control events, Cached events happen before PostBack events.
ASP.NET page level and control level events execution order



To understand the order in which Page and Server control events execute, add a Web form with a TextBox, RequiredFieldValidator, and a Button control. You can find RequiredFieldValidator under Validation tab, in the ToolBox. Double click the TextBox control on the WebForm, and the event handler TextBox1_TextChanged() will be automatically generated. Along the same lines, double click the Button control, Button1_Click() event handler will be generated. Right Click the RequiredFieldValidator control on the webform and select Properties. From the Properties window, Set ControlToValidate property to TextBox1. At this stage copy and paste the following code in WebForm1.aspx.cs.
protected void Page_PreInit(object sender, EventArgs e)

    Response.Write("Page_PreInit" + "<br/>"); 
}
protected void Page_Init(object sender, EventArgs e)

    Response.Write("Page_Init" + "<br/>"); 
}
protected void Page_InitComplete(object sender, EventArgs e)

    Response.Write("Page_InitComplete" + "<br/>"); 
}
protected void Page_PreLoad(object sender, EventArgs e)

    Response.Write("Page_PreLoad" + "<br/>"); 
}
protected void Page_LoadComplete(object sender, EventArgs e)

    Response.Write("Page_LoadComplete" + "<br/>"); 
}
protected void Page_PreRender(object sender, EventArgs e)

    Response.Write("Page_PreRender" + "<br/>"); 
}
protected void Page_PreRenderComplete(object sender, EventArgs e)

    Response.Write("Page_PreRenderComplete" + "<br/>"); 
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
    Response.Write("Text Changed Event""<br/>");
}
protected void Button1_Click(object sender, EventArgs e)
{
    Response.Write("Button Click""<br/>");
}

Now, run the project, and when the webform renders, the page level events occur in the following order. Notice that, TextChanged and ButtonClick events are not fired.
Page_PreInit
Page_Init
Page_InitComplete
Page_PreLoad
Page_LoadComplete
Page_PreRender
Page_PreRenderComplete

Don't type anything in the TextBox, and click the button control. The RequiredFieldValidator message is displayed. No other events get processed and the page is not posted back to the server. 

Now, enter some text, into the TextBox and Click the button. Notice that, Text Changed Event and Button Click, execute after Page Load and Before Page Load Complete events. Among the control events, TextChanged event is fired before the click event. The execution order is shown below.
Page_PreInit
Page_Init
Page_InitComplete
Page_PreLoad
Text Changed Event
Button Click
Page_LoadComplete
Page_PreRender
Page_PreRenderComplete

2 comments:

  1. Hi Venkat,



    This is Chandu, I have one dought in view state.



    i have created one application .In that i have placed one text box and one button contorl .after that i have set the text

    box property of "Enable view state =false" , and written the code in code behind file like this.



    public partial class Default2 : System.Web.UI.Page
    {


    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
    int incriment =Convert.ToInt32 (TextBox2.Text);
    incriment = incriment + 1;
    TextBox2.Text = incriment.ToString();
    }
    }



    Actually every web server control uses the view state by default.because of that i have set the "Enable view state =false" for text box even though i set this as false .when i am clicking on incriment button the value of text box is incresing.



    then how to disable the view state for text box or other server controls.



    could u plese look into this and clarify my dought.

    ReplyDelete
    Replies
    1. Hi, Chandrasekhar, the article below, explains the reasons for TextBox maintaining its state even after viewstate is disabled.
      http://www.codeproject.com/Articles/31701/ViewState-and-Postback

      Delete

It would be great if you can help share these free resources