Shivprasad Koirala
.NET/ASP.NET interview Questions - Demonstrate Render and PreRender?
By Shivprasad Koirala in ASP.NET on May 15 2011
  • Shivprasad Koirala
    May, 2011 15

    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.

    1. protected override void OnPreRender(EventArgs e)
    2. {
    3. foreach (GridViewRow row in GridView1.Rows)
    4. {
    5. row.ForeColor = Color.Blue;
    6. }
    7. }

    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.

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

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

    1. using System.Drawing;
    2. using System.Collections;
    3. namespace Data
    4. {
    5. public class Customer
    6. {
    7. public string CustomerCode { set; get; }
    8. public string CustomerName { set; get; }
    9. }
    10. public partial class WebForm1 : System.Web.UI.Page
    11. {
    12. protected void Page_Load(object sender, EventArgs e)
    13. {
    14. LoadGrid();
    15. }
    16. public void LoadGrid()
    17. {
    18. ArrayList objArray = new ArrayList();
    19. Customer ObjCustomer = new Customer();
    20. ObjCustomer.CustomerCode = "1001";
    21. ObjCustomer.CustomerName = "shiv";
    22. objArray.Add(ObjCustomer);
    23. ObjCustomer = new Customer();
    24. ObjCustomer.CustomerCode = "1002";
    25. ObjCustomer.CustomerName = "Feroz";
    26. objArray.Add(ObjCustomer);
    27. GridView1.DataSource = objArray;
    28. GridView1.DataBind();
    29. }
    30. protected override void OnPreRender(EventArgs e)
    31. {
    32. foreach (GridViewRow row in GridView1.Rows)
    33. {
    34. row.ForeColor = Color.Blue;
    35. }
    36. }
    37. protected override void Render(HtmlTextWriter writer)
    38. {
    39. writer.Write("This displays the grid in blue color");
    40. base.Render(writer);
    41. }
    42. }
    43. }

    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.

    • 0


Most Popular Job Functions


MOST LIKED QUESTIONS