Redigera

Dela via


LinqDataSource.SelectParameters Property

Definition

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

public:
 property System::Web::UI::WebControls::ParameterCollection ^ SelectParameters { System::Web::UI::WebControls::ParameterCollection ^ get(); };
[System.ComponentModel.Browsable(false)]
[System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerProperty)]
public System.Web.UI.WebControls.ParameterCollection SelectParameters { get; }
[<System.ComponentModel.Browsable(false)>]
[<System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerProperty)>]
member this.SelectParameters : System.Web.UI.WebControls.ParameterCollection
Public ReadOnly Property SelectParameters As ParameterCollection

Property Value

The parameters that are used to create the Select clause.

Attributes

Examples

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>
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>

Remarks

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.

Applies to