Click here to hide categories Click here to show left categories

User: Home          welcome : Guest          Log In / Register here     




.NET/ASP.NET interview Questions - Show how to do 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

Share this article   |    Print    |    Article read by 4869 times
Author:
Shivprasad koirala Koirala
I am a Microsoft MVP for ASP/ASP.NET and currently a CEO of a small E-learning company in India. We are very much active in making training videos , writing books and corporate trainings. Do visit my site http://www.questpond.com for .NET, C# , design pattern , WCF , Silverlight , LINQ , ASP.NET , ADO.NET , Sharepoint , UML , SQL Server training and Interview questions and answers
Related Articles: No related article
Related Interview Questions: No related interview question