Wednesday, March 14, 2012

.NET/ASP.NET interview Questions - how to do Render and PreRender?

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.

Steps to create window service in Visual Studio

(1) Open Visual Studio (2) Create a window service with any name (3) Now write your logic there (4) Build. If successfully build (5) Now on service file (service1.cs designer), right click and select "Add Installer" It will add "serviceProcessInstaller1" and "serviceInstaller1" (6) Add description/name/service name (7) Now select the properties of "serviceProcessInstaller1" and set Account=LocalSystem. If you will not select this then it will ask local system username and password every time. (8) Mention StartType as 'Automatic'. If select 'Manual' it needs to start manually. (9) Build this service. If its successfully build. the go on next step. (10) Go to Visual Studio command prompt. Write there C:\Program Files\Microsoft Visual Studio 8\VC>installutil -i "C:\Inetpub\wwwroot\ServiceName\bin\Debug\Service.exe" If you want to uninstall window service the just write the -u instead of -i. C:\Program Files\Microsoft Visual Studio 8\VC>installutil -u "C:\Inetpub\wwwroot\ServiceName\bin\Debug\Service.exe" If starttype is manual, then it needs to run manually. Every time it install. It need to start manually from servies. To Start it . Go to Run > Services.msc. Right click on service and click Start.

Could not find a part of the path ... bin\roslyn\csc.exe

I am trying to run an ASP.NET MVC (model-view-controller) project retrieved from TFS (Team Foundation Server) source control. I have added a...