2011-08-21 159 views
2

我想設置GridView來過濾屬於特定員工的憑證。下拉列表提供員工列表,一旦選中,我希望gridview只填充屬於特定員工的條目。ASP.net DropDownList填充GridView

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent"> 
</asp:Content> 
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent"> 
    <asp:DropDownList ID="DropDownListEmployee" runat="server" 
         AutoPostBack="True" 
         OnSelectedIndexChanged="SelectionHasChanged" 
         DataSourceID="SqlDataSource2" DataTextField="Fullname" 
         DataValueField="Employee_ID" AppendDataBoundItems="true" 
         Width="214px"> 
     <asp:ListItem>Select</asp:ListItem> 
    </asp:DropDownList> 
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
         ConnectionString="<%$ ConnectionStrings:DBConnectionString %>" 
         SelectCommand="SELECT Employee.F_Name + ' ' + Employee.L_Name AS Fullname, Employee.Primary_Address, Employee.Primary_Phone, Employee.E_mail, Credentials.Degree, Credentials.Years_Experience, Credentials.Certifications, Credentials.Positions, Employee.Employee_ID FROM Employee INNER JOIN Credentials ON Employee.Employee_ID = Credentials.Employee_ID"></asp:SqlDataSource> 
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataSourceID="SqlDataSource2" DataKeyNames="Employee_ID"> 
     <Columns> 
      <asp:BoundField DataField="Fullname" HeaderText="Fullname" 
          ReadOnly="True" SortExpression="Fullname" /> 
      <asp:BoundField DataField="Primary_Address" HeaderText="Primary_Address" 
          SortExpression="Primary_Address" /> 
      <asp:BoundField DataField="Primary_Phone" HeaderText="Primary_Phone" 
          SortExpression="Primary_Phone" /> 
      <asp:BoundField DataField="E_mail" HeaderText="E_mail" 
          SortExpression="E_mail" /> 
      <asp:BoundField DataField="Degree" HeaderText="Degree" 
          SortExpression="Degree" /> 
      <asp:BoundField DataField="Years_Experience" HeaderText="Years_Experience" 
          SortExpression="Years_Experience" /> 
      <asp:BoundField DataField="Certifications" HeaderText="Certifications" 
          SortExpression="Certifications" /> 
      <asp:BoundField DataField="Positions" HeaderText="Positions" 
          SortExpression="Positions" /> 
      <asp:BoundField DataField="Employee_ID" HeaderText="Employee_ID" 
          InsertVisible="False" ReadOnly="True" 
          SortExpression="Employee_ID" /> 
     </Columns> 
    </asp:GridView> 
</asp:Content> 
+0

聽起來像功課 – jzm

回答

1

你沒有說你當前的代碼是什麼一切發生的時候,但我敢打賭,當你選擇一個員工,重新加載頁面,你會得到所有員工的列表中你的數據庫,對不對?

如果這是真的,原因是相同的SqlDataSource綁定到您的DropDownList和您的GridView,並且SqlDataSource的Select命令檢索所有員工 - 沒有WHERE標準來選擇所需的員工。

我會使用2個SqlDataSources - 一個用於DropDownList,另一個用於GridView。第二個SqlDataSource將有一個Select命令來根據DropDownList中的選擇獲取所需員工的信息。

您可以通過修改SqlDataSource2的SelectCommand只返回全名和僱員字段,你不需要爲你的DropDownList休息:

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent"> 
    <asp:DropDownList ID="DropDownListEmployee" runat="server" 
         AutoPostBack="True" 
         OnSelectedIndexChanged="SelectionHasChanged" 
         DataSourceID="SqlDataSource2" DataTextField="Fullname" 
         DataValueField="Employee_ID" AppendDataBoundItems="true" 
         Width="214px"> 
     <asp:ListItem>Select</asp:ListItem> 
    </asp:DropDownList> 
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
         ConnectionString="<%$ ConnectionStrings:DBConnectionString %>" 
         SelectCommand="SELECT F_Name + ' ' + L_Name AS Fullname, Employee_ID FROM Employee"></asp:SqlDataSource> 

在你的第二個SqlDataSource的,你需要添加一個參數(EmployeeID)添加到SelectParameters集合中,並更新您的SelectCommand以獲取參數。

<asp:SqlDataSource ID="gvDataSource" runat="server" 
         ConnectionString="<%$ ConnectionStrings:DBConnectionString %>" 
         SelectCommand="SELECT Employee.F_Name + ' ' + Employee.L_Name AS Fullname, Employee.Primary_Address, Employee.Primary_Phone, Employee.E_mail, Credentials.Degree, Credentials.Years_Experience, Credentials.Certifications, Credentials.Positions, Employee.Employee_ID FROM Employee INNER JOIN Credentials ON Employee.Employee_ID = Credentials.Employee_ID WHERE Employee.EmployeeID = IsNull(@EmployeeID, EmployeeID)"> 
     <SelectParameters> 
      <asp:ControlParameter ControlID="DropDownListID" 
       ConvertEmptyStringToNull="true" Name="EmployeeID" 
       PropertyName="SelectedValue" /> 
     </SelectParameters> 

然後分配此的SqlDataSource到GridView而不是第一個。

請注意,SqlDataSource沒有對提交到參數中的值進行驗證 - 這可能是安全威脅。

SqlDataSource.Select Method

SqlDataSource.SelectParameters Property

+0

感謝您的幫助。很抱歉打擾你,當我嘗試這個時,我不斷遇到一個錯誤:「DataBinding:'System.Data.DataRowView'不包含名稱爲'Employee_ID'的屬性。」我猜這是因爲我的GridView中沒有名爲Employee_ID的列? – nick

+0

@nick - 你在你發佈的標記。不過,它被設置爲InsertVisible false。你是試圖做一個插入,還是隻是加載員工的信息?嘗試刪除InsertVisible =「false」並查看會發生什麼(我明白爲什麼InsertVisible =「false」,只是想知道底層問題是什麼 - 當您是時,您會想保留它,所以最有可能的是您的EmployeeID數據庫是一個標識列)。 – Tim

+0

得到它的工作,我不得不刪除:「選擇」,這是用來指導用戶做出選擇,我也必須改變datasource1查詢爲:「SELECT Employee_ID AS全名FROM員工「或我會得到錯誤:」將nvarchar值'Joe'轉換爲數據類型int時轉換失敗。「現在留下一個數字列表(ID)。 〜感謝所有的幫助,非常感謝! – nick