SelectedIndexChanging不触发

时间:2019-05-30 13:27:46

标签: asp.net vb.net

我有一个下拉列表,它是从后面的代码填充的,下拉列表下面是一个Gridview控件,我也从后面的代码填充了该控件。我的目标是从下拉列表中选择一个部门,并填充Gridview控件,该部门中的所有员工都会

这是我的代码,用于填充Gridview控件:


Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load

        If Not Me.IsPostBack Then      

            LoadDataGrid()
            FillWithDepartments()

        End If

    End Sub


 Private Sub LoadDataGrid()

        Try
            Using conn As New SqlConnection(ConfigurationManager.ConnectionStrings("###").ConnectionString)

                Using cmd As New SqlCommand("SELECT Departments.ShortDescription AS [Department] ,  CostCentre AS [Cost Centre] , FirstName AS [Name] , LastName AS [Surname] , MedicalResults.EmployeeID AS [ID No]  , EmployeeCodes.Code AS [Clock No] ,MedicalResults.[DateTested] AS [Date Tested]  , MedicalResults.[NextDueDate] AS [Next Due Date] , MedicalResults.ECGDate AS [ECG] , MedicalResults.LungFunctionDate AS [Lungfunction] , MedicalResults.AudioGramDate AS [Hearing Test] , MedicalResults.EyeTestDate AS [Eye Test] , OtherProblems AS [Other Problems] , Notes FROM MedicalResults, EmployeeCodes , EmployeevsPositionLink , PositionCodes , Departments WHERE MedicalResults.ID = EmployeeCodes.ID AND EmployeevsPositionLink.EmployeeID = EmployeeCodes.ID AND EmployeevsPositionLink.PositionID = PositionCodes.ID AND PositionCodes.DepartmentCode = Departments.Code AND  EmployeeCodes.TerminationDate Is Null AND EmployeevsPositionLink.PositionNumber = '1' AND   MedicalResults.EmployeeID = EmployeeCodes.EmployeeID Order By DateTested DESC", conn)
                    Dim sda As New SqlDataAdapter(cmd)
                    Dim dt As New DataTable()

                    sda.Fill(dt)
                    GridView1.DataSource = dt
                    GridView1.DataBind()

                End Using

            End Using

        Catch ex As Exception

        End Try

    End Sub


这是用于从后面的代码填充我的下拉列表的代码:

    Protected Sub FillWithDepartments()

        Using conn As New SqlConnection(ConfigurationManager.ConnectionStrings("###").ConnectionString)
            'conn.Open()

            Dim cmd As New SqlCommand("SELECT Code , ShortDescription  FROM Departments", conn)
            Dim adapter As New SqlDataAdapter(cmd)
            Dim tbl As New DataTable()
            adapter.Fill(tbl)
            drplstDepartment.DataSource = tbl
            drplstDepartment.DataTextField = "ShortDescription"
            drplstDepartment.DataValueField = "Code"
            drplstDepartment.DataBind()

            ' conn.Close()

        End Using
    End Sub

下拉列表的前端代码


<asp:DropDownList ID="drplstDepartment" OnSelectedIndexChanged="drplstDepartment_SelectedIndexChanged"  CssClass="form-control mb-2 mr-sm-2" runat="server" AutoPostBack="true">       
    </asp:DropDownList>

前端代码来回网格视图


     <asp:GridView  RowStyle-CssClass="text-center" OnPageIndexChanging="GridView1_PageIndexChanging" OnSelectedIndexChanging="GridView1_SelectedIndexChanging"   AllowPaging="true" class="table table-default table-striped table-bordered table-condensed table-responsive table-hover" border="1" ID="GridView1" runat="server" HeaderStyle-HorizontalAlign="Center" HeaderStyle-VerticalAlign="Middle" HeaderStyle-BackColor="#6C6C6C" HeaderStyle-ForeColor="White"  RowStyle-BackColor="#eeeeee" AlternatingRowStyle-BackColor="White" AlternatingRowStyle-ForeColor="#000" RowStyle-ForeColor="#000"  AutoGenerateColumns="false" >
                 <Columns>
                     <asp:ButtonField ItemStyle-Width = "150px"                                         DataTextField= "Department"     HeaderText = "Department"/>
                     <asp:ButtonField ItemStyle-Width = "150px"                                         DataTextField= "Cost Centre"    HeaderText = "Cost Centre"/>
                     <asp:ButtonField ItemStyle-Width = "150px"                                         DataTextField= "Name"           HeaderText = "Name" />
                     <asp:ButtonField ItemStyle-Width = "150px"                                         DataTextField= "Surname"        HeaderText = "Surname"/>                     
                     <asp:ButtonField ItemStyle-Width = "150px"                                         DataTextField= "ID No"          HeaderText = "ID No"/>
                     <asp:ButtonField ItemStyle-Width = "150px"                                         DataTextField= "Clock No"       HeaderText = "Clock No"/>
                     <asp:ButtonField ItemStyle-Width = "150px"   DataTextFormatString="{0:dd/MM/yyyy}"  DataTextField= "Date Tested"    HeaderText = "Date Tested"/>
                     <asp:ButtonField ItemStyle-Width = "150px"   DataTextFormatString="{0:dd/MM/yyyy}"  DataTextField= "Next Due Date"  HeaderText = "Next Due Date"/>
                     <asp:ButtonField ItemStyle-Width = "150px"   DataTextFormatString="{0:dd/MM/yyyy}"  DataTextField= "ECG"            HeaderText = "ECG"/>
                     <asp:ButtonField ItemStyle-Width = "150px"   DataTextFormatString="{0:dd/MM/yyyy}"  DataTextField= "Lungfunction"   HeaderText = "Lungfunction"/>
                     <asp:ButtonField ItemStyle-Width = "150px"   DataTextFormatString="{0:dd/MM/yyyy}"  DataTextField= "Hearing Test"   HeaderText = "Hearing Test"/>
                     <asp:ButtonField ItemStyle-Width = "150px"   DataTextFormatString="{0:dd/MM/yyyy}"  DataTextField= "Eye Test"       HeaderText = "Eye Test"/>
                     <asp:ButtonField ItemStyle-Width = "150px"                                         DataTextField= "Other Problems" HeaderText = "Other Problems"/>
                     <asp:ButtonField ItemStyle-Width = "150px"                                         DataTextField= "Notes"          HeaderText = "Notes"/>  


                 </Columns>

                </asp:GridView>

这是无法触发的代码

   Protected Sub drplstDepartment_SelectedIndexChanged(sender As Object, e As EventArgs)
        Dim dt As DataTable = New DataTable()
        Using conn As New SqlConnection(ConfigurationManager.ConnectionStrings("#").ToString())

            Dim cmd As New SqlCommand("SELECT Departments.ShortDescription AS [Department] ,  CostCentre AS [ Cost Centre] , FirstName AS [Name] , LastName AS [Surname] , MedicalResults.EmployeeID AS [ID No]  , EmployeeCodes.Code AS [Clock No] ,CONVERT(nvarchar(10), MedicalResults.[DateTested], 101) AS [Date Tested]  ,CONVERT(nvarchar(10), MedicalResults.[NextDueDate], 101)  AS [Next Due Date] , CONVERT(nvarchar(10), MedicalResults.ECGDate, 101) AS [ECG] , CONVERT(nvarchar(10), MedicalResults.LungFunctionDate, 101)  AS [Lungfunction] , CONVERT(nvarchar(10), MedicalResults.AudioGramDate, 101)   AS [Hearing Test] , CONVERT(nvarchar(10), MedicalResults.EyeTestDate, 101)  AS [Eye Test] , OtherProblems AS [Other Problems] , Notes FROM MedicalResults, EmployeeCodes , EmployeevsPositionLink , PositionCodes , Departments WHERE  MedicalResults.ID = EmployeeCodes.ID AND EmployeevsPositionLink.EmployeeID = EmployeeCodes.ID AND EmployeevsPositionLink.PositionID = PositionCodes.ID AND PositionCodes.DepartmentCode = Departments.Code AND  EmployeeCodes.TerminationDate Is Null AND EmployeevsPositionLink.PositionNumber = '1' AND   MedicalResults.EmployeeID = EmployeeCodes.EmployeeID AND (Departments.ShortDescription = @ShortDescription)", conn)

            cmd.Parameters.AddWithValue("@ShortDescription", drplstDepartment.SelectedValue)
            Dim da As New SqlDataAdapter(cmd)
            da.Fill(dt)
            GridView1.DataSource = dt
            GridView1.DataBind()
        End Using

    End Sub

1 个答案:

答案 0 :(得分:0)

在您的drplstDepartment_SelectedIndexChanged事件中尝试以下操作:

Dim cmd As New SqlCommand("SELECT 
... 
FROM
...
WHERE 
...
AND (Departments.Code = @Code)", conn)

cmd.Parameters.AddWithValue("@Code", drplstDepartment.SelectedItem.Value)

@Code匹配drplstDepartment.SelectedItem.Value@ShortDescription匹配drplstDepartment.SelectedItem.Text