Showing posts from 2011

Effective usage of the value parameter in setters: an example with WebHeaderCollection

A couple of days ago I had to implement a read/write property that would store a WebHeaderCollection associated with a web response. My first take on that was to create an automatic property that would handle getting and setting the values. So far, so good.
Yet, when I tried to assign the value wrapped by that property to a Header collection associated with a Request object, the application threw an exception. After googling awhile I found out that ASP.NET prevents the code from setting some HTTP headers for the request. So, if I were to set the property appropriately, only “eligible” headers would be copied from the response. I decided to stash the filtering in the setter:

private staticWebHeaderCollection _WikiHeaders;
public staticWebHeaderCollection WikiHeaders
return _WikiHeaders;
    if (null != value asWebHeaderCollection)
// the value parameter is implicitly typed as WebHeaderCollection
foreach (string webHeader invalue.Keys)

The ListView control: A tip how to insert ItemTemplate into LayoutTemplate

Add an HTML element to the <LayoutTemplate> container (your item template will be nested in this element).Set the element's ID to itemPlaceholder. Otherwise the ASP.NET template parser will throw an error.ExampleConsider the following fragment of a web form:
<asp:ListView ID="ListView1" DataSourceID="XmlDataSource1" runat="server"> <LayoutTemplate> <div runat="server" id="itemPlaceholder" /> </LayoutTemplate> <ItemTemplate> <div style="clear:both"> <div style="float:left"><%# Eval("title")%></div> <div style="float:left"><%# Eval("author")%></div> </div> </ItemTemplate> </asp:ListView> This markup will output HTML markup similar to the one below:
<div> <!-- results from <div runat="server" id="itemPlaceholder" /&g…

Parse data-binding expressions in typical and atypical ways

Most ASP.NET developers bind data items to the page in the way like below:
<%# Eval(Container.DataItem, "SomeProperty")%>But there is a less known technique to achieve the same.
The Page object contains a method to retrieve the data item at the top of the data-binding context stack - GetDataItem(). In other words, this method returns a reference to the successive item, to which the Page object moves while iterating through bound data sources.
How can you leverage this functionality? In your page’s code-behind, add a property that returns a string (later you will want to insert this string into your markup):
protected string SomeProperty {   get   {     object dataItem =         this.GetDataItem(); // dataItem == Container.DataItemobject _SomeProperty =         DataBinder.Eval(dataItem, "SomeProperty");     //some processing goes herereturn _SomeProperty as string;   } }In the markup, add a binding expression that references the property you have created:

Filter XML elements by their namespace using XSLT

XSL Transformations (XSLT) is a powerful tool that can do much chore for us developers. Occasionally, XSLTs can compete with the other tools intended to transform XML. Look, for example, at the snippet that follows:
<xsl:templatematch="o:*" />
It strips out all the elements that are defined in the namespace referenced by alias o.
ExplanationA normal template expression has a format similar to the one of below:
  <xsl:apply-templatesselect="author" />
This example processes all the author children of the author-group. By default, the XSLT processor processes each matched element only once. After an author-group element is selected, the processor outputs the author children of the matched element to XML. So, if the xsl:apply-templates element is omitted, the children of the source element are skipped during the transformation.