如何使用C#基于下拉列表选择的值填充gridview?

时间:2013-07-22 07:27:16

标签: c# asp.net visual-studio-2010 drop-down-menu sql-server-2008-r2

我需要根据下拉列表选择值填充网格:我的c#编码是

protected void atddroplist_SelectedIndexChanged(object sender, EventArgs e)
{
      empatdListBI c = new empatdListBI();
      DbConnection b = new DbConnection();
      SqlDataAdapter da = new SqlDataAdapter();
      DataTable dt = new DataTable();
      DataSet ds = new DataSet();


      if (atddroplist.SelectedIndex == 1)
      {
         b.OpenConnection();
         dt = c.LoadRecords(empText.Text);
         GridView1.DataSource = dt;
         GridView1.DataBind();
         b.CloseConnection();
      }
}

通过这种编码,Iam无法在输出中看到Grid。请帮帮我

2 个答案:

答案 0 :(得分:0)

我们可以检查几件事 - AutoPostBack下拉列表设置为True
- 我们在dt中获取数据 - 如有必要,我们可以将网格放在您网页上的更新面板中

答案 1 :(得分:0)

    if (atddroplist.SelectedIndex  == 1)
            {
              empatdListBI c = new empatdListBI();
      DbConnection b = new DbConnection();
      SqlDataAdapter da = new SqlDataAdapter();
               DataTable DT = new DataTable();
               DT = c.LoadRecords(empText.Text);
  b.OpenConnection();
               if (DT.Rows.Count == 0)
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "PopUp", "alert(' Record Not Found...');", true);
                    txtsearchrecord.Text = "";
                    txtsearchrecord.Focus();
                }
                else
                {
                    GridView1.DataSource = DT;
                    GridView1.DataBind();                    
                }  
     b.CloseConnection();                 
            }