当前位置:编程学习 > C#/ASP.NET >>

asp.net 开发用vb语言编写“查找”功能代码

DropDownList控件+DataGrid控件+SQL Server 实现查找并显示信息。

根据DropDownList选择的内容搜索数据库中的字段用DataGrid输出信息,

需要完整的代码。

答案:    SqlConnection conn = new SqlConnection("Server=WWW-2270B8786BB;Integrated Security=SSPI;Database=Northwind;Uid=sa;Pwd=sa;");

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{

string sqlStr = "select * from Orders ";
SqlCommand cmd = new SqlCommand(sqlStr, conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);

DataSet ds = new DataSet();
da.Fill(ds);
this.DropDownList1.DataSource = ds.Tables[0].DefaultView;
DropDownList1.DataTextField = "CustomerID";
DropDownList1.DataValueField = "OrderID";
DropDownList1.DataBind();
}

}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
string val = DropDownList1.SelectedValue;

string sqlStr = "select * from Orders where OrderID= '" + val + "'";
SqlCommand cmd = new SqlCommand(sqlStr, conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);

DataSet ds = new DataSet();
da.Fill(ds);

this.DataGrid1.DataSource = ds.Tables[0].DefaultView;
DataGrid1.DataBind();
}

上一个:ASP.NET删除listbox控件选中行代码?
下一个:ASP.NET中怎样才能使自己的代码运行的效率更高?

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,