views:

241

answers:

2

I have a data view web part on a page which is displaying a table of data.

I want to allow users to choose which column to sort by. I think the only way to do this is go into the data view properties, and enable the toolbar with sorting. This works and allows the user to select a column to sort using a dropdown list of columns. The default is 'none', and when the page first loads, there is no sorting applied (rows are shown by their ID, which is pretty much random).

I want the default sort to be a particular column - i.e. when the page is first opened, I want the data to be sorted by a 'Surname' column. I can't figure out how to achieve this using the toolbar sort.

I have tried specifying an ORDER BY SURNAME in the SQL statement which gets the data - however this ends up overriding the toolbar sort, so sorting is always by surname, regardless of the toolbar sort selection.

There is another 'sorting' option in the data view properties which allows you to specify a sort, however again this overrides everything and renders the toolbar sorting option useless (selectiong something else has no effect).

I'm thinking surely there must be a solution to this simple problem?

The generated code for the sorting toolbar is below. I have tried making the surname the selected item in the dropdown box by changing to - this makes the surname column selected in the dropdown by default but the sorting isn't actually applied.

Any ideas?

<table cellSpacing="0" cellPadding="2" border="0" class="ms-toolbar" style="margin-left: 3px; margin-right: 5px;">
        <tr>
            <td id="dvt_tb_sort" nowrap=""><table><tr><td nowrap="" class="ms-toolbar"><nobr>Sort by: <select>
                <xsl:variable name="clvar1_dvt_sortfield">&apos; + this.options[this.selectedIndex].value + &apos;</xsl:variable>
                <xsl:variable name="clvar2_dvt_sortfield">&apos; + this.options[this.selectedIndex].fieldtype + &apos;</xsl:variable>
                <xsl:variable name="clvar3_dvt_sortfield">&apos; + this.options[this.selectedIndex].title + &apos;</xsl:variable>
                <xsl:variable name="clvar4_dvt_sortfield">&apos; + this.options[this.selectedIndex].sorttype + &apos;</xsl:variable>
                <xsl:attribute name="OnChange">javascript:<xsl:value-of select="ddwrt:GenFireServerEvent(concat('NotUTF8;dvt_sortfield={', $clvar1_dvt_sortfield, '};dvt_sortdir={', $dvt_sortdir, '};dvt_sorttype={', $clvar4_dvt_sortfield, '}'))" /></xsl:attribute>
                <option value="">None</option>
                <option value="Location">
                <xsl:if test="$dvt_sortfield='Location'">
                    <xsl:attribute name="selected">yes</xsl:attribute>
                </xsl:if>
                Location</option>
                <option value="Email">
                <xsl:if test="$dvt_sortfield='Email'">
                    <xsl:attribute name="selected">yes</xsl:attribute>
                </xsl:if>
                Email</option>
                <option value="Mobile">
                <xsl:if test="$dvt_sortfield='Mobile'">
                    <xsl:attribute name="selected">yes</xsl:attribute>
                </xsl:if>
                Mobile</option>
                <option value="Position">
                <xsl:if test="$dvt_sortfield='Position'">
                    <xsl:attribute name="selected">yes</xsl:attribute>
                </xsl:if>
                Position</option>
                <option value="Telephone">
                <xsl:if test="$dvt_sortfield='Telephone' and not($dvt_sorttype='number')">
                    <xsl:attribute name="selected">yes</xsl:attribute>
                </xsl:if>
                Telephone</option>
                <option value="Telephone" sorttype="number">
                <xsl:if test="$dvt_sortfield='Telephone' and $dvt_sorttype='number'">
                    <xsl:attribute name="selected">yes</xsl:attribute>
                </xsl:if>
                Telephone(Number)</option>
                <option value="Forename">
                <xsl:if test="$dvt_sortfield='Forename' or $dvt_sortfield=''">
                    <xsl:attribute name="selected">yes</xsl:attribute>
                </xsl:if>
                Forename</option>
                <option value="Surname">
                <xsl:if test="$dvt_sortfield='Surname'">
                    <xsl:attribute name="selected">yes</xsl:attribute>
                </xsl:if>
                Surname</option>
                </select><a>
                <xsl:attribute name="href">
                    <xsl:choose>
                        <xsl:when test="$dvt_sortdir='descending'">javascript:<xsl:value-of select="ddwrt:GenFireServerEvent(concat('dvt_sortfield={', $dvt_sortfield, '};dvt_sortdir={ascending}'))" /></xsl:when>
                        <xsl:otherwise>javascript:<xsl:value-of select="ddwrt:GenFireServerEvent(concat('dvt_sortfield={', $dvt_sortfield, '};dvt_sortdir={descending}'))" /></xsl:otherwise>
                    </xsl:choose>
                </xsl:attribute>
                <xsl:if test="$dvt_sortfield" ddwrt:cf_ignore="1"><img border="0">
                    <xsl:attribute name="src">
                        <xsl:choose>
                            <xsl:when test="$dvt_sortdir='descending'"><xsl:value-of select="ddwrt:FieldSortImageUrl('Asc')" /></xsl:when>
                            <xsl:otherwise><xsl:value-of select="ddwrt:FieldSortImageUrl('Desc')" /></xsl:otherwise>
                        </xsl:choose>
                    </xsl:attribute>
                    <xsl:attribute name="alt">
                        <xsl:choose>
                            <xsl:when test="$dvt_sortdir='descending'">Descending</xsl:when>
                            <xsl:otherwise>Ascending</xsl:otherwise>
                        </xsl:choose>
                    </xsl:attribute>
                    </img></xsl:if>
                </a></nobr></td></tr></table></td><td width="99%"></td>
        </tr>
    </table>
A: 

What happens if you select the Surname column first in the SQL?

Nat
Unfortunatley, this seems to make no difference :(
James Allen
A: 

James, Search for the parameter binding labeled dvt_sortfield, then manually write in: Default="FIELDNAME" line. This should do the trick.

ParameterBinding Name="dvt_sortfield" Default="ID" Location="Postback;Connection"

If you need to change the default sort there is also a sort parameter called dvt_sortdir and you can set it again in the ParameterBinding tag.

Dan