我有一個從四個表填充的gridview,並且select命令(如下所示)功能齊全。我希望能夠搜索gridview,並只顯示「狀態」列值等同於下拉列表中所選項目的那些行。我相信我有這個選擇語句(下面也列出),但我只希望gridview在按鈕單擊時顯示搜索結果。我如何使這個select語句有條件?換句話說,我該如何使搜索選擇語句僅在用戶單擊搜索按鈕時使用?使用dropdownlist所選項目來搜索gridview?
定期select語句:
SelectCommand="SELECT Customer.SubId, Customer.CustName, Customer.CustCity, Customer.CustState, Broker.BroName, Broker.BroState, Broker.EntityType, Submission.Coverage, Status.Status FROM Submission INNER JOIN Broker ON Broker.SubId = Submission.SubmissionId INNER JOIN Customer ON Customer.SubId = Submission.SubmissionId INNER JOIN Status ON Status.StatusId = Submission.StatusId"
我搜索select語句:
`SelectCommand="SELECT Customer.SubId, Customer.CustName, Customer.CustCity, Customer.CustState, Broker.BroName, Broker.BroState,
Broker.EntityType, Submission.Coverage, Status.Status FROM Submission WHERE Status = '" + Ddl.SelectedItem.Text + "' INNER JOIN Broker ON Broker.SubId = Submission.SubmissionId
INNER JOIN Customer ON Customer.SubId = Submission.SubmissionId INNER JOIN Status ON Status.StatusId = Submission.StatusId">`
完整的GridView/droplist代碼:
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSource2" DataTextField="Status" DataValueField="Status">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
SelectCommand="SELECT [Status] FROM [Status]"></asp:SqlDataSource>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Search" />
</asp:Panel>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1"
EmptyDataText="There are no data records to display." AllowPaging="True"
BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px"
CellPadding="3" GridLines="Vertical" HorizontalAlign="Center"
AllowSorting="True" >
<AlternatingRowStyle BackColor="#DCDCDC" />
<Columns>
<asp:BoundField DataField="SubId" HeaderText="Submission Id"
SortExpression="SubId" >
</asp:BoundField>
<asp:BoundField DataField="CustName" HeaderText="Customer"
SortExpression="CustName" />
<asp:BoundField DataField="CustCity" HeaderText="Customer City"
SortExpression="CustCity" />
<asp:BoundField DataField="CustState" HeaderText="Customer State"
SortExpression="CustState" />
<asp:BoundField DataField="BroName" HeaderText="Broker"
SortExpression="BroName" />
<asp:BoundField DataField="BroState" HeaderText="Broker State"
SortExpression="BroState" />
<asp:BoundField DataField="EntityType" HeaderText="Entity Type"
SortExpression="EntityType" />
<asp:BoundField DataField="Coverage"
HeaderText="Coverage" SortExpression="Coverage" />
<asp:BoundField DataField="Status" HeaderText="Status"
SortExpression="Status" />
<asp:HyperLinkField DataNavigateUrlFields="SubId"
DataNavigateUrlFormatString="View.aspx?SubId={0}" Text="View" />
<asp:HyperLinkField DataNavigateUrlFields="SubId"
DataNavigateUrlFormatString="ViewEdit.aspx?SubId={0}" HeaderText="Edit"
Text="Edit" />
</Columns>
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
<HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
<SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#0000A9" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#000065" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
ProviderName="<%$ ConnectionStrings:ProductInstanceString.ProviderName %>"
SelectCommand="SELECT Customer.SubId, Customer.CustName, Customer.CustCity, Customer.CustState, Broker.BroName, Broker.BroState, Broker.EntityType, Submission.Coverage, Status.Status FROM Submission INNER JOIN Broker ON Broker.SubId = Submission.SubmissionId INNER JOIN Customer ON Customer.SubId = Submission.SubmissionId INNER JOIN Status ON Status.StatusId = Submission.StatusId">
</asp:SqlDataSource>