ASP.NET中DropDownList的SelectedIndexChanged事件未触发求助
Let's figure out why your drplstDepartment_SelectedIndexChanged event isn't working (or seeming not to work) and fix it step by step:
1. First, Confirm if the Event Actually Triggers
Before digging into data logic, let's verify the event is running at all. Add a quick debug line at the start of your event handler:
Protected Sub drplstDepartment_SelectedIndexChanged(sender As Object, e As EventArgs) ' Add this line to test if the event fires Response.Write("SelectedIndexChanged event triggered!") ' Rest of your existing code... End Sub
Run the page, select a department, and check if the message appears. If it does, the event is firing—your issue lies with the data query or binding, not the event itself.
2. Fix the Mismatched SQL Parameter (Critical Bug)
This is the most likely reason your GridView isn't updating:
- Your DropDownList uses
DataValueField = "Code"(sodrplstDepartment.SelectedValuereturns the department's code, not its description). - But your SQL query filters on
Departments.ShortDescription = @ShortDescription—you're comparing a department code to a description, which will never match, resulting in an empty DataTable.
Update your event's SQL command and parameter to match:
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.Code = @DepartmentCode", conn) cmd.Parameters.AddWithValue("@DepartmentCode", drplstDepartment.SelectedValue)
3. Fix the Connection String Mismatch
In your event handler, you're using ConfigurationManager.ConnectionStrings("#").ToString()—but in Page_Load and FillWithDepartments, you use "###". Make sure the connection string name matches exactly:
Using conn As New SqlConnection(ConfigurationManager.ConnectionStrings("###").ConnectionString)
4. Add Proper Error Handling
Your current code swallows exceptions (empty Catch blocks), which hides errors that might be breaking the event logic. Update your Catch blocks to display errors so you can debug:
For example, in the event handler:
Catch ex As Exception Response.Write($"Error: {ex.Message}") ' For production, log this error to a file/database instead of showing it to users End Try
This will help you spot issues like broken database connections or invalid SQL.
5. Verify ViewState is Enabled
Postback events rely on ViewState to preserve control values. Check your page directive at the top of the ASPX file to ensure ViewState isn't disabled:
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="YourPageName.aspx.vb" Inherits="YourNamespace.YourPageName" EnableViewState="true" %>
EnableViewState defaults to true, but it's worth confirming if you've modified it.
6. Optional: Add Feedback for Empty Results
After filling the DataTable, add a check to handle cases where no employees are found for the selected department:
da.Fill(dt) If dt.Rows.Count > 0 Then GridView1.DataSource = dt GridView1.DataBind() Else GridView1.DataSource = Nothing GridView1.DataBind() Response.Write("No employees found for this department.") End If
Once you make these changes, your dropdown should correctly filter the GridView when a department is selected.
内容的提问来源于stack exchange,提问作者Tevin

