按日期和项目名称搜索

时间:2015-10-09 02:37:23

标签: c# asp.net gridview

我需要按日期搜索gridview,而Project Name是可选的,用户可以将其留空。但是我已经完成的代码,用户必须键入项目名称。如何使项目名称可选?提前谢谢。

<div class="form-inline">Project Name:
    <asp:TextBox ID="Pname"
                 runat="server"
                 CssClass="form-control"
                 Width="230px"></asp:TextBox> From:
    <asp:TextBox ID="FromDate"
                 runat="server"
                 CssClass="form-control"
                 Width="230px"></asp:TextBox>
    <asp:CalendarExtender ID="CalendarExtender1"
                          runat="server"
                          TargetControlID="FromDate"
                          cssclass="MyCalendar"></asp:CalendarExtender> To:
    <asp:TextBox ID="ToDate"
                 runat="server"
                 CssClass="form-control"
                 Width="230px"></asp:TextBox>
    <asp:CalendarExtender ID="CalendarExtender2"
                          runat="server"
                          TargetControlID="ToDate"
                          cssclass="MyCalendar"></asp:CalendarExtender>
    <asp:Button ID="btnsearch"
                runat="server"
                Text="Search"
                cssclass="btn btn-info" />
</div>

的GridView:

<asp:GridView ID="GridView1"
              runat="server"
              AutoGenerateColumns="False"
              DataKeyNames="ReportId"
              DataSourceID="SqlDataSource1"
              AllowPaging="True"
              AllowSorting="True"
              RowStyle-Wrap="true">
    <Columns>
        <asp:BoundField DataField="ReportId"
                        HeaderText="ReportId"
                        InsertVisible="False"
                        ReadOnly="True"
                        SortExpression="ReportId"
                        visible="false" />
        <asp:BoundField DataField="ReportDesc"
                        HeaderText="Report"
                        ReadOnly="True"
                        SortExpression="ReportDesc" />
        <asp:BoundField DataField="ProjectName"
                        HeaderText="Project"
                        SortExpression="ProjectName" />
        <asp:BoundField DataField="ReportDate"
                        HeaderText="Date"
                        SortExpression="ReportDate" />
    </Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1"
                   runat="server"
                   ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
                   SelectCommand="SELECT  * From Report"
                   FilterExpression="[ReportDate] >= '{1}' and [ReportDate] <= '{2}' and [ProjectName]='{0}'">
    <FilterParameters>
        <asp:ControlParameter ControlID="Pname"
                              Name="name"
                              PropertyName="text"
                              Type="String" />
        <asp:ControlParameter ControlID="FromDate"
                              Name="Date"
                              PropertyName="Text"
                              Type="String" />
        <asp:ControlParameter ControlID="ToDate"
                              Name="Date2"
                              PropertyName="Text"
                              Type="String" />
    </FilterParameters>
</asp:SqlDataSource>

1 个答案:

答案 0 :(得分:0)

  1. 在SqlDataSource上设置CancelSelectOnNullParameter =“false”
  2. 为Pname ControlParameter
  3. 设置DefaultValue为NULL