Sarbani Bhowmik
Sarbani Bhowmik

Reputation: 31

DropDownList selectedindexchanged not firing on default selected item during dropdown bind within Gridview

I have a Gridview with dropdownlist is created dynamically in OnRowDataBound event of gridview, initially I am setting a selected value.

The problem is when I switch to different index of dropdown its working fine but when I change to the default selected index the SelectedIndexChanged is not fired.

Kindly help me..

protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
 DropDownList DropDownList1 = new DropDownList();
                    DropDownList1.ID = "DropDownList1";
                    DropDownList1.SelectedIndexChanged += new EventHandler(DropDownList1_SelectedIndexChanged);
                    DropDownList1.EnableViewState = true;
                    DropDownList1.AutoPostBack = true;
                    DropDownList1.EnableViewState = true;
                    string sql1 = ".....";
                    DataTable dtDDL = new DataTable();
                    dtDDL = SQL.ReturnDataTable(sql1);
                    if (dtDDL.Rows.Count > 0)
                    {
                        DropDownList1.DataSource = dtDDL;
                        DropDownList1.DataTextField = "CODE";
                        DropDownList1.DataValueField = "CODE";
                        DropDownList1.DataBind();
                        DropDownList1.Font.Size = 8;
                        //DropDownList1.Items.Insert(0, new ListItem("0", "0"));
                    }

                        DropDownList1.SelectedValue = dtShift.Rows[0]["SHIFT_CODE"].ToString();
                        DropDownList1.ToolTip = dtShift.Rows[0]["ShiftTime"].ToString();
                  }






  protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
       //not coming here for default index changed
    }

Upvotes: 0

Views: 666

Answers (2)

codebased
codebased

Reputation: 7073

I have noticed such behaviour also when there is more than one value in the list that has same values. Thus instead of Code for Value, you could use a sequence number that will never be the same.

So for e.g.

If the selected index value is "X" and next selected value is also "X" then it may not call.

Upvotes: 0

Genish Parvadia
Genish Parvadia

Reputation: 1455

AutoPostBack="true" and try it

<asp:DropDownList ID="Drplist1" AutoPostBack="true" runat="server" OnSelectedIndexChanged="Drplist1_SelectedIndexChanged"></asp:DropDownList>

Upvotes: 2

Related Questions