无法基于动态填充的DropDownList选定值绑定动态创建的GridView

时间:2015-07-31 12:55:28

标签: c# asp.net gridview

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

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

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

非常感谢任何帮助。

3 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

我找到了一种解决方法,可能不是最安全或最好的,但它确实有效。 我从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();

答案 2 :(得分:0)

我认为你需要这样的东西:

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