在第二次下拉菜单中填充从选择中下拉

时间:2016-01-21 17:15:18

标签: c# sql

所以我试图从另一个选择中填充一个下拉列表。我测试了我正在使用的存储过程,当输入值时,我得到了正确的结果。我知道有很多这样的问题,但似乎没有解决我的问题。

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

1 个答案:

答案 0 :(得分:1)

如果您在执行代码时单步执行代码,您将看到在调用cmd.Parameters.AddWithValue("@Environment", ddlEnvironment.SelectedValue);时,ddlEnvironment.SelectedValue将不会设置为任何内容。这是因为在您运行此代码时,它正好在将ddlEnvironment绑定到其数据之后。它当时没有关于用户选择的信息。

您需要将第二个列表的绑定移动到处理ddlEvironment.SelectedIndexChanged事件的事件处理程序中。在那里,ddlEnvironment.SelectedValue将设置为用户选择的内容。在Page_Load中,您不希望每次回发时都重新绑定第一个列表,因此需要将其包装在if (!Page.IsPostBack)中。

请在此处查看问题:DropDownList's SelectedIndexChanged event not firing

您在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();
    }
}