2011-05-18 36 views
1

我有一個從四個表填充的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> 
     &nbsp;<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> 
     &nbsp; 
     <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> 

回答

1

爲了展示什麼對我有用,這裏是我的代碼。它的功能正常。關於@ashelvey的回答,雖然我沒有測試過他的方法,但他對於參數化是正確的。我沒有的唯一原因是我正在進行一項培訓項目,我的經理特意告訴我現在要避開它。該網站不適合部署。無論如何,我最初的問題是「如何更改按鈕單擊的選擇命令?」答案如下:

protected void BtnStatusSearch_Click(object sender, EventArgs e) 
{ 
    SqlDataSource1.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 WHERE Status.Status = '" + DdlStatus.SelectedItem.Text + "'"; 
    SqlDataSource1.DataBind(); 
} 
1

首先,你一定要使用參數與您的SQL。你發佈的例子很容易受到SQL注入攻擊。其次,你可以通過使用存儲過程來實現一種類型的查詢或另一種類型的查詢(它也有很多其他的好處)。在該過程中,您可以檢查將要傳入的狀態參數是否爲null。如果它爲空,則使用常規,如果它不使用搜索語句。

在你的數據庫,你可以創建一個存儲過程,它看起來是這樣的(也可能是更優雅,但是這是一個簡單的例子):

CREATE PROCEDURE [dbo].[GetStuffByStatus] 

@status varchar(255) = null 

AS 
BEGIN 

IF @status IS NOT NULL 
BEGIN 

    SELECT Customer.SubId, Customer.CustName, Customer.CustCity, Customer.CustState, Broker.BroName, Broker.BroState, 

    Broker.EntityType, Submission.Coverage, Status.Status 

    FROM Submission 

    WHERE Status = @status 

    INNER JOIN Broker ON Broker.SubId = Submission.SubmissionId 

    INNER JOIN Customer ON Customer.SubId = Submission.SubmissionId INNER JOIN Status ON Status.StatusId = Submission.StatusId 

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

END 

然後你就可以適當地編輯您的SqlDataSource:

<asp:SqlDataSource ID="SqlDataSource2" runat="server" 
     ConnectionString="<%$ ConnectionStrings:MyConnectionString %>" 
     SelectCommand="GetStuffByStatus" SelectCommandType="StoredProcedure"> 
     <SelectParameters> 
      <asp:ControlParameter Name="status" ControlID="DtopDownList1" PropertyName="SelectedValue" ConvertEmptyStringToNull="true" /> 
     </SelectParameters> 
</asp:SqlDataSource> 

然後,如果您不想按狀態篩選,請確保沒有任何內容通過此參數傳遞(DropDownList SelectedValue爲空)。默認情況下,空值添加到您的DropDownList:

<asp:DropDownList ID="DtopDownList1" runat="server" DataSourceID="SqlDataSource1" DataTextField="someidentifier" DataValueField="someidentifier" AppendDataBoundItems="true"> 
    <asp:ListItem Text="No filter" Value="" /> 
</asp:DropDownList> 

我在第一次得到的SqlDataSource用於處理非過濾的第一個(空)的情況下有點麻煩,我不得不一個OnSelecting處理程序添加到的SqlDataSource爲GridView。我添加了這個屬性在SqlDataSource:

OnSelecting="SqlDataSource2_Selecting" 

寫下了處理程序,檢查爲空的情況下,並設置參數的DBNull:

protected void SqlDataSource2_Selecting(object sender, SqlDataSourceSelectingEventArgs e) 
{ 
    if (DtopDownList1.SelectedValue == String.Empty) 
    { 
     ((SqlCommand)e.Command).Parameters["@status"].Value = DBNull.Value; 
    } 
} 

我已經測試了這個解決方案,併爲您描述它的工作原理。

當然,這樣做的最佳做法是編寫自己的數據層,以更恰當地處理此邏輯,但這可能超出了此問題的範圍。

相關問題