代码如下
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控件,我是新手,请大家帮帮我,非常感谢
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控件,我是新手,请大家帮帮我,非常感谢
解决方案 »
- 请好心人总结asp.net前台验证正则表达式
- mvc 页面超时跳转。跳出ifram.
- Repeater中获取Radio的值
- 编译不了,什么原因?
- asp.net 新建一個excel.xls文件怎麼這個文件無法打開
- 一个很简单的函数自己却看不懂
- ---- 50 分求助---- Ajax 问题?
- OracleConnection引用的是哪个类?在线等,没有MSDN。
- 请问:在vs2003.net中,为何只能新建asp页,如何新建asp.net(aspx)页,还有,如何打开一个新的窗口?谢谢!
- 下载了一个数据访问层代码,不知道怎么用,请指教
- 【高分】急问,在线等!
- asp页面 和asp.net后台代码怎么互动
下拉列表绑定的是2个值
有2种情况 第一种 绑定事件的时间差问题(值乱码)
第二种 下拉列表的绑定对象有2个 你是否绑定错误
还有一个 Page_Load里面是否是因为回传所引起
添加一个IF(!IsPostBack)
{
}
试试
然后再绑定数据
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];
}
}