首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如果我从下拉列表中选择value,相应的值应该会显示在文本框中

如果我从下拉列表中选择value,相应的值应该会显示在文本框中
EN

Stack Overflow用户
提问于 2011-06-03 13:02:49
回答 3查看 3.6K关注 0票数 0

我的数据记录表有以下数据:

代码语言:javascript
复制
Name       Shiftname       operatorname         date        Plantname       Line    Machine
Ashwini   Shift1(7-3)     Operator 1    2011-05-24      Plant 1    Line1       mc1
Deepika   Shift2(3-11)  Operator 2  2011-05-24      Plant 2    Line3       mc5
Pradeepa      Shift2(11-7)  Operator 3  2011-05-25      Plant 3    Line5       mc10
Deepika   Shift1(7-3)   Operator 1  2011-05-25      Plant 1    Line1       mc1

我提供了两个下拉列表,即line和shift,如果用户从日历中选择日期,则提供一个文本框来存储日期,以及两个文本框来存储工厂和操作员名称的值。

例如,如果用户从下拉列表中选择line和shift,并从日历中选择日期,则应在文本框中显示对应的工厂名和操作符名称,例如,如果用户从下拉列表中选择line1,从下拉列表中选择shift1,并且日期为25/05/2011,则两个文本框应显示值为plant1和operator 1

我写的代码有以下几个:

代码语言:javascript
复制
protected void ddlline_SelectedIndexChanged(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("connection string");
        con.Open();
        DataTable dt = new DataTable();
        SqlCommand sqlCmd = new SqlCommand("SELECT distinct plantname,operatorname FROM datalogging1 WHERE Line='" + ddlline.SelectedItem + "'and date='"+txtdate.Text+"'and shiftname='"+ddlshift.SelectedItem+"'", con);
        SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
        sqlDa.Fill(dt);

        if (dt.Rows.Count > 0)
        {
            //Where ColumnName is the Field from the DB that you want to display 
            txtplant.Text = dt.Rows[0]["Plantname"].ToString();
            txtop.Text = dt.Rows[0]["operatorname"].ToString();
        }
    }

但是它没有显示出来。

EN

回答 3

Stack Overflow用户

发布于 2011-06-03 13:11:30

问题出在您的SQL Query where Clause中,您正在以错误的方式设置值。

它应该是ddlline.SelectedItem.Value而不是ddlline.SelectedItem,因为ddlline.SelectedItem返回listitem,但是您需要SelectedValue,dropdown ddlshift也是如此

票数 2
EN

Stack Overflow用户

发布于 2011-06-03 13:14:35

尝试@Muhammad Akhtar建议,但我也可以看到您没有使用SQL参数,并且您的代码容易受到SQL注入攻击。这很容易避免,它将使您的嵌入式SQL更加美观。

代码语言:javascript
复制
SELECT distinct plantname, operatorname 
FROM datalogging1 
WHERE Line = @Line AND date = @Date AND shiftname = @ShiftName

然后,在执行此语句之前,请添加带有值的参数。

代码语言:javascript
复制
sqlCmd.Parameters.AddWithValue("@Line", ddlline.SelectedItem.Value);
// passing the date as text is also a bad idea because it will make your
// date format dependant on culture and language specific settings in both
// the database and application code if you parse the date first and
// pass the value as a DateTime value you eliminate the date format hassle
// that might otherwise occur in the database
sqlCmd.Parameters.AddWithValue("@Date ", txtdate.Text);
sqlCmd.Parameters.AddWithValue("@ShiftName", ddlshift.SelectedItem.Value);
票数 1
EN

Stack Overflow用户

发布于 2011-06-03 13:31:37

必须使用dropdownlist changed事件来检测用户何时从下拉列表中选择值,并实现代码以获取该事件中的详细信息

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

https://stackoverflow.com/questions/6223143

复制
相关文章

相似问题

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