.NET/ASP.NET interview Questions – Define Render and PreRender?

Answer:PreRender: –PreRender is an event which is used for modifying server
controls just  before sending them to the client.Render: –Render is an method which actually puts the HTML output to
the response stream.

Lets see an simple example to get an better idea, below is the code snippet
for the same.

To use PreRender event, we have to override it and can make necessary
changes to the controls we want to.

protected override void OnPreRender(EventArgs e)
{
 foreach (GridViewRow row in GridView1.Rows)
  {
      row.ForeColor = Color.Blue;
  }         
}

In the above code snippet, I have override the PreRender event and just changed the forecolor of the GridView.

Now lets see how we can code for Render. Below is the code snippet for the same, in which I have override the Render and using “HtmlTextWriter” just displayed a simple HTML text output.

protected override void Render(HtmlTextWriter writer)
{
   writer.Write("This displays the grid in blue color");
   base.Render(writer);
}

Below is the full code snippet so that you can try it by yourself and see the respective result.

using System.Drawing;
using System.Collections;

namespace Data
{
    public class Customer
    {
        public string CustomerCode { set; get; }
        public string CustomerName { set; get; }
    }
    public partial class WebForm1 : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            LoadGrid();
        }
        public void LoadGrid()
        {
            ArrayList objArray = new ArrayList();
            Customer ObjCustomer = new Customer();
            ObjCustomer.CustomerCode = "1001";
            ObjCustomer.CustomerName = "shiv";
            objArray.Add(ObjCustomer);
            ObjCustomer = new Customer();
            ObjCustomer.CustomerCode = "1002";
            ObjCustomer.CustomerName = "Feroz";
            objArray.Add(ObjCustomer);
            GridView1.DataSource = objArray;
            GridView1.DataBind();
        }
        protected override void OnPreRender(EventArgs e)
        {
            foreach (GridViewRow row in GridView1.Rows)
            {
                row.ForeColor = Color.Blue;
            }

        }
        protected override void Render(HtmlTextWriter writer)
        {
            writer.Write("This displays the grid in blue color");
            base.Render(writer);
        }
    }
}

In the above code snippet, I have used prerender event to change the forecolor of the girdview before the gridview is displayed on the client browser and I have override the render method to display the html output to the response stream.

PreRender event executed before the render method gets called which creates the HTML code using the HtmlWriter.so in other words prerender event fires first and do the necessary changes to the server control and later the render method is called.

In below diagram you can see that the gridview forecolor is been changed because of the prerender event and the render method display the html output using html writer.

render.png

In the following video, view samples of various questions
asked in C# and .NET interview.

Please click here to see more .NET/ASP.NET interview questions

Regards,
Visit Authors blog for more .NET and ASP.NET  interview questions
Advertisements

About c# and .NET Interview questions

This blog is for developers who want to crack .NET and C# interviews. It has all tips and tricks needed to crack .NET interviews , C# interview , SQL Server interview , Java interview , WCF Interview , Silverlight interview , WPF interview , LINQ interview , Entity framework Interview. Do not forget to watch our Learn step by step video series. Learn MVC in 16 hours:- https://www.youtube.com/watch?v=Lp7nSImO5vk Learn AngularJS Step by Step:- https://www.youtube.com/watch?v=0kmdjqgO9IY Learn Design Pattern in 8 hours:- https://www.youtube.com/watch?v=YDobmucohqk Learn C# and .NET in 60 days:- https://www.youtube.com/watch?v=yh2SrzCkNQA Learn MSBI in 32 hours:- https://www.youtube.com/watch?v=mGPJx3ocFgg Learn SharePoint Step by Step in 8 hours:- https://youtu.be/C2fW76SwJNU
This entry was posted in Uncategorized and tagged , , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s