Tuesday, May 29, 2007

Using Parameters in the SelectCommand of a Data View Web Part

The SelectCommand attribute of the SPDataSource element within a SharePoint DataView web part can use parameters supplied to the web part - and these parameters can come from a varity of sources, including the querystring in the page request.

This opens the way to easily control the data displayed in the data view; for example, via an ID supplied in the querystring.

The following code snippet shows some of the DataFormWebPart elements that use a value in the querystring to select a single list item to be displayed (the parameter is named SelectedRisk). Notice how the parameter is referenced in the SelectCommand:

<DataSources>

    
<SharePoint:SPDataSource runat="server" SelectCommand="<View><Query><Where><Eq><FieldRef Name="ID"/><Value Type="Counter">{SelectedRisk}</Value></Eq></Where></Query></View>" ...>

        
<SelectParameters>

            
<WebPartPages:DataFormParameter PropertyName="ParameterValues" ParameterKey="SelectedRisk" DefaultValue="0" Name="SelectedRisk"></WebPartPages:DataFormParameter>

        
</SelectParameters>

    
</SharePoint:SPDataSource>

</DataSources>

<ParameterBindings>

    
<ParameterBinding Name="SelectedRisk" Location="QueryString(ID)" DefaultValue="0"/>

</
ParameterBindings>

3 comments:

newis said...

Help pls:
For:
[SelectParameters]
[WebPartPages:DataFormParameter PropertyName="UserId1" ParameterKey="UserId1" Name="UserId1" /]
[/SelectParameters]
[...]
[ParameterBindings]
[ParameterBinding Name="UserId1" Location="ServerVariable(LOGON_USER)" DefaultValue="0"/]
[/ParameterBindings]

I got next:
Error while executing web part: System.Web.HttpException: DataBinding: 'Microsoft.SharePoint.WebPartPages.DataFormWebPart' does not contain a property with the name 'UserId1'

Alessandro said...

hi, I've the same error: DataBinding: 'Microsoft.SharePoint.WebPartPages.WebPartPage' does not contain a property with the name 'ParameterValues'.
could you help me?
thank you in advance

alessandro

whats.to.learn.today said...

Hi Alessandro,

I think you may have the values in your custom DataFormParameter wrong.

Every custom WebPartPages:DataFormParameter that you add requires the PropertyName attribute set to "ParameterValues". This is then used when parsing the values.

Set the ParameterKey and Name attributes to the name of your new parameter - the name of "SelectedRisk" in my example demonstrates the values you need to change in the SPDataSource, the DataFormParameter and the ParameterBinding elements.

Hope that helps