2014-09-20 45 views
0

我有一個帶下拉列表的Gridview是在GridView的OnRowDataBound事件中動態創建的,最初我設置了一個選定的值。DropDownList selectedindexchanged在GridView內的下拉式綁定期間不會觸發默認選定的項目

問題是,當我切換到不同的下拉下拉其工作正常,但當我更改爲默認選定的索引時,SelectedIndexChanged未被觸發。

請幫助我..

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 
    } 

回答

2

AutoPostBack="true"和嘗試

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

我注意到這樣的行爲也時有在具有相同的值列表中有多個值。 因此,而不是Code for Value,您可以使用永遠不會相同的序列號。

因此對於例如

如果選定的索引值是「X」,並且下一個選定的值也是「X」,那麼它可能不會調用。

相關問題