I have google a lot for working example of page events in C# but i was not able to find code in c# but found in vb. then i decided to write my own example in c# , so that it may help all my .net friends.
Here are two pages code one is .aspx and other one is .aspx.cs :
Default.aspx :
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
<asp:PlaceHolder runat="server" ID="LabelPlaceHolder" >
<%--<asp:Label ID="lblMessage" runat="server" Text="Label"></asp:Label>--%>
</asp:PlaceHolder>
<br />
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
</asp:Content>
and the code behind file for this page is :
CODE Behind File :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
public partial class Default3 : System.Web.UI.Page
{
void Page_Error(object sender, EventArgs e)
{
//This is for getting error from page and handing them accordingly
}
void Page_PreInit(object sender, EventArgs e)
{
if (IsPostBack)
{
Label lblMessage = new Label();
lblMessage.ID = "lblMessage";
lblMessage.Width = 500;
lblMessage.Text = "Dynamically created control.";
ContentPlaceHolder ContentPlaceHolder1 = (ContentPlaceHolder)Master.FindControl("MainContent");
PlaceHolder LabelPlaceHolder = (PlaceHolder)ContentPlaceHolder1.FindControl("LabelPlaceHolder");
LabelPlaceHolder.Controls.Add(lblMessage);
}
}
void Page_Init(object sender, EventArgs e)
{
ContentPlaceHolder ContentPlaceHolder1 = (ContentPlaceHolder)Master.FindControl("MainContent");
PlaceHolder LabelPlaceHolder = (PlaceHolder)ContentPlaceHolder1.FindControl("LabelPlaceHolder");
Label lblMessage = (Label)LabelPlaceHolder.FindControl("lblMessage");
lblMessage.ForeColor = System.Drawing.Color.Black;
Trace.Write("Page_Init " + DateTime.Now.ToString());
}
void Page_InitComplete(object sender, EventArgs e)
{
ContentPlaceHolder ContentPlaceHolder1 = (ContentPlaceHolder)Master.FindControl("MainContent");
PlaceHolder LabelPlaceHolder = (PlaceHolder)ContentPlaceHolder1.FindControl("LabelPlaceHolder");
Label lblMessage = (Label)LabelPlaceHolder.FindControl("lblMessage");
lblMessage.ToolTip = "Tooltip added during InitComplete event.";
ViewState["Author"] = "Robert S. Robbins";
System.Diagnostics.Trace.WriteLine("Page_InitComplete " + DateTime.Now.ToString());
}
void Page_PreLoad(object sender, EventArgs e)
{
System.Diagnostics.Trace.WriteLine(ViewState.Values.Count, "ViewState Count");
System.Diagnostics.Trace.WriteLine(Request.ServerVariables["SCRIPT_NAME"], "Script Name");
System.Diagnostics.Trace.WriteLine("Page_PreLoad " + DateTime.Now.ToString());
}
void Page_Load(object sender, EventArgs e)
{
SqlConnection cn;
SqlCommand objSqlCommand = new SqlCommand();
cn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["Northwind"].ToString();
cn.Open();
objSqlCommand.Connection = cn;
objSqlCommand.CommandText = "SELECT ProductID, ProductName, UnitPrice FROM Products ORDER BY ProductName";
DataSet objDataSet = new DataSet();
SqlDataAdapter objSqlDataAdapter = new SqlDataAdapter();
objSqlDataAdapter.SelectCommand = objSqlCommand;
objSqlDataAdapter.Fill(objDataSet);
GridView1.DataSource = objDataSet;
GridView1.DataBind();
cn.Close();
System.Diagnostics.Trace.WriteLine("Page_Load " + DateTime.Now.ToString());
}
void Page_LoadComplete(object sender, EventArgs e)
{
System.Diagnostics.Trace.WriteLine(GridView1.Rows[5].Cells[01].Text, "Row 6, Cell 2");
System.Diagnostics.Trace.WriteLine("Page_LoadComplete " + DateTime.Now.ToString());
}
void Page_PreRender(object sender, EventArgs e)
{
ContentPlaceHolder ContentPlaceHolder1 = (ContentPlaceHolder)Master.FindControl("MainContent");
PlaceHolder LabelPlaceHolder = (PlaceHolder)ContentPlaceHolder1.FindControl("LabelPlaceHolder");
GridView GridView1= (GridView)ContentPlaceHolder1.FindControl("GridView1");
GridViewRow objGridViewRow;
objGridViewRow = new GridViewRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal);
TableCell objTableCell= new TableCell();
LiteralControl literal= new LiteralControl();
literal.Text = "Northwind Products";
objTableCell.Controls.Add(literal);
objTableCell.ColumnSpan = 3;
objTableCell.HorizontalAlign = HorizontalAlign.Center;
objTableCell.ForeColor = System.Drawing.Color.White;
objTableCell.BackColor = System.Drawing.Color.Black;
objGridViewRow.Cells.Add(objTableCell);
GridView1.Controls[0].Controls.AddAt(0, objGridViewRow);
System.Diagnostics.Trace.WriteLine("Page_PreRender " +DateTime.Now();
}
void Page_PreRenderComplete(object sender, EventArgs e)
{
ContentPlaceHolder ContentPlaceHolder1 = (ContentPlaceHolder)Master.FindControl("MainContent");
PlaceHolder LabelPlaceHolder = (PlaceHolder)ContentPlaceHolder1.FindControl("LabelPlaceHolder");
GridView GridView1 = (GridView)ContentPlaceHolder1.FindControl("GridView1");
GridView1.HeaderRow.Cells[0].Text = "Product ID";
GridView1.HeaderRow.Cells[1].Text = "Product Name";
GridView1.HeaderRow.Cells[2].Text = "Unit Price";
}
void Page_SaveStateComplete(object sender, EventArgs e)
{
ViewState["Date"] = DateTime.Now.ToString();
System.Diagnostics.Trace.WriteLine("Page_SaveStateComplete " + DateTime.Now.ToString());
}
void Page_Unload(object sender, EventArgs e)
{
System.Diagnostics.Trace.WriteLine(ViewState.Values.Count, "ViewState Count");
}
}
-----------------------------------------------------------------------------------------
I have used this code in vs2010.
Try using breakpoin with every event . and then debug .
By using Debugger . you can check which event fires first and what can we do with each event..
HOPE THIS HELPS :)
Thanks