首页 > 解决方案 > 从下拉列表中选择一个值,并在转发器控件中显示与该选定值相关的所有信息

问题描述

我有一个下拉列表和一个转发器控件。我已经用数据库中的部门名称填充了下拉列表。我正在努力从下拉列表中选择一个部门,并让该部门的所有员工都显示在转发器控件中。任何帮助将不胜感激。

这是我的下拉列表

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

标签: asp.netvb.nettwitter-bootstrap

解决方案


这是我填充下拉列表的代码

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


推荐阅读