Custom server controls do great job. A complex page can easily be split into many controls. In this way it becomes easier to support after that. Used in a page, a server control follows the whole page life cycle.

Sometimes you will need just to get the HTML output from a server control for a different usage like sending it by an e-mail. So what can you do?  There is a method called RenderControl, which renders a control using a HtmlTextWriter object. We can simply load the control using the LoadControl method and then use RenderControl.

Control myControl = LoadControl("~/MyControl.ascx");
StringBuilder sb = new StringBuilder();
using (StringWriter sw = new StringWriter(sb))
{
	using (HtmlTextWriter tw = new HtmlTextWriter(sw))
	{
		myControl.RenderControl(tw);
	}
}
Console.WriteLine(sb);

But the output is not what we really want. It contains only the static HTML tags. The server controls inside our control are not rendered. Why? Because in our way, the control does not follow the page life cycle. So, what we should do is to add it to a page.

Page page = new Page();
Control myControl = LoadControl("~/MyControl.ascx");
StringBuilder sb = new StringBuilder();

page.Controls.Add(myControl);

using (StringWriter sw = new StringWriter(sb))
{
	Server.Execute(page, sw, false);
}

Console.WriteLine(sb);

Server.Execute method executes an IHttpHandler object and writes the output using a TextWriter object. In this way we follow the whole page life cycle so our control will render everything inside it.

This post is tagged under | |

Crack .NET

New great debugging tool has come to the web. Crack .NET allows you to go through the managed heap of your Windows Desktop application (Windows Forms or WPF) and see what it contains. Not only you can see what’s inside, but you can modify it using IronPython scripts.

Crack .NET

Read the rest of this entry »

This post is tagged under | | |

Silverlight Write & Win Contest

Silverlight 2 ContestSilverlight Write & Win Contest was carried out for the second time. It is a interesting contest. You only have to write an article about Silverlight (application, control, game, web services, Blend, design, example, problem solution, etc). The first contest was organized by Michael Sync. Now, SilverlightShow take this initiative. They really did a great job in the organization.

I took part in the first edition, so I decided to write an article for this one, too. And I got it! I won the third prize, which looks like this:

  • Telerik – RadControls for ASP.NET AJAX Developer Subscription and Source Code License ($999 value). This license includes all 23 Telerik ASP.NET AJAX controls, a Gold Support Package and free product updates for a period of one year. The winners will also receive complimentary licenses for Telerik RadControls for Silverlight
  • SilverlightShow - $100 in Amazon Gift certificate or cache

Cool, right? The one who get the first prize is Alexey Zakharov with his article - Virtual earth deep zooming.

SilverlightShow announced that they intend to organize another contest, so I am waiting for it.

This post is tagged under | |

StyleCop

Every programming language has its own coding style. It is very essential that every developer, who uses the language, keeps to its style. When a team of developers work over a project, they should be able to read easily the code written by everyone in the team. It does make sense how the code is written!

Here are some coding rules, when using C#:

  • Avoid writing very long methods. A method should typically have 1~25 lines of code. If a method has more than 25 lines of code, you must consider re factoring into separate methods.
  • Use String.Empty instead of “”

    Good:

    if (name == String.Empty) { // do something }

    Bad:

    if (name == "") { // do something }
  • Convert strings to lowercase or upper case before comparing. This will ensure the string will match even if the string being compared has a different case.
    if (name.ToLower() == "john") { //… }

StyleCop analyzes C# source code to enforce a set of style and consistency rules. It can be run from inside of Visual Studio or integrated into an MSBuild project. Don’t hesitate and download this great tool!

Silverlight Version 2 RC0 Released

Good news for the Silverlight fans - the first Silverlight release candidate is released. Note that this is not the final runtime, it is developer only. It is released to let developers get into the changes and reform their Silverlight beta 2 applications. There are some breaking changes between Beta2 and this RC. You can take a look at this great article for most of them. As such, you can only use the RC for development right now - you can’t go live with the new APIs until the final release is shipped (which will be soon though).

You can download today’s Silverlight Release Candidate and accompanying VS and Blend support for it here.  Note that Expression Blend support for Silverlight 2 is now provided using Blend 2.0 SP1.  You will need to install Blend 2.0 before applying the SP1 service pack that adds Silverlight 2 support.  If you don’t already have Blend 2.0 installed you can download a free trial of it here. Read the rest of this entry »

This post is tagged under | | | | |
« Previous Entries  

Meta

  • Valid XHTML
  • XFN
  • RSS
  • Comments RSS

Calendar

July 2009
M T W T F S S
« Nov    
 12345
6789101112
13141516171819
20212223242526
2728293031