Feeds:
Posts
Comments

Archive for the ‘.NET’ Category

In a hyperlink column of gridview, suppose  the value of the property DataNavigateUrlFormatString is “ProductsForSupplierDetails.aspx?SupplierID={0}”. Many people think that the argument {0} is replaced by the value of the property DataKeyNames of the Gridview. But in actual case this is replaced by the value of the property DataNavigateUrlFields of the hyperlink column.

Now suppose you want to pass two arguments instead of one, something like this “ProductsForSupplierDetails.aspx?SupplierID={0}&CompanyName={1}”.

Now as these two arguments come from property DataNavigateUrlFields of the hyperlink column. So value of this property has to be “DataNavigateUrlFields=”SupplierID,CompanyName”.

At run time the .NET replaces {0} with “SupplierID” and {1} with “CompanyName” of that row.

A complete sample code will look like this:-

<asp:GridView ID=”GridView1″ runat=”server” AutoGenerateColumns=”False” DataKeyNames=”SupplierID” DataSourceID=”ObjectDataSource1″>
<Columns>
<asp:BoundField DataField=”SupplierID” HeaderText=”SupplierID” InsertVisible=”False” ReadOnly=”True” SortExpression=”SupplierID” />
<asp:BoundField DataField=”CompanyName” HeaderText=”CompanyName” SortExpression=”CompanyName” />
<asp:BoundField DataField=”Address” HeaderText=”Address” SortExpression=”Address” />
<asp:BoundField DataField=”City” HeaderText=”City” SortExpression=”City” />
<asp:BoundField DataField=”Country” HeaderText=”Country” SortExpression=”Country” />
<asp:BoundField DataField=”Phone” HeaderText=”Phone” SortExpression=”Phone” />
<asp:HyperLinkField DataNavigateUrlFields=”SupplierID,CompanyName” DataNavigateUrlFormatString=”ProductsForSupplierDetails.aspx?SupplierID={0}&CompanyName={1}” Text=”View Products” />
</Columns>
</asp:GridView>

Pretty simple and straightforward it seems but I have seen many people struggling with this simple concept. Also I found wrong answers for this problem in some .NET forums. Hence thought to post it here for benefit of masses.

~Mayank

 

Advertisements

Read Full Post »

Many times it is our requirement to perform various types of processing in XSL file using functions. There are many in-built functions in XSL for doing various types of tasks like string manipulation, number calculations etc but sometimes we need to do some custom processing which can only be achieved by writing our own custom functions. These custom functions can be written in either JavaScript or in some other language. This BOK focuses on how to write these custom functions in a .NET compliant language (like VB.NET, C#) and then use it in our XSL file.

Here are the steps to achieve the same:-

1. Create the custom function in a class. If you need to write many custom functions, it is better to place these functions in a separate class; otherwise the function can be written in same class file as your calling code.

Here a separate class “XSLCustomFunctions” is created which has a function for date formatting.

Public Class XSLCustomFunctions
Public Function Format_Date(ByVal sDateTime As String) As DateTime
         Return CDate(sDateTime)
     End Function
End Class

Point to Note – Here class name is “XSLCustomFunctions” and custom function name is “Format_Date”

2. Create an object of the custom function class. This has to be done in the function where the XSL transform is being applied.

Dim oCustFunc As New XSLCustomFunctions

3. Now we need to create XML namespace declaration in the XSL file for this custom class

<xsl:stylesheet version=”1.0″ xmlns:xsl=”http://www.w3.org/1999/XSL/Transform” xmlns:myCustFunc=”urn:custFunc”>

Point to Note – Here “myCustFunc” is the XML namespace which refers to “urn:custFunc”. This will be passed as XSL extension while adding XSL extension object in step 5

4. Use the custom function in the XSL at the required place. Please note that function is used in the format – “XML_NameSpace:CustomFunction”.

<xsl:value-of select=”myCustFunc:Format_Date(Value)” />

5. Create an object of XSLT argument list in the VB.NET code and add extension object to it and then only apply the XSL transform.

Private Function ApplyXSLTransform(ByVal sXml As String) As String

Dim oArgsList As New XsltArgumentList
      Dim oXmlDocument As New XmlDocument
      Dim oXslTransform As New XslTransform
Dim oStringWriter As New System.IO.StringWriter
Dim oCustFunc As New XSLCustomFunctions

oXmlDocument.LoadXml(sXml)
oXslTransform.Load(Server.MapPath(“xsl_doc.xsl”))

oArgsList.AddExtensionObject(“urn:custFunc”, oCustFunc)

oXslTransform.Transform(oXmlDocument, oArgsList, oStringWriter, Nothing)

Return oStringWriter.ToString
End Function

In this way, we can use our own custom functions created in VB.NET/C# in the XSL file. 

~Mayank

Read Full Post »