nitinvertigo
nitinvertigo

Reputation: 1180

dropdown_SelectedIndexChanged works second time only

Here I am trying to populate a dropdown list based on the selected value from another dropdown list. Here is my code for both:

<div>
    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" onselectedindexchanged="DropDownList1_SelectedIndexChanged">
    </asp:DropDownList>

    <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="true">
    </asp:DropDownList> 
</div>
</form>

And here is the code-behind:

 protected void Page_Load(object sender, EventArgs e)
 {
     if (!IsPostBack)
     {
         getDeveloperId();
     }
 }


  public void getProjectId()
        {
            string projName, projId, projFinal;
            using (var cmdScope_ID = new SqlCommand("SELECT [ProjectId],[ProjectName] FROM [DB].[dbo].[Sample Table]", con))
            {
                using (var reader = cmdScope_ID.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        projId = reader[0].ToString();
                        projName = reader[1].ToString();
                        projFinal = projId + "-" + "[" + projName + "]";
                        DropDownList1.Items.Insert(0, new ListItem(projFinal, "1"));
                    }
               }
            }
        }

 protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
 {
     //code to get data from DB and populate DropDownList2
 }

My problem is when I select any entry from the first dropdown, nothing happens the first time. Only the page reloads. But if I do it again then only the second dropdown populates.

Also, if I keep if (!IsPostBack) condition, nothing happens. But if I remove it then, the first dropdown is populated twice and then only the second dropdown comes. Any suggestions why this is happening?

EDIT: I have added the code to populate the first dropdown. Same code for second dropdown also.

Upvotes: 0

Views: 542

Answers (2)

nitinvertigo
nitinvertigo

Reputation: 1180

I have changed the way the data is bound to the dropdown. Now its working fine.

 protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                getDeveloperId();
            }
        }
        public void getDeveloperId()
        {
            con.Open();
            SqlDataAdapter da = new SqlDataAdapter("SELECT [DeveloperId], [DeveloperName] FROM [DB].[dbo].[tbl]", con);
            DataSet ds = new DataSet();
            da.Fill(ds);
            DropDownList1.DataSource = ds.Tables[0];  //bind the ddp_dataview to dataview
            DropDownList1.DataTextField = "DeveloperName";
            DropDownList1.DataValueField = "DeveloperId";
            DropDownList1.DataBind();
        }

Upvotes: 1

Neeraj
Neeraj

Reputation: 4489

Hey the problem is that line DropDownList1.Items.Insert(0, new ListItem(projFinal, "1"));

Because you are adding item on index "0" with value 1 and compiler gets not attached selectedchange event of dropdown.

Please bind items on different index with value then only only your problem can be resolved.

I have make a simple program to test your Issue. please look.

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            fill();
        }

    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        List<int> lst =
      new List<int>();
        lst.Add(12);
        lst.Add(22);
        lst.Add(32);
        DropDownList2.DataSource = lst;
        DropDownList2.DataBind();
    }

    private void fill()
    {
        List<int> lst =
        new List<int>();
        lst.Add(1);
        lst.Add(2);
        lst.Add(3);
        var count=0;
        foreach (var i in lst)
        {
            DropDownList1.Items.Insert(count, new ListItem(i.ToString(), count.ToString()));
            count++;
        }


    }

Hope it will helps you...

Upvotes: 1

Related Questions