在第二个下拉列表中填充“选择”下拉列表
本文关键字:下拉列表 选择 第二个 填充 | 更新日期: 2023-09-27 18:31:29
所以我正在尝试从另一个下拉列表中填充一个下拉列表。 我已经测试了我正在使用的存储过程,并且在输入值时,我得到了正确的结果。 我知道有很多这样的问题,但似乎没有一个能解决我的问题。
protected void Page_Load(object sender, EventArgs e)
{
DataTable environments = new DataTable();
var connection = ConfigurationManager.ConnectionStrings["Connection"].ConnectionString;
using (SqlConnection conn = new SqlConnection(connection))
{
SqlDataAdapter adapter = new SqlDataAdapter("SELECT Environment FROM Environments", conn);
adapter.Fill(environments);
ddlEnvironment.Items.Insert(0, new ListItem(String.Empty, String.Empty));
ddlEnvironment.SelectedIndex = 0;
ddlEnvironment.DataSource = environments;
ddlEnvironment.DataTextField = "Environment";
ddlEnvironment.DataValueField = "Environment";
ddlEnvironment.DataBind();
SqlCommand cmd = new SqlCommand();
SqlDataAdapter adapter2 = new SqlDataAdapter();
DataTable servers = new DataTable();
cmd = new SqlCommand("sp_EnvironmentSelection", conn);
cmd.Parameters.AddWithValue("@Environment", ddlEnvironment.SelectedValue);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
adapter2.SelectCommand = cmd;
adapter2.Fill(servers);
ddlServer.Items.Insert(0, new ListItem(String.Empty, String.Empty));
ddlServer.SelectedIndex = 0;
ddlServer.DataSource = servers;
ddlServer.DataTextField = "ServerName";
ddlServer.DataValueField = "ServerIP";
ddlServer.DataBind();
}
}
问题是,无论我在第一个下拉菜单上的选择如何,我都没有在第二个下拉菜单上获得任何选择。
如果需要,这是存储的过程。
@Environment nvarchar(50)
AS
BEGIN
SET NOCOUNT ON
SELECT Server.ServerName, Server.ServerIP, Environments.Environment
FROM Server
INNER JOIN Environments
ON
Environments.Environment=Server.Environment
WHERE Server.Environment=@Environment
END
如果在执行代码时单步执行代码,您将看到调用cmd.Parameters.AddWithValue("@Environment", ddlEnvironment.SelectedValue);
时,ddlEnvironment.SelectedValue 将不会设置为任何内容。这是因为在运行此代码时,它正好在 ddlEnvironment 绑定到其数据之后。它当时没有关于用户选择的内容的信息。
您需要将第二个列表的绑定移动到处理 ddlEvironment.SelectedIndexChanged 事件的事件处理程序中。在那里,ddlEnvironment.SelectedValue将被设置为用户选择的内容。而在Page_Load中,您不希望每次有回发时都重新绑定第一个列表,因此需要将其包装在if (!Page.IsPostBack)
中。
请参阅此处的问题:DropDownList 的 SelectedIndexChanged 事件未触发
asp 代码中的第一个下拉列表需要如下所示:
<asp:DropDownList ID="ddlEnvironemnt" runat="server" AutoPostBack="True"
onselectedindexchanged="ddlEnvironemnt_SelectedIndexChanged">
</asp:DropDownList>
您的page_load如下所示:
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
return;
}
DataTable environments = new DataTable();
var connection = ConfigurationManager.ConnectionStrings["Connection"].ConnectionString;
using (SqlConnection conn = new SqlConnection(connection))
{
SqlDataAdapter adapter = new SqlDataAdapter("SELECT Environment FROM Environments", conn);
adapter.Fill(environments);
ddlEnvironment.Items.Insert(0, new ListItem(String.Empty, String.Empty));
ddlEnvironment.SelectedIndex = 0;
ddlEnvironment.DataSource = environments;
ddlEnvironment.DataTextField = "Environment";
ddlEnvironment.DataValueField = "Environment";
ddlEnvironment.DataBind();
}
}
您将有一个事件处理程序:
protected void ddlEnvironemnt_SelectedIndexChanged(object sender, EventArgs e)
{
var connection = ConfigurationManager.ConnectionStrings["Connection"].ConnectionString;
using (SqlConnection conn = new SqlConnection(connection))
{
SqlCommand cmd = new SqlCommand();
SqlDataAdapter adapter2 = new SqlDataAdapter();
DataTable servers = new DataTable();
cmd = new SqlCommand("sp_EnvironmentSelection", conn);
cmd.Parameters.AddWithValue("@Environment", ddlEnvironment.SelectedValue);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
adapter2.SelectCommand = cmd;
adapter2.Fill(servers);
ddlServer.Items.Insert(0, new ListItem(String.Empty, String.Empty));
ddlServer.SelectedIndex = 0;
ddlServer.DataSource = servers;
ddlServer.DataTextField = "ServerName";
ddlServer.DataValueField = "ServerIP";
ddlServer.DataBind();
}
}