Aug 28, 2014

C# - What is Predicate?

Once I was looking for this for hours..
http://stackoverflow.com/questions/1710301/what-is-a-predicate-in-c

Aug 13, 2014

ASP.NET why Page_PreRender ?

So what's the purpose of Page_PreRender in ASP.NET webforms?

Easiest answer for me is through simple example.


  1. Create classic ASP.NET webforms with single ASPX
  2. Create 2 ASCX web user controls
Place in each code in Page_Load:

WebUserControl1.ascx


    public partial class WebUserControl1 : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Page.Title = "WebUserControl1";
        }

WebUserControl2.ascx

    public partial class WebUserControl1 : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Page.Title = "WebUserControl1";
        }


Register both on ASPX like this:

<%@ Page Title="About" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="About.aspx.cs" Inherits="WebApplication6.About" %>
<%@ Register Src="~/WebUserControl2.ascx" TagPrefix="app" TagName="web2" %>
<%@ Register Src="~/WebUserControl1.ascx" TagPrefix="app" TagName="web1"%>
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">    
    <app:web1 id="ctrl1" runat="server" />  
    <app:web2 id="ctrl2" runat="server" />
</asp:Content>

Run web and observe that in browser  Tab Title is reflecting change from WebUserControl2.ascx Page_Load.
Now add Page_PreRender event in first one WebUserControl1.ascx:

   protected void Page_Load(object sender, EventArgs e)
        {
            Page.Title = "WebUserControl1";
        }
        protected void Page_PreRender(object sender, EventArgs e)
        {
            Page.Title = "PreRender_WebUserControl1";
        }
    }

Run  and observe that now text from WebUserControl1's Page_PreRender event is shown instead of previous from WebUserControl2 Page_Load.

Same idea if you have filled Page_PreRender of ASPX.

Bottom line is to use Page_PreRender on only one place in your page elements (ASPX,ASCX) when you need to make sure that code in PreRender is the last modification to common page elements that are shared among all controls.