gridview控件不爽快?

时间:2018-04-03 21:03:21

标签: c# asp.net sql-server

我希望这是一个简单的问题。我有一个绑定到sqldatasource的gridview控件。

首先,相关代码:

<asp:DropDownList ID="cboCustomerID" runat="server"
    DataSourceID="DataSourceCustomer" DataTextField="CustomerName" DataValueField="CustomerID">
</asp:DropDownList>
<asp:DropDownList ID="cboStaffID" runat="server"
    DataSourceID="DataSourceStaff" DataTextField="StaffFullName" DataValueField="StaffID">
</asp:DropDownList>
<div><gcctl:MyCheckBox ID="chkShowClosed" Text="Show Closed Jobs?" Checked="false" AutoPostBack="true" runat="server" /></div>
<div><asp:Button ID="btnSearch" runat="server" OnClick="btnSearch_Click" Text="Search" /></div>

<div id="customersearchresults" class="searchresults">
    <asp:SqlDataSource id="gvJobsDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:GeekCommandConnString %>"
        SelectCommand="SELECT j.JobID, j.JobName, s.JobStatus, jal.[JobAssignmentsFullList]
            FROM (SELECT * FROM [dbo].[Jobs] WHERE ((NULLIF(@CustomerSearch,'') IS NOT NULL AND CustomerID = @CustomerSearch) OR (NULLIF(@CustomerSearch,'') IS NULL))) j
            INNER JOIN (SELECT * FROM [list].[JobStatuses] WHERE ((@ShowClosed = 0 AND IsStatusOpen = 1) OR (@ShowClosed = 1)) AND IsActive = 1) s ON j.JobStatusID = s.JobStatusID
            LEFT JOIN (SELECT * FROM [dbo].[JobAssignments] WHERE ((NULLIF(@StaffSearch,'') IS NOT NULL AND StaffID = @StaffSearch) OR (NULLIF(@StaffSearch,'') IS NULL))) ja ON j.JobID = ja.JobID
            LEFT JOIN [dbo].[udv_JobAssignmentsCommaDelimited] jal ON j.JobID = jal.JobID
            ">
        <SelectParameters>
            <asp:Parameter Name="CustomerSearch" Type="Int32" />
            <asp:Parameter Name="StaffSearch" Type="Int32" />
            <asp:Parameter Name="ShowClosed" Type="Boolean" />
        </SelectParameters>
    </asp:SqlDataSource>

    <asp:GridView ID="gvJobs" runat="server" AllowSorting="True"  
        AutoGenerateColumns="False"
        DataKeyNames="JobID"
        DataSourceID="gvJobsDataSource"
        AutoGenerateDeleteButton="False"    
        AutoGenerateEditButton="False" 
        AutoGenerateSelectButton="False"
        CssClass="searchresultsgrid" 
        AllowPaging="True" PageSize="50"
        OnRowCommand="gvJobs_RowCommand"
        EmptyDataText="No matching jobs on record." >
        <Columns>
            <asp:templatefield>
                <itemtemplate>
                    <asp:linkbutton id="btnEdit" runat="server" CommandName="JobEdit" OnClientClick="PageForm.target ='_blank';" text="View/Edit" />
                </itemtemplate>
            </asp:templatefield>

            <asp:BoundField DataField="JobID" HeaderText="ID" InsertVisible="false" ReadOnly="true" Visible="false" SortExpression="JobID" />
            <asp:templateField HeaderText="Job Name" SortExpression="JobName">
                <ItemTemplate><%# Eval("JobName") %></ItemTemplate>
            </asp:templateField>
            <asp:templateField HeaderText="Assigned to" SortExpression="JobAssignmentsFullList">
                <ItemTemplate><%# Eval("JobAssignmentsFullList") %></ItemTemplate>
            </asp:templateField>
        </Columns>
    </asp:GridView>
</div>


<asp:SqlDataSource ID="DataSourceCustomer" runat="server"
    ConnectionString="<%$ ConnectionStrings:GeekCommandConnString %>" 
    SelectCommand="SELECT NULL AS [CustomerID]
                , NULL AS [CustomerName]
            UNION SELECT [CustomerID]
                ,[CustomerName]
            FROM [GeekCommand].[dbo].[Customers]
            WHERE ((@ShowInactive = 0 AND IsActive = 1) OR (@ShowInactive = 1))
            ORDER BY CustomerName">
    <SelectParameters>
        <asp:ControlParameter Name="ShowInactive" Type="Boolean" ControlID="chkCustomersShowInactive" PropertyName="Checked" />
    </SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="DataSourceStaff" runat="server"
    ConnectionString="<%$ ConnectionStrings:GeekCommandConnString %>" 
    SelectCommand="SELECT NULL AS [StaffID]
                , NULL AS [StaffFullName]
            UNION SELECT [StaffID]
                ,COALESCE([FirstName], [Nickname], '') + ' ' + COALESCE([LastName], '') AS StaffFullName
            FROM [GeekCommand].[dbo].[Staff]
            WHERE ((@ShowInactive = 0 AND IsActive = 1) OR (@ShowInactive = 1))
            ORDER BY StaffFullName">
    <SelectParameters>
        <asp:ControlParameter Name="ShowInactive" Type="Boolean" ControlID="chkStaffShowInactive" PropertyName="Checked" />
    </SelectParameters>
</asp:SqlDataSource>

相关的aspx.cs代码:

int? iCustomerID;
int? iStaffID;

//--------
protected void SetIDs()
{
    this.iCustomerID = null;
    this.iStaffID = null;

    if (Request.QueryString["customerid"] != null) //new customer
    {
        try
        {
            this.iCustomerID = Convert.ToInt32(Request.QueryString["customerid"]);
        }
        catch { }
    }

    if (Request.QueryString["staffid"] != null) //new customer
    {
        try
        {
            this.iStaffID = Convert.ToInt32(Request.QueryString["staffid"]);
        }
        catch { }
    }

    if (iCustomerID != null)
    {
        cboCustomerID.SelectedValue = iCustomerID.ToString();
    }

    if (iStaffID != null)
    {
        cboStaffID.SelectedValue = iStaffID.ToString();
    }
}

//--------
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        SetIDs();
    }
}

//--------
protected void btnSearch_Click(object sender, EventArgs e)
{
    gvJobsDataSource.SelectParameters["CustomerSearch"].DefaultValue = cboCustomerID.SelectedValue.ToString();
    gvJobsDataSource.SelectParameters["StaffSearch"].DefaultValue = cboStaffID.SelectedValue.ToString();
    gvJobsDataSource.SelectParameters["ShowClosed"].DefaultValue = chkShowClosed.Checked.ToString();

    gvJobs.DataBind();
}

当我在SSMS中运行以下内容时,我得到了2行,我应该这样做:

DECLARE @CustomerSearch int, @StaffSearch int, @ShowClosed bit
SELECT @CustomerSearch = 2331, @StaffSearch = '', @ShowClosed = CAST(0 AS bit)

SELECT j.JobID, j.JobName, s.JobStatus, jal.[JobAssignmentsFullList]
FROM (SELECT * FROM [dbo].[Jobs] WHERE ((NULLIF(@CustomerSearch,'') IS NOT NULL AND CustomerID = @CustomerSearch) OR (NULLIF(@CustomerSearch,'') IS NULL))) j
INNER JOIN (SELECT * FROM [list].[JobStatuses] WHERE ((@ShowClosed = 0 AND IsStatusOpen = 1) OR (@ShowClosed = 1)) AND IsActive = 1) s ON j.JobStatusID = s.JobStatusID
LEFT JOIN (SELECT * FROM [dbo].[JobAssignments] WHERE ((NULLIF(@StaffSearch,'') IS NOT NULL AND StaffID = @StaffSearch) OR (NULLIF(@StaffSearch,'') IS NULL))) ja ON j.JobID = ja.JobID
LEFT JOIN [dbo].[udv_JobAssignmentsCommaDelimited] jal ON j.JobID = jal.JobID

但是当我在调试模式下选择一个客户(特别是id为2331的客户)并逐步执行btnSearch_Click代码时,cboCustomerID.SelectedValue =“2331”,cboStaffID.SelectedValue =“”,chkShowClosed.Checked = false(全部其中是正确的)...但是当我跳过databind命令时没有任何反应。 gridview继续显示“记录中没有匹配的作业。”

我觉得我错过了一些非常明显的东西,但我不能为我的生活找出它是什么。

更新:好的。这是有趣的。显然,查询永远不会被发送到SQL Server。我刚刚以跟踪模式启动SQL Server,重新加载了aspx页面并进行了搜索,虽然日志中有两个下拉列表后面的查询,但gridview后面的查询却不存在。

更新#2:我已使用以下内容替换了选择参数:

<asp:ControlParameter Name="CustomerSearch" ControlID="cboCustomerID" PropertyName ="SelectedValue" />
<asp:ControlParameter Name="StaffSearch" ControlID="cboStaffID" PropertyName ="SelectedValue" />
<asp:ControlParameter Name="ShowClosed" Type="Boolean" ControlID="chkShowClosed" PropertyName="Checked" />

...并删除了btnSearch_Click事件中的额外代码,因此该代码中唯一的一行是:

protected void btnSearch_Click(object sender, EventArgs e)
{
    gvJobs.DataBind();
}

......没有变化。单击搜索按钮时仍然没有任何反应。

1 个答案:

答案 0 :(得分:0)

耶!找到答案: https://forums.asp.net/t/1243253.aspx?Gridview+Databind+Not+Working

问题是gridviews有一个属性:CancelSelectOnNullParameter,默认情况下为true。由于在我进行搜索时,至少有一个下拉列表几乎总是为空,因此当我点击搜索按钮时没有发生任何事情。当我将CancelSelectOnNullParameter =“false”添加到gridview控件时,它修复了问题。

(半相关说明)我还在ControlParameters中添加了以下内容:ConvertEmptyStringToNull =“true”