Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
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();

   }


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();
   }



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!
Posted
Updated 21-Nov-15 1:22am
v5
Comments
John C Rayan 16-Nov-15 10:10am    
Your question is not very clear. You are missing one selectedindexchanged() code for fourth dropdown. Are you sure that the viewstate enabled for all the ddls
rezaeti 16-Nov-15 10:31am    
hi thanks for reply
viewstate enabled for all the ddls is true.
my question is:
suppose that my ddl items are:
2012
2013
2014
2015
when I select 2013 the ddl item does not select and item 2012 be selected. but for other items work correctly
rezaeti 16-Nov-15 10:42am    
all of ddl work correctly but one of item for all ddl does not selected
John C Rayan 16-Nov-15 11:14am    
Can you check the value for the item 2013 in select from HTML. Are you getting a value ? I assume that you are talking about code behind not the state in the ddl.
rezaeti 16-Nov-15 13:26pm    
yes my above code is code behind.how to solve my problem?

1 solution

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

C#
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 :

C#
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; 
    }
 
Share this answer
 
v4
Comments
Richard Deeming 27-Nov-15 8:46am    
Why are you converting an int to a string, and then immediately converting it back to an int?

Just use:
int i = ddl.SelectedIndex;
label.Text = GridView1.Rows[i].Cells[1].Text;
rezaeti 28-Nov-15 6:45am    
Hi Richard Deeming Thanks for your attention you are true and I edit my code
thanks again

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900