【问题标题】:Can't bind Dynamically created GridView based on Dynamically populated DropDownList selected value无法根据动态填充的 DropDownList 选定值绑定动态创建的 GridView
【发布时间】:2015-08-01 17:02:05
【问题描述】:

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

我从后面的代码(以及 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();
}

任何帮助将不胜感激。

【问题讨论】:

  • 你在 page_load 中写了什么。你在检查 !Page.IsPostBack 加载吗
  • if (!IsPostBack) { populateGrid();我的 Page_Load 中有这个,我调用方法来创建 GW 和 SqlDS。
  • 在填充 DDL 之前,您必须检查 Page.IsPostBack。请检查
  • 请在此处编写您的页面加载代码
  • 受保护的 void DropDownListUtenti_SelectedIndexChanged(object sender, EventArgs e) { populateGrid(); }

标签: c# asp.net gridview


【解决方案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";

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-24
        • 1970-01-01
        • 2017-06-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多