我有一个包含ListBox的UpdatePanel。每当我从DropDown更改选择时,我希望列表通过UpdatePanel进行更新。但是这不起作用。
到目前为止,这是我的代码:
Protected Sub drpDepartments_SelectedIndexChanged(sender As Object, e As EventArgs) Handles drpDepartments.SelectedIndexChanged
Dim conn As New SqlConnection("Data Source=BRIAN-PC\SQLEXPRESS;Initial Catalog=master_db;Integrated Security=True")
Dim deptComm As String = "SELECT department_id FROM departments WHERE department_name = @DepartmentName"
Dim deptSQL As New SqlCommand
Dim dr As SqlDataReader = deptSQL.ExecuteReader()
deptSQL = New SqlCommand(deptComm, conn)
deptSQL.Parameters.AddWithValue("@DepartmentName", drpDepartments.SelectedItem.Text)
dr.Read()
If dr.HasRows Then
Dim department_id As Integer = Convert.ToInt32(dr("department_id"))
Session("DepartmentID") = department_id
End If
dr.Close()
conn.Close()
ASP
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:ListBox ID="lstDeptMembers" runat="server" DataSourceID="SqlDataSource4" DataTextField="name" DataValueField="name" Height="176px" Width="204px"></asp:ListBox>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="drpDepartments" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
<asp:SqlDataSource ID="SqlDataSource4" runat="server" ConnectionString="<%$ ConnectionStrings:ConnStringDb1 %>" SelectCommand="SELECT * FROM users u
INNER JOIN
(
Select x.user_id as userid,x.department_id,y.department_name
from user_Department x
inner join departments y
on x.department_id=y.department_id WHERE x.department_id=@parameter
)
f on u.user_id= f.userid">
<SelectParameters>
<asp:SessionParameter Name="parameter" SessionField="DepartmentID" />
</SelectParameters>
</asp:SqlDataSource>
的DropDownList:
<asp:DropDownList ID="drpDepartments" runat="server" DataSourceID="SqlDataSource3" DataTextField="department_name" DataValueField="department_name">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:ConnStringDb1 %>" SelectCommand="SELECT [department_name] FROM [departments]"></asp:SqlDataSource>
截图:
如果从DropDown点击新选项,我怎样才能使ListBox更新?
编辑:包含ListBox等的表的代码..:
<asp:DropDownList ID="drpDepartments" runat="server" DataSourceID="SqlDataSource3" DataTextField="department_name" DataValueField="department_name">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:ConnStringDb1 %>" SelectCommand="SELECT [department_name] FROM [departments]"></asp:SqlDataSource>
<br />
<br />
<table style="width:100%;">
<tr>
<td style="width: 221px">
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:ListBox ID="lstDeptMembers" runat="server" DataSourceID="SqlDataSource4" DataTextField="name" DataValueField="name" Height="176px" Width="204px"></asp:ListBox>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="drpDepartments" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
<asp:SqlDataSource ID="SqlDataSource4" runat="server" ConnectionString="<%$ ConnectionStrings:ConnStringDb1 %>" SelectCommand="SELECT * FROM users u
INNER JOIN
(
Select x.user_id as userid,x.department_id,y.department_name
from user_Department x
inner join departments y
on x.department_id=y.department_id WHERE x.department_id=@parameter
)
f on u.user_id= f.userid">
<SelectParameters>
<asp:SessionParameter Name="parameter" SessionField="DepartmentID" />
</SelectParameters>
</asp:SqlDataSource>
</td>
<td style="width: 398px">
<asp:TextBox ID="txtuserSearch" runat="server"></asp:TextBox>
<asp:Button ID="btnSearchDeptUser" runat="server" Text="Search" />
<br />
<asp:Label ID="lblAddDeptUser" runat="server" Visible="False"></asp:Label>
</td>
</tr>
<tr>
<td >
<asp:Button ID="btnRmvDeptMem" runat="server" Text="Remove" />
</td>
<td>
<asp:Button ID="btnAddDeptUser" runat="server" Text="Add" />
</td>
</tr>
</table>
答案 0 :(得分:1)
如果您想在用户选择UpdatePanel
中的项目时立即更新DropDownList
,则必须将其AutoPostBack
属性设置为"True"
(默认为{{ 1}}):
false
答案 1 :(得分:1)
试试这个...... 改变这个
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
带
<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">