无法根据动态填充的 DropDownList 选定值绑定动态创建的 GridView

Can't bind Dynamically created GridView based on Dynamically populated DropDownList selected value

我一直在努力寻找解决方案,但仍然没有什么好办法。

我从后台代码(以及 SqlDataSource)创建了一个 GridView。在我的页面中有一个 DropDownList 填充了用户名(填充在后面的代码中)。

当我 select DDL 中的用户名时,GridView 应该重新绑定并仅显示属于该特定用户的数据。出于某种原因,我不能让它工作,我不明白为什么。

这是我的代码:

创建GW和SqlDS:

SqlDataSource SqlDataSourceFormulariDaAppr = new SqlDataSource();
            SqlDataSourceFormulariDaAppr.ID = "SqlDataSourceFormulariDaAppr";
            this.Page.Controls.Add(SqlDataSourceFormulariDaAppr);
            SqlDataSourceFormulariDaAppr.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
            SqlDataSourceFormulariDaAppr.SelectCommand = "Query";
            SqlDataSourceFormulariDaAppr.SelectParameters.Add("userID", DropDownListUtenti.SelectedValue);
            SqlDataSourceFormulariDaAppr.DataBind();
            GridViewFormulariDaAppr.DataSource = SqlDataSourceFormulariDaAppr;
            GridViewFormulariDaAppr.DataBind();

SelectedIndex 方法:

    protected void DropDownListUtenti_SelectedIndexChanged(object sender, EventArgs e)
{
    GridViewFormulariDaAppr.DataBind();
}

如有任何帮助,我们将不胜感激。

我找到了一个解决方法,可能不是最安全或最好的,但它确实有效。 我去掉了SqlDS的参数,直接在查询中插入了DropDownList选择(我现在写了一个简单的例子),如下:

SqlDataSource SqlDataSourceFormulariDaAppr = new SqlDataSource();
            SqlDataSourceFormulariDaAppr.ID = "SqlDataSourceFormulariDaAppr";
            this.Page.Controls.Add(SqlDataSourceFormulariDaAppr);
            SqlDataSourceFormulariDaAppr.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
            SqlDataSourceFormulariDaAppr.SelectCommand = "SELECT * FROM table WHERE userID = " + DropDownListUtenti.SelectedValue + "";
            SqlDataSourceFormulariDaAppr.DataBind();
            GridViewFormulariDaAppr.DataSource = SqlDataSourceFormulariDaAppr;
            GridViewFormulariDaAppr.DataBind();

我想你需要这样的东西:

Parameter param = new Parameter("userID", DbType.String, DropDownListUtenti.SelectedValue);
        if (SqlDataSourceFormulariDaAppr.SelectParameters.Contains(param))
        {
            SqlDataSourceFormulariDaAppr.SelectParameters["userID"] = DropDownListUtenti.SelectedValue;
        }
        else
        {
            SqlDataSourceFormulariDaAppr.SelectParameters.Add(param);
        }

很高兴知道您找到了解决方法。 根据您问题中的代码,进行以下更改对您有用:

SqlDataSourceFormulariDaAppr.SelectParameters.Clear(); 将清除您之前添加的参数

创建GW和SqlDS:

SqlDataSource SqlDataSourceFormulariDaAppr = new SqlDataSource();
        SqlDataSourceFormulariDaAppr.ID = "SqlDataSourceFormulariDaAppr";
        this.Page.Controls.Add(SqlDataSourceFormulariDaAppr);
        SqlDataSourceFormulariDaAppr.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
        SqlDataSourceFormulariDaAppr.SelectCommand = "Query";
        SqlDataSourceFormulariDaAppr.SelectParameters.Add("userID", DropDownListUtenti.SelectedValue);
        SqlDataSourceFormulariDaAppr.DataBind();
        GridViewFormulariDaAppr.DataSource = SqlDataSourceFormulariDaAppr;
        GridViewFormulariDaAppr.DataBind();
SqlDataSourceFormulariDaAppr.SelectParameters.Clear();

SelectedIndex 方法:

   protected void DropDownListUtenti_SelectedIndexChanged(object sender, EventArgs e)
{
    populateGrid();
}

您还想补充:

SqlDataSourceFormulariDaAppr.SelectCommandType = SqlDataSourceCommandType.StoredProcedure;

之前

SqlDataSourceFormulariDaAppr.SelectCommand = "Query";