Quantcast
Channel: ASP.NET AJAX + Ajax Control Toolkit (ACT)
Viewing all articles
Browse latest Browse all 5678

Gridview with Multiple Search Criteria - Only Search for Criteria that isn't Blank

$
0
0

I have a webpage with a Gridview.  At the bottom of the page, I have several search fields.  The first one is Reason which has a dropdownlist that comes from a SQL table.  The second and third fields are for to and from dates that have calendar extenders.  I would like the ability to search either by Reason, To and From Dates, or both Reason and To and From Dates.  Right now, I have to enter the Reason, and the to and from dates before it will search.  It searches when I add the last variable - I do not have a button.  Here is my code:

<%@ Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/site.master" CodeBehind="TimeOffAllByUser.aspx.vb" Inherits="timework.TimeOffAllByUser" %><%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %><asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server"><style type="text/css">
        .style1
        {
            font-family: "Segoe UI";
            font-weight: bold;
            font-size: medium;
        }</style></asp:Content><asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"><asp:GridView ID="GridView1" runat="server" 
        DataSourceID="SqlDataSource1" AutoGenerateColumns="False" CellPadding="4" 
        ForeColor="#333333" GridLines="None" style="margin-right: 0px" 
        AllowPaging="True" AllowSorting="True" Height="419px"><AlternatingRowStyle BackColor="White" ForeColor="#284775" /><Columns><asp:BoundField DataField="EmployeeName" HeaderText="Name" 
                ItemStyle-Width="150px"
                SortExpression="EmployeeName" /><asp:BoundField DataField="BeginDateOff" HeaderText="Begin Date" 
                DataFormatString="{0:d}" 
                SortExpression="BeginDateOff" /><asp:BoundField DataField="EndDateOff" HeaderText="End Date" 
                DataFormatString="{0:d}" 
                SortExpression="EndDateOff" /><asp:CheckBoxField DataField="AllDay_YesNo" HeaderText="All Day?" 
                ItemStyle-HorizontalAlign="Center"
                SortExpression="AllDay_YesNo" /><asp:BoundField DataField="BeginTimeOff" HeaderText="Begin Time"
                 DataFormatString="{0:t}" 
                SortExpression="BeginTimeOff" /><asp:BoundField DataField="EndTimeOff" HeaderText="End Time" 
                DataFormatString="{0:t}" 
                SortExpression="EndTimeOff" /><asp:CheckBoxField DataField="Approved" HeaderText="Approved" 
                ItemStyle-HorizontalAlign="Center"
                SortExpression="Approved" /><asp:BoundField DataField="Description" HeaderText="Reason" 
                 ItemStyle-Width="150px"
                SortExpression="Description" /></Columns><EditRowStyle BackColor="#999999" /><FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /><HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /><PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" /><RowStyle BackColor="#F7F6F3" ForeColor="#333333" /><SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" /><SortedAscendingCellStyle BackColor="#E9E7E2" /><SortedAscendingHeaderStyle BackColor="#506C8C" /><SortedDescendingCellStyle BackColor="#FFFDF8" /><SortedDescendingHeaderStyle BackColor="#6F8DAE" /></asp:GridView><asp:Panel ID="Panel1" runat="server" BorderColor="#666666" BorderStyle="Solid" Width="802px"><span class="style1"><br /> &nbsp; Search for:</span><br /><br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Time Off Reason:  <asp:DropDownList ID="fltrdesc" runat="server"  
            Height="26px" Width="135px" style="text-align: center" 
            DataSourceID="SqlDataSource2" DataTextField="Description" 
            DataValueField="Description" AutoPostBack="True" ></asp:DropDownList><asp:SqlDataSource ID="SqlDataSource2" runat="server" 
            ConnectionString="<%$ ConnectionStrings:TimeSQLConnectionString1 %>" 
            SelectCommand="SELECT [Description] FROM [tblCodesWork] ORDER BY [Description]"></asp:SqlDataSource><br /><asp:ToolkitScriptManager ID="toolkitScriptManager" runat="server" /><br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Time Off Between: &nbsp;<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True" Height="25px" 
            Width="110px"></asp:TextBox><ajaxToolkit:CalendarExtender ID="CalendarExtender1" runat="server" 
            Enabled="True" TargetControlID="TextBox1"/>&nbsp;<asp:TextBox ID="TextBox2" runat="server" AutoPostBack="True" Height="25px" 
            Width="110px"></asp:TextBox><ajaxToolkit:CalendarExtender ID="CalendarExtender2" runat="server" 
             Enabled="True" TargetControlID="TextBox2"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<asp:Button ID="btnClear" runat="server" onclick="btnClear_Click" Text="Clear" /><asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:TimeSQLConnectionString1 %>" 
            SelectCommand="SELECT tblWorkHours.EmployeeName, tblWorkHours.BeginDateOff, tblWorkHours.EndDateOff, tblWorkHours.AllDay_YesNo, tblWorkHours.BeginTimeOff, tblWorkHours.EndTimeOff, tblWorkHours.Approved, tblCodesWork.Description, tblEmployees.Login FROM tblWorkHours INNER JOIN tblCodesWork ON tblWorkHours.WorkCode = tblCodesWork.WorkCodeID INNER JOIN tblEmployees ON tblWorkHours.Employee = tblEmployees.EmployeeID WHERE (tblEmployees.Login = @username) ORDER BY tblWorkHours.BeginDateOff DESC" 
            OnSelecting="SqlDataSource1_Selecting"
            FilterExpression="Description='{0}' and BeginDateOff >= '{1}' AND EndDateOff <= '{2}'"><SelectParameters><asp:QueryStringParameter Name="username"  /></SelectParameters><FilterParameters><asp:ControlParameter ControlId="fltrdesc" Name="Description" PropertyName="Text" Type="String" /><asp:ControlParameter ControlID="TextBox1" Name="BeginDateOff" PropertyName="Text" Type="String" /><asp:ControlParameter ControlID="TextBox2" Name="enddateOff" PropertyName="Text" Type="String" /></FilterParameters></asp:SqlDataSource><br /><br /></asp:Panel><br /><br /></asp:Content>

And here is my VB code:

Imports System
Imports System.Data
Imports System.Data.OleDb
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Web.Mvc
Public Class TimeOffAllByUser
    Inherits System.Web.UI.Page

    Protected Sub SqlDataSource1_Selecting(ByVal sender As Object, ByVal e As SqlDataSourceSelectingEventArgs)

        Dim vusername() As String
        vusername = Split(HttpContext.Current.User.Identity.Name, "\", , CompareMethod.Text)
        e.Command.Parameters("@username").Value = vusername(1)

    End Sub

    Protected Sub btnClear_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnClear.Click

        'Clear Description (Reason for Time Off) field

        SqlDataSource1.FilterExpression = ""
        SqlDataSource1.DataBind()
        'Redirect to same page again
        Response.Redirect(HttpContext.Current.Request.Url.ToString(), True)

    End Sub

End Class

Thanks in advance for your help!


Viewing all articles
Browse latest Browse all 5678

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>