Export (0) Print
Expand All

LinqDataSource.SelectParameters Property

Gets the collection of parameters that are used during a data-retrieval operation.

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web.Extensions (in System.Web.Extensions.dll)

[BrowsableAttribute(false)]
[PersistenceModeAttribute(PersistenceMode.InnerProperty)]
public ParameterCollection SelectParameters { get; }

Property Value

Type: System.Web.UI.WebControls.ParameterCollection
The parameters that are used to create the Select clause.

The LinqDataSource control uses parameters in the SelectParameters collection to create the Select clause at run time. You add parameters to the SelectParameters collection when you want to use run-time values in the Select clause. For example, you can add a parameter to the SelectParameters collection to represent a property in the user's profile. You can then use that property and a value from the data source to calculate a new value.

If you do not have to set a value at run time in the Select clause, you do not have to use the SelectParameters collection. You can define the properties to retrieve in the Select property. For example, to return the FirstName and LastName values from a database table, set Select to "FirstName, LastName" without any parameters.

To set values in the SelectParameters collection, you add a placeholder in the Select property for the named parameter. In the Select clause, preface each parameter name with the @ symbol.

You cannot use a parameter in the SelectParameters collection to represent a property name. To dynamically set the Select property to the name of a property, create an event handler for the Selecting event and customize the Select property as needed.

The following example shows how to use a user-supplied value to calculate a value in the returned data. The user can enter a value in the text box that represents the number of manufacturing days. That value is divided by a value from a database that represents the number of days to manufacture a product. The returned value indicates how many products can be manufactured during the specified number of days. The user's input is included in the Select command through the SelectParameters collection.


Enter number of manufacturing days:
<asp:TextBox Text="1" ID="TextBox1" runat="server"></asp:TextBox><br />
<asp:Button ID="Button1" runat="server" Text="Refresh" /><br />
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
    AllowSorting="True" AutoGenerateColumns="False" DataSourceID="LinqDataSource1">
    <Columns>
        <asp:boundfield DataField="Name" 
            HeaderText="Name" 
            ReadOnly="True" 
            SortExpression="Name">
        </asp:boundfield>
        <asp:boundfield DataField="NumberToManufacture" 
            HeaderText="Number to Manufacture" 
            ReadOnly="True" 
            SortExpression="NumberToManufacture">
        </asp:boundfield>
    </Columns>
</asp:GridView>
<asp:LinqDataSource 
    ContextTypeName="ExampleDataContext" 
    TableName="Products"
    Where="DaysToManufacture > 0 "
    Select="new (Name, @Days / DaysToManufacture As NumberToManufacture)" 
    ID="LinqDataSource1" 
    runat="server">
  <SelectParameters>
    <asp:ControlParameter 
        Type="Decimal" 
        Name="Days" 
        ControlID="TextBox1" 
        DefaultValue="1" />
  </SelectParameters>
</asp:LinqDataSource>


.NET Framework

Supported in: 4, 3.5

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Community Additions

ADD
Show:
© 2014 Microsoft