为什么selected.value不起作用 [英] why selected.value does not work

查看:178
本文介绍了为什么selected.value不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我在asp.net vs c#中有4个DropDownList,在app_code中我有这个代码来填充数据库中的ddl:

Hi I have 4 DropDownList in asp.net vs c# and in app_code I have this code to fill ddl from database:

public void fillddl(DropDownList ddl, string tablename, string textfieldname, string valuefieldname)
   {
       SqlConnection scon = new SqlConnection(cstr);
       scon.Open();
       string selectstr = string.Format("SELECT {0},{1} FROM {2}",

                                        textfieldname,
                                        valuefieldname,
                                        tablename);
       SqlDataAdapter sda = new SqlDataAdapter(selectstr,scon );
       DataSet ds = new DataSet();
       sda.Fill(ds);
       ddl.DataSource = ds;
       ddl.DataTextField = textfieldname;
       ddl.DataValueField = valuefieldname;
       ddl.DataBind();
       scon.Close();

   }





和page_load我有这个代码:





and in page_load I have this code :

if (!Page.IsPostBack)
        {
            dbmanager dbm = new dbmanager();
            dbm.fillddl(DropDownList1, "my_tbl", "year", "price");
            dbm.fillddl(DropDownList2, "my_tbl", "year", "price");
            dbm.fillddl(DropDownList3, "my_tbl", "year", "price");
            dbm.fillddl(DropDownList4, "my_tbl", "year", "price");
        }





和此代码:





and this code:

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
   {
       Label1.Text = DropDownList1.SelectedValue.ToString();
   }
   protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
   {
       Label2.Text = DropDownList2.SelectedValue.ToString();
   }
   protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
   {
       Label3.Text = DropDownList3.SelectedValue.ToString();
   }
    protected void DropDownList4_SelectedIndexChanged(object sender, EventArgs e)
   {
       Label4.Text = DropDownList4.SelectedValue.ToString();
   }







假设我的ddl值中的year_field是:

2012

2013

2014

2015

和price_value:

100

200

100



问题是当我选择2012年或2014年(价格为它们相同且相等)所选的ddl不会改变。但是对于其他年份(其价格不同),ddlselected值改变了并且运作良好。

请帮帮我!




suppose that year_field in my ddl values are:
2012
2013
2014
2015
and price_value:
100
200
100
400
the problem is that when I select 2012 or 2014 ( the price of them are same and equal) the ddl selected does not changed. but for other year (which theire price are different)ddlselected value changed and work good.
please help me!

推荐答案

嗨。我自己解决了问题:

首先我在我的表格中添加一个GridView

然后我改变了代码如下:



Hi. I Self Solved The Problem :
First I Add a GridView in My Form
Then I Changed Codes As Follow:

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            dbmanager dbm = new dbmanager();
            dbm.loaddataingrid("select * from dieh", GridView1);
            dbm.fillddl(DropDownList1, "my_tbl", "year", "id");
            dbm.fillddl(DropDownList2, "my_tbl", "year", "id");
            dbm.fillddl(DropDownList3, "my_tbl", "year", "id");
            dbm.fillddl(DropDownList4, "my_tbl", "year", "id");

        }
    }





和:





and :

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string i = DropDownList1.SelectedIndex.ToString();
        Label1.Text = GridView1.Rows[int.Parse(i)].Cells[1].Text; 
    }
    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {
        string i = DropDownList2.SelectedIndex.ToString();
        Label2.Text = GridView1.Rows[int.Parse(i)].Cells[1].Text; 
    }
    protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
    {
        string i = DropDownList3.SelectedIndex.ToString();
        Label3.Text = GridView1.Rows[int.Parse(i)].Cells[1].Text; 
    }
    protected void DropDownList4_SelectedIndexChanged(object sender, EventArgs e)
    {
        string i = DropDownList4.SelectedIndex.ToString();
        Label4.Text = GridView1.Rows[int.Parse(i)].Cells[1].Text; 
    }


这篇关于为什么selected.value不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆