{love to code?}

September 28, 2009

Getting rendered HTML of a server control

Filed under: ASP.NET — navaneethkn @ 10:31 PM
Tags:

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();
}

September 17, 2009

How small code optimizations can improve the performance and storage requirements

Filed under: Algorithms — navaneethkn @ 9:16 AM
Tags: ,

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);
}

Here is how this code executes for fibonacci(5). (more...)

September 15, 2009

Disabling a custom control from Visual Studio’s toolbox

Filed under: .NET — navaneethkn @ 7:59 AM
Tags: ,

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.

Blog at WordPress.com.