Getting rendered HTML of a server control

This post shows a generic method that can return the HTML string generated by ASP.NET server control after the rendering.

string GetRenderedOutput<T>(T control) where T : Control
{
    StringBuilder builder = new StringBuilder();
    using (StringWriter sWriter = new StringWriter(builder))
    using (Html32TextWriter writer = new Html32TextWriter(sWriter))
    {
        control.RenderControl(writer);
    }
    return builder.ToString();
}

How small code optimizations can improve the performance and storage requirements

While reading an algorithms book, I learned that minor optimizations and choosing right algorithm can improve the code performance drastically. In this post we will see how this is happening.

Consider the following trivial code to calculate a Fibonacci value of a number using recursion.

int fibonacci(int num)
{
if (num < = 1) return num; else return fibonacci(num - 1) + fibonacci(num - 2); } [/sourcecode] Here is how this code executes for fibonacci(5). Continue reading

Disabling a custom control from Visual Studio’s toolbox

When a custom control is created, Visual Studio will show it in the toolbox for dragging and dropping. This can be annoying when you create a composite control which is a combination of several other custom controls where VS will display all custom controls in the toolbox. System.ComponentModel.ToolBoxItemAttribute can be used with the controls that you want to hide from toolbox. Apply this attribute to all the controls that you want to hide from toolbox.

Sample code

[System.ComponentModel.ToolboxItem(false)]
public class MyCustomControl : Label
{
}

MyCustomControl will not be displayed on the toolbox.