Showing posts with label prender. Show all posts
Showing posts with label prender. Show all posts

Sunday, May 15, 2011

.NET/ASP.NET interview Questions - Give a brief on 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.




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

Friday, February 25, 2011

ASP.NET interview question:-What is the difference between render and prender event ?

ASP.NET interview question:-What is the difference between render and prender event ?


Answer:
This is again a very nice .NET interview question and the question can be confusing because of the common word "render". Now ASP.NET page has 4 important events. Init , Load , validate ,
event and render (Remember SILVER).

Now the final render is a actually a two step process.
1- First the ASP.NET UI objects are saved in to view state.
2- loaded viewstate is assembled to create the final HTML.
The first step is pre-render event and the second step is the render event.

Prerender
This is the event in which the objects will be saved to viewstate.This makes the PreRender event the right place for changing properties of controls or changing Control structure.Once the PreRender phase is done those changes to objects are locked in and the viewstate can not be changed. The PreRender step can be overridden using OnPreRender event.

Render.
Render event assembles the HTML so that it can be sent to the browser.In Render event developers can write custom HTML and override any HTML which is created till now.The Render method takes an HtmlTextWriter object as a parameter and uses that to output HTML to be streamed to the browser. Changes can still be made at this point, but they are reflected to the client only i.e. the end browser.

View my top .NET Interview question and answers