【问题标题】:How to select a value from Dropdownlist and pass value to Gridview?如何从下拉列表中选择一个值并将值传递给 Gridview?
【发布时间】:2018-04-14 04:38:55
【问题描述】:

我的 SQL 语句无法将下拉列表选择中的值传递到我的网格视图。我用特定的日期测试了我的 SQL 语句 Serve Management Studio,它可以工作,但它不适用于我的下拉列表中的选择值。我如何将值传递给 Gridview?感谢您的帮助,我是 asp.net 世界的新学生。

网络应用程序的图像: 我的数据库的图像:

public void RefreshDay()
        {
            SqlConnection conn = new SqlConnection(@"data source =.\sqlexpress; integrated security = true; database = DBdentist");
            SqlDataAdapter da = null;
            DataSet ds = null;
            DataTable dt = null;
            string sqlsel = "SELECT Distinct patient.patientID, day from patient, patientreservation where patient.patientID = patientreservation.patientID";


        try
        {

            da = new SqlDataAdapter();  
            da.SelectCommand = new SqlCommand(sqlsel, conn);  
            ds = new DataSet();
            da.Fill(ds, "myDay");   
            dt = ds.Tables["myDay"];   


            DropDownListDay.DataSource = dt;
            DropDownListDay.DataTextField = "day";
            DropDownListDay.DataValueField = "patientID";
            DropDownListDay.DataBind();
            DropDownListDay.Items.Insert(0, "Select Day"); 
        }
        catch (Exception ex)
        {
            LabelDay.Text = ex.Message; 
        }
        finally
        {
            conn.Close(); 
        }

    }



    protected void DropDownListDay_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DropDownListDay.SelectedIndex != 0)
        {
            SqlConnection conn = new SqlConnection(@"data source =.\sqlexpress; integrated security = true; database = DbDentist");
            SqlDataAdapter da = null;
            DataSet ds = null;
            DataTable dt = null;
            string sqlsel = "SELECT patientreservation.patientID, patient.firstname, patient.lastname, patientreservation.day, patientreservation.hour, treatment.treatment FROM((patientreservation INNER JOIN patient ON patientreservation.patientID = patient.patientID) INNER JOIN treatment ON patientreservation.treatmentID = treatment.treatmentID) WHERE patientreservation.day = " + DropDownListDay.SelectedValue + "";

            try
            {

                da = new SqlDataAdapter();  
                da.SelectCommand = new SqlCommand(sqlsel, conn); 
                ds = new DataSet();
                da.Fill(ds, "myDay");  
                dt = ds.Tables["myDay"];
                GridViewDay.DataSource = dt; 
                GridViewDay.DataBind();

            }
            catch (Exception ex)
            {
                LabelDay.Text = ex.Message; 
            }
            finally
            {
                conn.Close(); 
            }

        }

        else
        {
            LabelDay.Text = "You choose None:";
        }
    }
}

}

【问题讨论】:

  • DropDownListDay.SelectedValue 应该是数值吗?还是像“星期一”或类似的东西
  • 数据类型是日期...'2017-07-05'
  • SELECT patientreservation.patientID,patient.firstname,patient.lastname,patientreservation.day,patientreservation.hour,treatment.treatment FROM ((patientreservation INNER JOIN patient ON patientreservation.patientID=patient.patientID) INNER JOIN治疗 ON patientreservation.treatmentID =treatment.treatmentID) WHERE patientreservation.day = '2017-07-05' 这在我测试时工作正常,但是当我添加所选值时我开始遇到问题
  • 日期周围的那些单引号不在您的代码中。
  • 我已经尝试了几次单引号但没有运气......

标签: c# sql asp.net gridview


【解决方案1】:

你可以简单地使用这个:

WHERE patientreservation.day = '" + DropDownListDay.Text.ToString() + "'";

【讨论】:

    【解决方案2】:

    感谢您的帮助。我什么都试过了

    DropDownListDay.DataTextField = "day"; DropDownListDay.DataValueField = "day"

    帮助。我的gridview也有一些问题。我加起来删除它并制作一个新的,不知何故有帮助。

    【讨论】:

      【解决方案3】:

      你的下拉菜单的配置是

      DropDownListDay.DataTextField = "day";
      DropDownListDay.DataValueField = "patientID";
      

      但如果您想使用当前选定的值进行过滤,则需要将 day 作为值

      DropDownListDay.DataTextField = "day";
      DropDownListDay.DataValueField = "day";
      
      SELECT patientreservation.patientID, patient.firstname, patient.lastname, patientreservation.day, patientreservation.hour, treatment.treatment FROM((patientreservation INNER JOIN patient ON patientreservation.patientID = patient.patientID) INNER JOIN treatment ON patientreservation.treatmentID = treatment.treatmentID) WHERE **patientreservation.day** = " + DropDownListDay.**SelectedValue** + "";
      

      或者你可以试试WHERE **patientreservation.day** = " + DropDownListDay.**SelectedText** + "";

      【讨论】:

      • 仍然无法通过这些更改查看网格视图。对所选文本非常不满意。 DropDownListDay.DataTextField = "天"; DropDownListDay.DataValueField = "天";很有道理,但仍然不确定为什么它不会显示
      • 解决日期时间数据类型问题的方法类似于 $"WHERE DATEDIFF(dd, patientreservation.day, {DropDownListDay.SelectedValue}) =0";如果不起作用,请尝试添加简单的引号
      【解决方案4】:

      下拉列表未触发事件。尝试将此 AutoPostBack="true" 放在您的 asp 下拉列表标签中。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-22
        • 1970-01-01
        • 1970-01-01
        • 2020-02-24
        • 1970-01-01
        相关资源
        最近更新 更多