I have what I hope is a simple question. I have a gridview control that's bound to a sqldatasource.
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:
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:
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. 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. 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.
UPDATE #2: I've replaced the select parameters with the following:
<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:
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
The issue was that the gridviews have a property: CancelSelectOnNullParameter, which is true by default. 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.
(Semi-related note) I also added the following to the ControlParameters: ConvertEmptyStringToNull="true"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.