Dynamically call a xslt template

You cannot call a xslt template like:

<xsl:variable name="templateName" select="test"></xsl:variable>
<xsl:call-template name="$templateName">

<xsl:template name="test">
</xsl:template>


The clossed you can get to calling a xslt template dynamically is to use the <xsl:if or <xsl:choose based on a variable:

<xsl:if test="$parameter1=$templateName">
  <xsl:call-template name="test">
</xsl:if>

Generic C# function for reading a value from registry

Nice generic read value from registry function on: http://www.codeproject.com/KB/dotnet/frameworkversiondetection.aspx
Function can be called like:

using System;
using Microsoft.Win32;
using NUnit.Framework;
using Ada.Cdf.Common;
namespace Ada.Cdf.Test.Common
{
[TestFixture]
public class RegistryHelperTester
{
[Test]
public void GetRegistryValueTest()
{
string data = string.Empty;
// Read a registry key value
bool result = RegistryHelper.GetRegistryValue<string>(RegistryHive.LocalMachine,@"SOFTWARE\Microsoft\Windows\CurrentVersion","ProgramFilesDir", RegistryValueKind.String, out data);
// Show result
Console.WriteLine(data);
}
}
}


Result on my machine

C:\Program Files (x86)

using System;
using System.Globalization;
using Microsoft.Win32;
namespace Ada.Cdf.Common
{
public class RegistryHelper
{
/// <summary>
/// Read the value of a key from registry
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="hive"></param>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="kind"></param>
/// <param name="data"></param>
/// <returns></returns>
public static bool GetRegistryValue<T>(RegistryHive hive, string key, string value, RegistryValueKind kind, out T data)
{
bool success = false;
data = default(T);
using (RegistryKey baseKey = RegistryKey.OpenRemoteBaseKey(hive, String.Empty))
{
if (baseKey != null)
{
using (RegistryKey registryKey = baseKey.OpenSubKey(key, RegistryKeyPermissionCheck.ReadSubTree))
{
if (registryKey != null)
{
// If the key was opened, try to retrieve the value.
RegistryValueKind kindFound = registryKey.GetValueKind(value);
if (kindFound == kind)
{
object regValue = registryKey.GetValue(value, null);
if (regValue != null)
{
data = (T)Convert.ChangeType(regValue, typeof(T), CultureInfo.InvariantCulture);
success = true;
}
}
}
}
}
}
return success;
}
}
}

IE 6 and Telerik error: Sys.InvalidOperationException Telerik.Web.UI.Orientation has already been registered

If you get the error: Microsoft JScript runtime error: Sys.InvalidOperationException: Type Telerik.Web.UI.Orientation has already been registered. The type may be defined multiple times or the script file that defines it may have already been loaded. A possible cause is a change of settings during a partial update. in IE 6, change the <asp:ScriptManager in you’re master page to <telerik:RadScriptManager

How disabling viewstate on ASP .NET 2.0 website effects you’re code

You can disable viewstate on an ASP .NET 2.0 website by setting the enabledViewState=”false” on the configuration > system.web > pages node in the Web.config:

<
pages enableViewState="false">

Then you will have to fill dropdownlists etc. on every page postback in the OnInit event of the page and some times have to use the good old: this.Request.Form

string
selectedValue = this.Request.Form["ctl00$MainContentPlaceHolder$selectBatchDropDownList"];
The "ctl00$MainContentPlaceHolder$selectBatchDropDownList"  is the control UniqueID (selectedBatchDropDownList.UniqueID)

However, after reading some articles:
http://dotneteers.net/blogs/petersm/archive/2006/11/09/how-to-put-controlstate-into-viewstate-and-how-to-put-viewstate-into-session.aspx
http://weblogs.asp.net/ngur/archive/2004/03/08/85876.aspx 
http://blog.tatham.oddie.com.au/2008/12/18/how-i-learned-to-stop-worrying-and-love-the-view-state/

I re-enabled ViewState and started filling dropdownlists on the page “OnInit” event and saved the viewstate in the sessionstate.
This allowed me to use the normal code and reduced the viewstate size to 50 characters.

To persist viewstate to sessionstate use:

        /// <summary>
/// Set viewstate to sessionstate
/// </summary>
protected override PageStatePersister PageStatePersister
{
get
{
return new SessionPageStatePersister(this);
}
}

In the web.config

<system.web>
<browserCaps>
<case>
RequiresControlStateInSession=true
</case>
</browserCaps>


To completely remove viewstate information use:

/// <summary>
/// Completly disable viewstate for performance
/// </summary>
/// <param name="viewState"></param>
protected override void SavePageStateToPersistenceMedium(object viewState)
{
}
/// <summary>
/// Completly disable viewstate  for performance
/// </summary>
/// <returns></returns>
protected override object LoadPageStateFromPersistenceMedium()
{
return null;
}

.NET service (.svc) and “The page you are requesting cannot be served because of the extension configuration”

When you want to debug a .net service (*.svc) and you get the error: “The page you are requesting cannot be served because of the extension configuration”.

Try the solution on: http://tonytriguero.com/iis-7-and-webservices-svc-file-extension/

  • Open Visual Studio 2008 Command prompt.
  • Navigate to C:\Windows\Microsoft.NET\Framework\v3.0\Windows Communication Foundation
  • Run this command: servicemodelreg –i
  • Set initial sorting on a Telerik RadGrid column

    If you want to set an initial sorting on a column on a Telerik RadGrid, use the “NeedDataSource” event and add a GridSortExpression to the MasterTableView.SortExpressions

            protected void RadGrid_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
    {
    // Set datasource here…
    
    if (!IsPostBack)
    {
                    // Clear the current sortexpressions
    this.RadGrid.MasterTableView.SortExpressions.Clear();
    // Create "Date" sorting
    GridSortExpression expression = new GridSortExpression();
    expression.FieldName = "Date";
    expression.SortOrder = GridSortOrder.Descending;
    // Set initial sortexpression to the [Date] column
    this.opdrachtenRadGrid.MasterTableView.SortExpressions.AddSortExpression(expression);

                }
    }

    In the aspx page:

                  <telerik:GridDateTimeColumn
    DataField="Date"
    HeaderText="Date"
    SortExpression="Date"
    UniqueName="Date"
    FilterControlWidth="70px"
    ShowFilterIcon="false"
    DataType="System.DateTime"
    CurrentFilterFunction="EqualTo"
    PickerType="DatePicker"
    AutoPostBackOnFilter="true"
    HtmlEncode="false"
    DataFormatString="{0:dd-MM-yyyy}"
    meta:resourcekey="Date">
    <ItemStyle Width="70px" />
    </telerik:GridDateTimeColumn>


    Or you could use the “SortExpressions” tag in the MasterTableView tag:

    <telerik:RadGrid ID="RadGrid1" runat="server"> <MasterTableView> <SortExpressions> <telerik:GridSortExpression FieldName="Name" SortOrder="Ascending" /> </SortExpressions> <Columns> <telerik:GridBoundColumn AutoPostBackOnFilter="true" CurrentFilterFunction="Contains" DataField="Name" FilterControlWidth="200px" meta:resourcekey="CustomerNameColumn" UniqueName="Name" SortExpression="Name" ShowFilterIcon="true"> <ItemStyle Width="200px" /> </telerik:GridBoundColumn>

    ……

    Set initial filter on GridDateTimeColumn in a Telerik RadGrid

    If you want to set an initial filter on a Telerik RadGrid, use the “NeedDataSource” event and set the MasterTableView.FilterExpression and use the function MasterTableView.GetColumnSafe

            protected void RadGrid_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
    {
    // Set datasource here…
    
    if (!IsPostBack)
    {
    // Set initial filter on [RadGrid]
    this.RadGrid.MasterTableView.FilterExpression = string.Format("([Date] = '{0}')", DateTime.Today);
    // Set current filter function and value on [Date]
    GridColumn column = this.RadGrid.MasterTableView.GetColumnSafe("Date");
    column.CurrentFilterFunction = GridKnownFunction.EqualTo;
    column.CurrentFilterValue = DateTime.Today.ToString();
    }
    }

    In the aspx page use “EqualTo” as filter, not “Contains” and use a GridDateTimeColumn not a GridBoundColumn

                  <telerik:GridDateTimeColumn
    DataField="Date"
    HeaderText="Date"
    SortExpression="Date"
    UniqueName="Date"
    FilterControlWidth="70px"
    ShowFilterIcon="false"
    DataType="System.DateTime"
    CurrentFilterFunction="EqualTo"
    PickerType="DatePicker"
    AutoPostBackOnFilter="true"
    HtmlEncode="false"
    DataFormatString="{0:dd-MM-yyyy}"
    meta:resourcekey="Date">
    <ItemStyle Width="70px" />
    </telerik:GridDateTimeColumn>

    Cannot find column Customer when sorting a Telerik RadGrid column with LLBLGen entities

    When you sort a column in a Telerik RadGrid you get the error “Cannot find column Customer”, make sure the  SortExpression is the same as the DataField

                       DataField="Customer.Description"
    HeaderText="Customer"
    SortExpression="Customer.Description"
    UniqueName="Customer" 


    If SortExpression is not the same as the DataFiel, you will get the error:

    Message: Cannot find column Customer

    Exception: System.IndexOutOfRangeException

    Targetsite: System.Data.IndexField[] ParseSortString(System.String)

    Source: System.Data

    StackTrace: at System.Data.DataTable.ParseSortString(String sortString)

    at System.Data.DataView.CheckSort(String sort)

    at System.Data.DataView.set_Sort(String value)

    at Telerik.Web.UI.GridEnumerableFromDataView.PerformTransformation()

    at Telerik.Web.UI.GridEnumerableFromDataView.TransformEnumerable()

    at Telerik.Web.UI.GridTableView.GetEnumerator(Boolean useDataSource, GridEnumerableBase resolvedDataSource, ArrayList dataKeysArray)

    at Telerik.Web.UI.GridTableView.CreateControlHierarchy(Boolean useDataSource)

    at Telerik.Web.UI.GridTableView.CreateChildControls(IEnumerable dataSource, Boolean useDataSource)

    at System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data)

    at System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data)

    at System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback)

    at System.Web.UI.WebControls.DataBoundControl.PerformSelect()

    at Telerik.Web.UI.GridTableView.PerformSelect()

    at System.Web.UI.WebControls.BaseDataBoundControl.DataBind()

    at Telerik.Web.UI.GridTableView.DataBind()

    at Telerik.Web.UI.GridSortCommandEventArgs.ExecuteCommand(Object source)

    at Telerik.Web.UI.RadGrid.OnBubbleEvent(Object source, EventArgs e)

    at System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)

    at Telerik.Web.UI.GridItem.OnBubbleEvent(Object source, EventArgs e)

    at System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)

    at Telerik.Web.UI.GridItem.OnBubbleEvent(Object source, EventArgs e)

    at System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)

    at System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e)

    at System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument)

    at System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)

    at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)

    at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)

    at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

    Data count: 0

    IE 8 Developer Tools do not show when pressing F12 (Windows 7)

    If you press F12 in IE 8, the developer tools should show (In IE7 this was the developer toolbar). But if you’re developer tools are “un-pinned” it will be a separate window, by hovering the IE icon on the widows bar, you will see the developer tools window (right window):

    image

    Right click on the Developer Tools preview windows and click maximize, then press Ctrl+P to pin the developer tools.

    image