代码如下
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;public partial class Test : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       
           this.GridView1.DataSource = GetHouseInfo();
           this.GridView1.DataBind();
        
        
           }
      private DataTable GetHouseInfo()
       {
           string con = "Data Source=\\SQLEXPRESS;Initial Catalog=a;Integrated Security=True";           DataSet ret = new DataSet();           using (SqlConnection connection = new SqlConnection(con))
           {
               string queryString = "select Name from a where hCity='" + DropDownList1.SelectedValue + " '";               SqlDataAdapter adapter = new SqlDataAdapter();
               SqlCommand command = new SqlCommand(queryString, connection);
               command.CommandType = CommandType.Text;
               adapter.SelectCommand = command;
               adapter.Fill(ret);
           }
           return ret.Tables[0];
       }
}
脚本端有个Dropdownlist控件,我是新手,请大家帮帮我,非常感谢

解决方案 »

  1.   

    我想用DropdownList的值作为查询条件,不知道错在哪里,第一次提问,很激动,问题没表述清楚,请大家见谅
      

  2.   

    逻辑错了:DropdownList有选中事件,应该在选中某个值callback值,调用GetHouseInfo,再次绑定数据的时候就可以刷新页面了.....
      

  3.   

    在我没看到你的结果和你问出具体问题的情况下,我想问下是不是这两种情况
    下拉列表绑定的是2个值
     有2种情况 第一种 绑定事件的时间差问题(值乱码)
          第二种 下拉列表的绑定对象有2个 你是否绑定错误
    还有一个 Page_Load里面是否是因为回传所引起
     添加一个IF(!IsPostBack)
    {
    }
    试试
      

  4.   

    干嘛不用 服务器端的DropDownlist 不是很方便么 ?
      

  5.   

    一不明真相的人路过理论上应该是用下拉框的onchangindex或者onchangtext事件
    然后再绑定数据
      

  6.   

    代码如下 
    using System.Data; 
    using System.Configuration; 
    using System.Collections; 
    using System.Web; 
    using System.Web.Security; 
    using System.Web.UI; 
    using System.Web.UI.WebControls; 
    using System.Web.UI.WebControls.WebParts; 
    using System.Web.UI.HtmlControls; 
    using System.Data.SqlClient; 
    public partial class Test : System.Web.UI.Page 

        protected void Page_Load(object sender, EventArgs e) 
        { 
          
              this.GridView1.DataSource = GetHouseInfo(); 
              this.GridView1.DataBind(); 
            
            
              } 
          private DataTable GetHouseInfo() 
          { 
              string con = "Data Source=\\SQLEXPRESS;Initial Catalog=a;Integrated Security=True"; 
    //确定你的连接正确否
              DataSet ret = new DataSet(); 
    //这里是对的
              using (SqlConnection connection = new SqlConnection(con)) 
              { 
                  string queryString = "select Name from a where hCity='" + DropDownList1.SelectedValue + " '"; 
    //这里没错
                  SqlDataAdapter adapter = new SqlDataAdapter(); 
                  SqlCommand command = new SqlCommand(queryString, connection); 
                  command.CommandType = CommandType.Text; 
                  adapter.SelectCommand = command; 
    //我不知道为什么你声明了一个command还要再声明adapter,这两个貌似用一个就可以了哦
    //看我给你改的:
    //另外你注意一点哦,你没打开数据库,好大的一个玩笑。connection.open();/*SqlDataAdapter adapter = new SqlDataAdapter(queryString, connection); 
    adapter.Fill(ret,"a");
    connection.close();
      */               
              } 
              return ret.Tables[0]; 
          }