WebPart Life cycle is easy to understand like ASP.Net.
OnInit: This method handles initialization of the control.
OnLoad: This event handles the Load event. This is also used for initialize the control but is not intended for loading data or other processing functionality.
CreateChildControls: This is the most popular event in web part life cycle. This creates any child controls. So if you are adding any control to display then you have to write in this method
EnsureChildControls: This method ensures that CreateChildControls has executed.
EnsureChildControls method must be called to prevent null reference exceptions.
SaveViewState: View state of the web part saved.
OnPreRender: This method handles or initiates tasks such as data loading that must complete before the control can render.
Page.PreRenderComplete: The page fires the PreRenderComplete event after all controls have completed their OnPreRender methods.
Render: This method is used to render everything.
RenderContents: Renders the contents of the control only, inside of the outer tags and style properties.
OnUnload: Performs the final cleanup.
/***********************************************************
* Log Details* Author : Madan * Creation Date : 25-07-2008 (MOSS 2007)* FileName : WebPartLifeCycle.cs * Class : WebPartLifeCycle * Description : Tracking the web part life cycle. ***********************************************************************/using System;
[
ToolboxItemAttribute(false)]
public class WPLifeCycle1 : WebPart{//variblesprivate string strResults;
private TextBox txtContent;
private Button btnShow;
private Label lblErrMsg;
/// <summary>/// 1 st EVENT IN WEB PART /// During the initialization, configuration values that were marked as webbrowsable/// and set through the webpart task pane are loaded to the webpart/// </summary>/// <param name="e"></param>protected override void OnInit(EventArgs e){
try{this.strResults += "onInit Method <br>";
base.OnInit(e);}
catch (Exception ex){
this.lblErrMsg.Text = ex.Message.ToString();}
}
/// <summary> /// 2 nd EVENT IN WEB PART /// /// Viewstate is a property inherited from System.Web.UI.Control . /// The viewstate is filled from the state information that was previously serialized /// would like to persist your own data within webpart /// </summary> /// <param name="savedState"></param>protected override void LoadViewState(object savedState){
try{
strResults += "LoadViewState<br>";
object[] viewstate = null;
if (savedState != null){
viewstate = (
object[])savedState;
base.LoadViewState(viewstate[0]);strResults += (
string)viewstate[1] + "<br>";}
}
catch (Exception ex){
this.lblErrMsg.Text = ex.Message.ToString();}
}
/// <summary> /// 3 rd EVENT IN WEB PART MANAGER /// All of the constituent controls are created and added to the controls collection /// </summary>protected override void CreateChildControls(){
try{
strResults += "CreateChildControls<br>"; //creating error label controls this.lblErrMsg = new Label();
this.lblErrMsg.ID = "lblErrMsg";
this.lblErrMsg.Text = string.Empty;
this.Controls.Add(lblErrMsg); //creating text controls this.txtContent = new TextBox();
this.txtContent.ID = "tbxText";
this.txtContent.Text = string.Empty;
this.Controls.Add(txtContent); //creating button controls this.btnShow = new Button();
this.btnShow.ID = "btnShow";
this.btnShow.Text = "Check Text Value";
this.btnShow.Click += new EventHandler(btnShow_Click);
this.Controls.Add(btnShow);}
catch (Exception ex){
this.lblErrMsg.Text = ex.Message.ToString();}
}
/// <summary> /// butoon click event /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnShow_Click(object sender, EventArgs e){
try{this.strResults += "button click event has fired<br>";}
catch (Exception ex){
this.lblErrMsg.Text = ex.Message.ToString();}
}
/// <summary> /// 4 th EVENT IN WEB PART /// Perform actions common to all requests, such as setting up a database query /// At this point, server controls in the tree are created and initialized, the state is restored, /// and form controls reflect client-side data. /// </summary> /// <param name="e"></param> protected override void OnLoad(EventArgs e) {
try {
this.strResults += "Onload<br>";
base.OnLoad(e); }
catch (Exception ex) {
this.lblErrMsg.Text = ex.Message.ToString(); }
}
/// <summary> /// 5 th EVENT IN WEB PART/// Perform any updates before the output is rendered. /// Any changes made to the state of the control in the pre-render phase can be saved/// while changes made in the rendering phase are lost. /// </summary> /// <param name="e"></param> protected override void OnPreRender(EventArgs e) {
try {
this.strResults += "OnPreRender<br>";
base.OnPreRender(e); }
catch (Exception ex) {
this.lblErrMsg.Text = ex.Message.ToString(); }
}
/// <summary> /// 6 th EVENT IN WEB PART /// store cutom data within a web part's viewstate /// The ViewState property of a control is automatically persisted to a string object after this stage. /// This string object is sent to the client and back as a hidden variable. /// For improving efficiency, a control can override /// the SaveViewState method to modify the ViewState property. /// once viewstate is saved,the control webpart can be removed from the memory of the server. /// webpart receives notification that they are about removed from memory through dispose event /// </summary> /// <returns></returns> protected override object SaveViewState() {
this.strResults += "SaveViewState<br>";
object[] viewstate = new object[2]; viewstate[0] = base.SaveViewState(); viewstate[1] =
"MyTestData";
return viewstate; }
/// 7 th EVENT IN WEB PART/// Generate output to be rendered to the client. /// you can create user interface of your webpart using html table. /// You can apply your css classes here itself /// </summary> /// <param name="writer"></param> public override void RenderControl(HtmlTextWriter writer) {
try {
//this method ensure all created child controls EnsureChildControls(); this.strResults += "RenderControl<br>";
//table start writer.Write("<table id='tblTest'align='center' cellpadding='0' cellspacing='0' border='1' width='100%'>"); writer.Write(
"<tr>"); writer.Write(
"<td>"); writer.Write(strResults);
writer.Write(
"</td>"); writer.Write(
"</tr>"); //row 2 writer.Write("<tr>"); writer.Write(
"<td>");
this.txtContent.RenderControl(writer); writer.Write(
"</td>"); writer.Write(
"</tr>"); //row 3 writer.Write("<tr>"); writer.Write(
"<td>");
this.btnShow.RenderControl(writer); writer.Write(
"</td>"); writer.Write(
"</tr>"); //row 4 writer.Write("<tr>"); writer.Write(
"<td>");
this.lblErrMsg.RenderControl(writer); writer.Write(
"</td>"); writer.Write(
"</tr>"); writer.Write(
"</table>");
//table end writer.Write(txtContent.Text);
} catch (Exception ex) {
this.lblErrMsg.Text = ex.Message.ToString(); }
}
/// <summary> /// 8 th EVENT IN WEB PART /// Perform any final cleanup before the control is torn down. /// References to expensive resources such as database connections must be /// released in this phase. /// </summary> public override void Dispose(){
base.Dispose();}
/// <summary> /// 9 th EVENT IN WEB PART MANAGER/// Perform any final cleanup before the control is torn down. /// Control authors generally perform cleanup in Dispose and do not handle this event /// The webpart removed from memory of the server /// Generally webpart developer do not need access to this event because all /// the clean up should have been accomplish in dispose event /// </summary> /// <param name="e"></param> protected override void OnUnload(EventArgs e) {
base.OnUnload(e);
}
}
}
Build and Publish
No comments:
Post a Comment