3下拉列表搜索功能
本文关键字:功能 搜索 下拉列表 | 更新日期: 2023-09-27 18:28:55
我是c#的新手,如.
我目前正在c#中使用3个DropDownLists和一个提交按钮进行搜索。当用户在DropDownList上选择一个项目并单击提交时,它将打印相应选择的表格。
有3个下拉列表:
- 一个省
- 城市
- 专业化
这将搜索适合选择的可用医生。例如,我在第一个下拉列表上选择省1,在第二个下拉列表中选择城市1,并在第三个下拉列表选择心理学家,当按下提交按钮时,它将打印省1、城市1中有心理学家专业知识的可用医生。
我已经有了一个代码,还在想,但当我点击提交按钮时,什么都没有发生。有人能帮我吗?
以下是我迄今为止所做的:
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
System.Threading.Thread.Sleep(1000);
if (!IsPostBack)
{
string constring = ConfigurationManager.ConnectionStrings["AccreString"].ConnectionString;
SqlConnection conn = new SqlConnection(constring);
DataTable dt = new DataTable("emed_province");
using (conn)
{
conn.Open();
SqlCommand comm = new SqlCommand("SELECT * FROM emed_province ORDER BY PROVINCE_NAME ASC", conn);
SqlDataAdapter adptr = new SqlDataAdapter(comm);
adptr.Fill(dt);
}
ddlProvince.DataSource = dt;
ddlProvince.DataTextField = "PROVINCE_NAME";
ddlProvince.DataValueField = "PROVINCE_CODE";
ddlProvince.DataBind();
ddlProvince.Items.Insert(0, new ListItem("---------------SELECT---------------", "0"));
ddlCity.Items.Insert(0, new ListItem("---------------SELECT---------------", "0"));
ddlSpec.Items.Insert(0, new ListItem("---------------SELECT---------------", "0"));
}
}
protected void ddlProvince_SelectedIndexChanged(object sender, EventArgs e)
{
System.Threading.Thread.Sleep(1000);
string constring = ConfigurationManager.ConnectionStrings["AccreString"].ConnectionString;
SqlConnection conn = new SqlConnection(constring);
DataTable dt = new DataTable("emed_province");
using (conn)
{
conn.Open();
SqlCommand comm = new SqlCommand("SELECT * FROM emed_city WHERE PROVINCE_CODE =@pcode", conn);
comm.Parameters.AddWithValue("@pcode", ddlProvince.SelectedValue);
SqlDataAdapter adptr = new SqlDataAdapter(comm);
adptr.Fill(dt);
SqlParameter param = new SqlParameter();
param.ParameterName = "@pcode";
param.Value = ddlProvince;
comm.Parameters.Add(param);
}
ddlCity.DataSource = dt;
ddlCity.DataTextField = "CITY_NAME";
ddlCity.DataValueField = "CITY_CODE";
ddlCity.DataBind();
ddlCity.Items.Insert(0, new ListItem("---------------SELECT---------------", "0"));
}
protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
{
System.Threading.Thread.Sleep(1000);
string constring = ConfigurationManager.ConnectionStrings["AccreString"].ConnectionString;
SqlConnection conn = new SqlConnection(constring);
DataTable dt = new DataTable("emed_city");
using (conn)
{
conn.Open();
SqlCommand comm = new SqlCommand("SELECT * FROM emed_specialization", conn);
comm.Parameters.AddWithValue("@ccode", ddlCity.SelectedValue);
SqlDataAdapter adptr = new SqlDataAdapter(comm);
adptr.Fill(dt);
SqlParameter param = new SqlParameter();
param.ParameterName = "@ccode";
param.Value = ddlCity;
comm.Parameters.Add(param);
}
ddlSpec.DataSource = dt;
ddlSpec.DataTextField = "SPEC_NAME";
ddlSpec.DataValueField = "SPEC_CODE";
ddlSpec.DataBind();
ddlSpec.Items.Insert(0, new ListItem("---------------SELECT---------------", "0"));
}
protected void btnSub_Click(object sender, EventArgs e)
{
string constring = ConfigurationManager.ConnectionStrings["AccreString"].ConnectionString;
SqlConnection conn = new SqlConnection(constring);
DataTable dt = new DataTable("emed_doctors");
using (conn)
{
conn.Open();
SqlCommand comm = new SqlCommand("SELECT DOCTOR_NAME FROM emed_doctors where Province = '" + ddlProvince.SelectedItem.ToString() + "'", conn);
SqlDataAdapter adptr = new SqlDataAdapter(comm);
adptr.Fill(dt);
}
}
}
更改提交按钮点击功能中的这行代码
正确代码:
SqlCommand comm = new SqlCommand("SELECT DOCTOR_NAME FROM emed_doctors where province = '" + ddlProvince.SelectedItem.ToString() + "'", conn);
因为当你看到你的代码查询不正确时,请查看你的代码,这是不正确的
错误代码:
SqlCommand comm = new SqlCommand("SELECT DOCTOR_NAME FROM emed_doctors where (" + ddlProvince.SelectedItem.ToString() + " ", conn);
编辑:
如果你想显示你在DataTable中得到的记录,你要么需要循环这些记录,要么需要使用GridView。。。我想你错过了
在上面的after-where子句中,您错过了需要对其进行筛选的文件的名称…我已经通过修改该条件编写了更新代码。
有关GridView
,请参阅MSDN。
您说点击提交后,什么都没有发生。但在btnSub_Click中,我没有看到任何显示结果或更改页面的内容。您应该将dt绑定到一些控件,如网格视图等。