Project: Asp.Net.Rendering

How to render a control using LINQPad and execute data binding:
void Main()
{
	var gridView = new GridView ();
	gridView.DataSource = new CustomerService().GetAll();
	gridView.DataBind();
	Util.RawHtml(gridView.Render()).Dump();
}

public class CustomerService
{
	public IEnumerable<Customer> GetAll()
	{
		yield return new Customer { Name = "Bill" };
		yield return new Customer { Name = "Steve" };
	}
}

public class Customer
{
	public string Name { get; set; }
}

public static class ControlExtensions
{
	public static string Render(this Control control) 
	{
		var stringBuilder = new StringBuilder();

		using (var stringWriter = new StringWriter(stringBuilder))
		{		
			using (var htmlTextWriter = new HtmlTextWriter(stringWriter))
			{
				control.RenderControl(htmlTextWriter);
				return stringBuilder.ToString();
			}
		}
	}
}

Last edited Mar 20, 2012 at 11:47 AM by teoarch, version 1

Comments

No comments yet.