C# 无法基于动态填充的DropDownList选定值绑定动态创建的GridView
我一直在努力寻找解决办法,但还是没有什么好办法 我从代码隐藏和SqlDataSource创建了一个GridView。在我的页面中有一个DropDownList,其中填充了以代码隐藏方式填充的用户名 当我在DDL中选择用户名时,GridView应该重新绑定并仅显示属于该特定用户的数据。由于某种原因,我不能让它工作,我不明白为什么 这是我的代码: 创建GW和SQLD: SelectedIndex方法:C# 无法基于动态填充的DropDownList选定值绑定动态创建的GridView,c#,asp.net,gridview,C#,Asp.net,Gridview,我一直在努力寻找解决办法,但还是没有什么好办法 我从代码隐藏和SqlDataSource创建了一个GridView。在我的页面中有一个DropDownList,其中填充了以代码隐藏方式填充的用户名 当我在DDL中选择用户名时,GridView应该重新绑定并仅显示属于该特定用户的数据。由于某种原因,我不能让它工作,我不明白为什么 这是我的代码: 创建GW和SQLD: SelectedIndex方法: 任何帮助都将不胜感激。我找到了一个解决办法,也许不是最安全或最好的,但它很有效。 我从SqlDS中
任何帮助都将不胜感激。我找到了一个解决办法,也许不是最安全或最好的,但它很有效。 我从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);
}
很高兴知道你找到了解决办法。 根据问题中的代码,进行以下更改将对您有效: SqlDataSourceFromulariaAppr.SelectParameters.Clear;将清除以前添加的参数 创建GW和SQLD: SelectedIndex方法: 此外,您还想添加:
SqlDataSourceFormulariDaAppr.SelectCommandType = SqlDataSourceCommandType.StoredProcedure;
以前
SqlDataSourceFormulariDaAppr.SelectCommand = "Query";
你在页面上写什么。你在检查吗!Page.IsPostBack on loadif!IsPostBack{populateGrid;}我在我的Page_Load中有这个,我调用这个方法来创建GW和SqlDS。在填充DDL之前,必须检查Page.IsPostBack。请选中请在此处写入页面加载代码受保护的void DropDownListUtenti_SelectedIndexChangedobject发件人,EventArgs e{populateGrid;}
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();
protected void DropDownListUtenti_SelectedIndexChanged(object sender, EventArgs e)
{
populateGrid();
}
SqlDataSourceFormulariDaAppr.SelectCommandType = SqlDataSourceCommandType.StoredProcedure;
SqlDataSourceFormulariDaAppr.SelectCommand = "Query";