我有一个下拉列表和一个中继器控件。我已经用数据库中的部门名称填充了下拉列表。我正在努力从下拉列表中选择一个部门,并让该部门的所有员工都显示在转发器控件中。我们将不胜感激。
这是我的下拉列表
<asp:DropDownList ID="drplstDepartment" OnSelectedIndexChanged="drplstDepartment_SelectedIndexChanged" CssClass="form-control mb-2 mr-sm-2" runat="server" AutoPostBack="true" AppendDataBoundItems="true">
</asp:DropDownList>
这是我的中继器控件
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<%--<p>All The Data Is Shown</p>--%>
<table class="table table-default table-striped table-bordered table-condensed table-hover table-responsive">
<tr style="text-align:center;">
<th>Department</th>
<th>Cost Centre</th>
<th>Name</th>
<th>Surname</th>
<th>ID Number</th>
<th>Clock Number</th>
<th>Date Tested</th>
<th>Next Due Date</th>
<th>ECG</th>
<th>Lung Function</th>
<th>Hearing Test</th>
<th>Eye Test</th>
<th>Other Problems</th>
<th>Notes</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%# Eval("LongDescription") %></td>
<td><%# Eval("Code") %></td>
<td><%# Eval("FirstName") %></td>
<td><%# Eval("LastName") %></td>
<td><%# Eval("EmployeeID") %></td>
<td><%# Eval("Code") %></td>
<td><%# Eval("[Date tested]") %></td>
<td><%# Eval("[Next Due date]") %></td>
<td><%# Eval("[ECG]") %></td>
<td><%# Eval("[Lungfunction]") %></td>
<td><%# Eval("[Hearing Test]") %></td>
<td><%# Eval("[Eye Test]") %></td>
<td><%# Eval("[Other Problems]") %></td>
<td><%# Eval("[Notes]") %></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
答案 0 :(得分:0)
这是我的代码,用于填充下拉列表
Protected Sub FillWithDepartments()
Dim conn As New SqlConnection("server=XX; Integrated Security = true")
conn.Open()
Dim cmd As New SqlCommand("SELECT * FROM Departments Order By LongDescription ASC", conn)
Dim adapter As New SqlDataAdapter(cmd)
Dim tbl As New DataTable()
adapter.Fill(tbl)
drplstDepartment.DataSource = tbl
drplstDepartment.DataTextField = "LongDescription"
drplstDepartment.DataValueField = "Code"
drplstDepartment.DataBind()
conn.Close()
End Sub
这是我希望从下拉列表中选择的文本/值填充我的中继器控件的代码
Protected Sub drplstDepartment_SelectedIndexChanged(sender As Object, e As EventArgs)
Dim connString As New SqlConnection(ConfigurationManager.ConnectionStrings("LumotechPortal.My.MySettings.GuHR_Data").ToString())
connString.Open()
Dim cmdText = "select * from [EmployeeMedicalTest] WHERE (LongDescription = @LongDescription)"
Dim Data = New DataTable()
Dim Adapter = New SqlDataAdapter(cmdText, connString)
Adapter.SelectCommand.Parameters.AddWithValue("@LongDescription", drplstDepartment.SelectedValue)
Adapter.Fill(Data)
Repeater1.DataSource = Data
Repeater1.DataBind()
connString.Close()
End Sub
答案 1 :(得分:0)
行中的评论。
Protected Sub drplstDepartment_SelectedIndexChanged(sender As Object, e As EventArgs)
Dim Data = New DataTable()
'connString is a poor name for a connection
'connString implies Connection String and a connection is not a String it has a ConnectionString property
'call it cn or con or conn
Using cn As New SqlConnection(ConfigurationManager.ConnectionStrings("LumotechPortal.My.MySettings.GuHR_Data").ToString())
'Pass the query string and the connection directly to the constructor of the command
Using cmd As New SqlCommand("select * from EmployeeMedicalTest WHERE Code = @Code;", cn)
'Remember that you set the drplstDepartment.DataValueField = "Code"
'I am guessing at the datatype, check your database
'Always use .Add instead of .AddWithValue
'It will save you from type mismatches
cmd.Parameters.Add("@Code", SqlDbType.Int).Value = CInt(drplstDepartment.SelectedValue)
Data.Load(cmd.ExecuteReader)
End Using 'closes and disposes Command
End Using 'closes and disposes Connection
Repeater1.DataSource = Data
Repeater1.DataBind()
End Sub