首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何从Dropdownlist中选择一个值并将值传递给Gridview?

如何从Dropdownlist中选择一个值并将值传递给Gridview?
EN

Stack Overflow用户
提问于 2017-11-02 03:10:08
回答 3查看 1.4K关注 0票数 1

我的SQL语句在将下拉列表select中的值传递到我的gridview时遇到了问题。我用一个特定的日期测试了我的SQL语句Serve Management Studio,它可以工作,但它不能处理我的下拉列表中的select值。我如何将值传递给Gridview?谢谢你的帮助,我是asp.net世界的新手。

web应用程序的图像:

我的数据库镜像:

代码语言:javascript
复制
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:";
        }
    }
}

}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-11-02 15:23:58

您可以简单地使用以下命令:

代码语言:javascript
复制
WHERE patientreservation.day = '" + DropDownListDay.Text.ToString() + "'";
票数 0
EN

Stack Overflow用户

发布于 2017-11-11 05:54:34

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

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

帮上忙了。我的网格视图也有一些问题。我把它删除了,然后做了一个新的,不知何故,这有帮助。

票数 0
EN

Stack Overflow用户

发布于 2017-11-02 11:59:08

dropdownlist未触发事件。试着在你的asp下拉列表标签中把这个AutoPostBack=写成“真”。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47062025

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档