3 DropDownLists搜索功能

时间:2012-03-26 06:09:38

标签: c# asp.net .net ado.net

我是c#的新手,如同。

我正在使用3个DropDownLists和一个提交按钮在c#中使用搜索功能。当用户在DropDownList上选择一个项目并单击“提交”时,它将打印相应选择的表。

有3个DropDownLists:

  1. 一个省,
  2. city,
  3. 专业化。
  4. 这将搜索适合选择的可用医生。例如,我选择第一个DropDownList上的province1,第二个上的city1和第三个心理学家,当提交按钮被触发时,它将打印在省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);
            }
        }
    }
    

2 个答案:

答案 0 :(得分:1)

在提交按钮点击功能

中的这一行代码中进行更改

正确的代码:

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 ...我想你错过了那个东西

在上面的一个where子句之后你错过了你需要过滤的文件名...我已经通过修改那个条件来编写更新代码。

GridViewMSDN

答案 1 :(得分:0)

你说点击提交后没有任何动静。但是在btnSub_Click中,我没有看到任何显示结果或更改页面上任何内容的内容。你应该将dt绑定到某个控件,如gridview等。