0

我想为用户添加一个下拉列表以选择所有记录,未来 2 个月等,然后刷新 gridview。

如何通过后面的代码将 where 参数添加到 linqdatasource ?

<telerik:RadDropDownList ID="rdl_filter_date" runat="server" OnSelectedIndexChanged="rdl_filter_date_SelectedIndexChanged">
    <Items>
    <telerik:DropDownListItem Text="All" Value="all" />
    <telerik:DropDownListItem Text="Coming 2 months" Value="2mths" />
    </Items>
    </telerik:RadDropDownList>

    <telerik:RadGrid ID="RadGrid1" runat="server" CellSpacing="0" AutoGenerateColumns="False" 
        DataSourceID="LDS_UpcomingTraining" GridLines="None" Width="700">
<MasterTableView AutoGenerateColumns="False" DataSourceID="LDS_UpcomingTraining">
<CommandItemSettings ExportToPdfText="Export to PDF"></CommandItemSettings>

<RowIndicatorColumn Visible="True" FilterControlAltText="Filter RowIndicator column">
<HeaderStyle Width="20px"></HeaderStyle>
</RowIndicatorColumn>

<ExpandCollapseColumn Visible="True" FilterControlAltText="Filter ExpandColumn column">
<HeaderStyle Width="20px"></HeaderStyle>
</ExpandCollapseColumn>

    <Columns>
        <telerik:GridBoundColumn DataField="db_course.Course_Name" 
            FilterControlAltText="Filter Course Name" 
            HeaderText="Course Name" SortExpression="db_course.Course_Name" 
            UniqueName="db_course.Course_Name"  ReadOnly="True">
        </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="Start_Time" DataType="System.DateTime" 
            FilterControlAltText="Filter Start_Time column" HeaderText="Start Time" 
            ReadOnly="True" SortExpression="Start_Time" UniqueName="Start_Time"  DataFormatString="{0:dd-MMM-yyyy hh:mm tt}">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="End_Time" DataType="System.DateTime" 
            FilterControlAltText="Filter End_Time column" HeaderText="End Time" 
            ReadOnly="True" SortExpression="End_Time" UniqueName="End_Time"  DataFormatString="{0:dd-MMM-yyyy hh:mm tt}">
        </telerik:GridBoundColumn>
                <telerik:GridHyperLinkColumn Text="View" HeaderText="Online Teaching Material" DataNavigateUrlFields="db_training_session.MaterialURL" Target="_blank" UniqueName="HyperLinkColumn" AllowFiltering="false">   
        </telerik:GridHyperLinkColumn>

    </Columns>

<EditFormSettings>
<EditColumn FilterControlAltText="Filter EditCommandColumn column"></EditColumn>
</EditFormSettings>

<PagerStyle PageSizeControlType="RadComboBox"></PagerStyle>
</MasterTableView>

<PagerStyle PageSizeControlType="RadComboBox"></PagerStyle>

<FilterMenu EnableImageSprites="False"></FilterMenu>
    </telerik:RadGrid>
        <asp:LinqDataSource ID="LDS_UpcomingTraining" runat="server" 
        ContextTypeName="dcLRDBDataContext" EntityTypeName="" 
        Select="new (db_Course, Start_Time, End_Time, db_Training_Session)" 
        TableName="db_Employee_Enrollments" 
        Where="UserID == @UserID &amp;&amp; db_Training_Session.Status == @db_Training_Session and Start_Time >= DateTime.Now" OrderBy="Start_Time">
        <WhereParameters>
            <asp:SessionParameter Name="UserID" SessionField="UserID" Type="Int32" />
            <asp:Parameter DefaultValue="Confirmed" Name="db_Training_Session" 
                Type="String" />

        </WhereParameters>
    </asp:LinqDataSource>

背后的代码

Protected Sub rdl_filter_date_SelectedIndexChanged(ByVal sender As Object, ByVal e As Telerik.Web.UI.DropDownListEventArgs) Handles rdl_filter_date.SelectedIndexChanged
    If rdl_filter_date.SelectedValue = "2mths" Then

    ElseIf rdl_filter_date.SelectedValue = "all" Then

    End If
End Sub
4

1 回答 1

0

You can set that even in .aspx:

(you'll have to modify Where condition based on param)

<asp:LinqDataSource runat="server" ...>
    ...
    <WhereParameters>
        <asp:ControlParameter ControlID="rdl_filter_date" Name="Param" PropertyName="SelectedValue" />
    </WhereParameters>
</asp:LinqDataSource>

Second option - use codebehind to set it all. Markup:

<asp:LinqDataSource ID="lds" runat="server" OnSelecting="lds_Selecting" />

Codebehind:

protected void lds_Selecting(object sender, LinqDataSourceSelectEventArgs e) {
  var query = ...; // make your query here
  e.Result = query;
}    
于 2013-06-24T11:20:17.780 回答