所以,我有一个由sqldatasource填充的gridview,在它的一侧我有一个过滤器列表。其中一项是下拉菜单。当页面首次加载并选择一个项目时会发生什么。过滤器有效,但如果我尝试更改为其他内容则不会更改。我调试了它,当我在索引上调用bind时,它会通过sql onselecting方法。
我尝试过建议在索引更改时设置过滤器表达式,然后调用数据绑定(删除onselecting函数),但仍然无效。
修改 这是一些请求的标记/代码
的DataSource:
<asp:SqlDataSource ID="datasource" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>" OnSelecting="ApplyFilter"
SelectCommand="SELECT [CustomerId], [CustomerCancelationStatus], [CustomerGroup], [CustomerFirstName], [CustomerLastName], [CustomerFullName], [CompanyName], [CustomerAddress], [CustomerCity], [CustomerState], [CustomerZipcode], [CustomerFullAddress], [CustomerEmail], [CustomerEmail2], [CustomerPhoneNumber], [CustomerPhoneNumberExt], [CustomerType], [CustomerSubscriptionType], [CustomerCost], [CustomerPaymentMethod], [CustomerSignUpDate], [CustomerPickUpDay], [CustomerPickUpDay2], [CustomerNotes], [CustomerDriver], [HowHeard], [NewBinsRequested] FROM [Customers] WHERE ([CustomerStatus] <> @CustomerStatus) ORDER BY CustomerFirstName ASC">
<SelectParameters>
<asp:Parameter DefaultValue="Inactive" Name="CustomerStatus" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
下拉:
<asp:DropDownList ID="ddlDriver" runat="server" AutoPostBack="True"
DataSourceID="driverDataSource" DataTextField="DriverUsername"
DataValueField="DriverUsername" AppendDataBoundItems="True"
onselectedindexchanged="ddlDriver_OnSelectedIndexChanged"
Height="21px" style="position: relative; top: 2px; left: 3px" Width="150px">
<asp:ListItem>Select...</asp:ListItem>
</asp:DropDownList>
网格:
<asp:GridView ID="CustomersGridView" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" BackColor="White"
BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3"
DataKeyNames="CustomerId" DataSourceID="SqlDataSource1" ForeColor="Black"
GridLines="Vertical"
style="z-index: 1; left: 193px; top: -486px; position: relative; height: 35px; width: 600px; text-align: center;"
PageSize="500" ShowHeaderWhenEmpty="True" OnPageIndexChanged="CustomersGridView_PageIndexChanged">
<AlternatingRowStyle BackColor="#CCCCCC" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="CustomerSelectionCheckBox" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="CustomerId" HeaderText="ID" InsertVisible="False"
ReadOnly="True" SortExpression="CustomerId" />
<asp:BoundField DataField="CustomerFullName" HeaderText="Name" SortExpression="CustomerFullName"></asp:BoundField>
<asp:BoundField DataField="CustomerFullAddress" HeaderText="Address" SortExpression="CustomerFullAddress" />
<asp:BoundField DataField="CustomerPickUpDay" HeaderText="Pick-Up Day" SortExpression="CustomerPickUpDay"></asp:BoundField>
<asp:BoundField DataField="CustomerGroup" HeaderText="Group" SortExpression="CustomerGroup" />
<asp:BoundField DataField="CustomerSignUpDate" HeaderText="Sign-Up Date" SortExpression="CustomerSignUpDate" DataFormatString="{0:d}"></asp:BoundField>
<asp:BoundField DataField="CustomerSubscriptionType" HeaderText="Subscription" SortExpression="CustomerSubscriptionType" Visible="False" />
<asp:BoundField DataField="CustomerCost" HeaderText="Cost" SortExpression="CustomerCost" Visible="False" />
<asp:BoundField DataField="CustomerDriver" HeaderText="Driver" SortExpression="CustomerDriver" Visible="False" />
<asp:BoundField DataField="HowHeard" HeaderText="How Heard" SortExpression="HowHeard" Visible="False" />
<asp:BoundField DataField="NewBinsRequested" HeaderText="Bins Requested" SortExpression="NewBinsRequested" Visible="False" />
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:Button ID="EditButton" runat="server" CausesValidation="false" CommandName="" OnClick="EditButton_Click" Text="Edit" CommandArgument='<%# Eval("CustomerID") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#CCCCCC" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#808080" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#383838" />
</asp:GridView>
ApplyFilter(onSelecting)[还有更多项目,但这是相关的]:
if (ddlDriver.Text != "Select...")
{
if (!string.IsNullOrEmpty(SqlDataSource1.FilterExpression))
{
SqlDataSource1.FilterExpression += " AND [CustomerDriver]='{0}'";
SqlDataSource1.FilterParameters.Add("@CustomerDriver", ddlDriver.Text);
}
else
{
SqlDataSource1.FilterExpression = "[CustomerDriver]='{0}'";
SqlDataSource1.FilterParameters.Add("@CustomerDriver", ddlDriver.Text);
}
}
if (!String.IsNullOrEmpty(SqlDataSource1.FilterExpression))
{
SqlDataSource1.FilterExpression +=
" AND [CustomerStatus] <> 'Inactive' AND [CustomerStatus] <> 'Canceled'";
}
OnSelectedIndexChanged用于下拉列表:
protected void ddlDriver_OnSelectedIndexChanged(object sender, EventArgs e)
{
CustomersGridView.DataBind();
}
哦,这是在页面加载,不确定是否重要:
if (Page.IsPostBack)
{
if (SearchTextBox.Text.Length > 0)
{
var searchtext = SearchTextBox.Text + "*";
//var ctrlParam = new ControlParameter
// {
// ControlID = "SearchTextBox",
// PropertyName = "Text",
// Name = "Search"
// };
var ctrlParam2 = new QueryStringParameter
{
DefaultValue = "Canceled",
QueryStringField = "Canceled",
Name = "CustomerStatus",
Type = TypeCode.String
};
var ctrlParam3 = new QueryStringParameter
{
DefaultValue = "Confirmed",
QueryStringField = "Confirmed",
Name = "CustomerCancelationStatus",
Type = TypeCode.String
};
SqlDataSource1.SelectParameters.Clear();
//SqlDataSource1.SelectParameters.Add(ctrlParam);
SqlDataSource1.SelectParameters.Add("Search", TypeCode.String, searchtext);
SqlDataSource1.SelectParameters.Add(ctrlParam2);
SqlDataSource1.SelectParameters.Add(ctrlParam3);
SqlDataSource1.SelectCommand = "SELECT FullText_for_Customers.RANK, [CustomerId], [CustomerStatus], [CustomerGroup], [CustomerFirstName], [CustomerLastName], [CustomerFullName], [CompanyName], [CustomerAddress], [CustomerCity], [CustomerState], [CustomerZipcode], [CustomerFullAddress], [CustomerEmail], [CustomerEmail2], [CustomerPhoneNumber], [CustomerPhoneNumberExt], [CustomerType], [CustomerSubscriptionType], [CustomerCost], [CustomerPaymentMethod], [CustomerPickUpDay], [CustomerPickUpDay2], [CustomerSignUpDate], [CustomerNotes], [CustomerDriver] FROM [Customers] INNER JOIN CONTAINSTABLE(Customers, (CustomerFirstName, CustomerLastName, CustomerFullName, CompanyName, CustomerAddress, CustomerCity, CustomerState, CustomerZipcode, CustomerFullAddress, CustomerEmail, CustomerEmail2, CustomerPhoneNumber, CustomerNotes), @Search) as FullText_for_Customers ON FullText_for_Customers.[KEY]=Customers.CustomerId WHERE ([CustomerStatus] <> @CustomerStatus) AND ([CustomerCancelationStatus] <> @CustomerCancelationStatus) ORDER BY CustomerFirstName ASC";
}
else
{
var ctrlParam2 = new QueryStringParameter
{
DefaultValue = "Canceled",
QueryStringField = "Canceled",
Name = "CustomerStatus",
Type = TypeCode.String
};
var ctrlParam3 = new QueryStringParameter
{
DefaultValue = "Inactive",
QueryStringField = "Inactive",
Name = "CustomerStatus2",
Type = TypeCode.String
};
SqlDataSource1.SelectParameters.Clear();
SqlDataSource1.SelectParameters.Add(ctrlParam2);
SqlDataSource1.SelectParameters.Add(ctrlParam3);
SqlDataSource1.SelectCommand = "SELECT [CustomerId], [CustomerStatus], [CustomerGroup], [CustomerFirstName], [CustomerLastName], [CustomerFullName], [CompanyName], [CustomerAddress], [CustomerCity], [CustomerState], [CustomerZipcode], [CustomerFullAddress], [CustomerEmail], [CustomerEmail2], [CustomerPhoneNumber], [CustomerPhoneNumberExt], [CustomerType], [CustomerSubscriptionType], [CustomerCost], [CustomerPaymentMethod], [CustomerPickUpDay], [CustomerPickUpDay2], [CustomerNotes], [CustomerDriver], [CustomerSignUpDate], [HowHeard], [NewBinsRequested] FROM [Customers] WHERE ([CustomerStatus] <> @CustomerStatus AND [CustomerStatus] <> @CustomerStatus2) ORDER BY CustomerFirstName ASC";
}
}