简体   繁体   English

gridview控件不刷新?

[英]gridview control not refreshing?

I have what I hope is a simple question. 我希望有一个简单的问题。 I have a gridview control that's bound to a sqldatasource. 我有一个绑定到sqldatasource的gridview控件。

First, the relevant code: 一,相关代码:

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

And the relevant aspx.cs code: 以及相关的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();
}

When I run the following in SSMS, I get 2 rows back, as I should: 当我在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

But when I select a customer (specifically the customer whose id is 2331) in debug mode and step through the btnSearch_Click code, cboCustomerID.SelectedValue = "2331", cboStaffID.SelectedValue = "", chkShowClosed.Checked = false (all of which is correct)... but nothing happens when I step past the databind command. 但是,当我在调试模式下选择一个客户(特别是ID为2331的客户)并逐步检查btnSearch_Click代码时,cboCustomerID.SelectedValue =“ 2331”,cboStaffID.SelectedValue =“”,chkShowClosed.Checked = false(所有这些都是正确)...但是当我通过databind命令时什么也没有发生。 The gridview continues to show "No matching jobs on record." 网格视图继续显示“记录中没有匹配的作业”。

I feel like I'm missing something really obvious, but I can't for the life of me figure out what it is. 我觉得自己缺少真正明显的东西,但是我无法终生弄清楚它是什么。

UPDATE: Ok. 更新:好的。 This is interesting. 这是有趣的。 Apparently, the query never gets sent to the SQL Server. 显然,查询永远不会发送到SQL Server。 I just started up SQL Server in trace mode, reloaded the aspx page, and did a search, and while the queries that are behind the two dropdownlists are there in the log, the query that's behind the gridview is just not there. 我刚刚以跟踪模式启动SQL Server,重新加载了aspx页面,然后进行了搜索,虽然两个下拉列表后面的查询都在日志中,但是gridview后面的查询却不存在。

UPDATE #2: I've replaced the select parameters with the following: 更新2:我已将select参数替换为以下内容:

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

...and removed the extra code in the btnSearch_Click event, so the only line in that code is: ...并删除了btnSearch_Click事件中的多余代码,因此该代码中的唯一一行是:

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

...no change. ...没变。 Still nothing happens when I click the search button. 当我单击搜索按钮时,仍然没有任何反应。

Yay! 好极了! Found the answer: https://forums.asp.net/t/1243253.aspx?Gridview+Databind+Not+Working 找到了答案: https : //forums.asp.net/t/1243253.aspx?Gridview+Databind+Not+Working

The issue was that the gridviews have a property: CancelSelectOnNullParameter, which is true by default. 问题是gridviews具有属性:CancelSelectOnNullParameter,默认情况下为true。 Since at least one of the drop down lists is almost always null when I do the search, this resulted in nothing happening when I hit the search button. 由于在执行搜索时,至少有一个下拉列表几乎总是为空,因此当我按下搜索按钮时,什么也没有发生。 When I added CancelSelectOnNullParameter="false" to the gridview control, it fixed the problem. 当我将CancelSelectOnNullParameter =“ false”添加到gridview控件时,它解决了该问题。

(Semi-related note) I also added the following to the ControlParameters: ConvertEmptyStringToNull="true" (半相关的注释)我还向ControlParameters中添加了以下内容:ConvertEmptyStringToNull =“ true”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM