2014-01-29 183 views
0

在下面的代码中,当我选择一个值时,我有一个下拉菜单,onselectedindexchanged没有触发该事件。我尝试过但我无法获得预期的结果。那么任何人都可以帮助我?onselectindexchange事件没有触发

代码

<asp:DropDownList ID="cbAutoList" 
        runat="server" 
        CssClass="cbAutoList1" 
        AutoPostBack="true" 
        OnSelectedIndexChanged="cbAutoList_SelectedIndexChanged"> 
    <asp:ListItem Value="hai"></asp:ListItem> 
    <asp:ListItem Value="hello"></asp:ListItem> 
</asp:DropDownList> 

代码隐藏

public delegate void LeavingFocusHandler(int CurrentIndex); 

public event LeavingFocusHandler LeavingFocus; 
public string strValue { get; set; } 
public int ItemIndex { get; set; } 

protected void cbAutoList_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    try 
    { 
     if (cbAutoList.Items.Count > 0) 
      this.strValue = cbAutoList.Items[cbAutoList.SelectedIndex].ToString(); 
     else 
      this.strValue = string.Empty; 

     if (this.LeavingFocus != null) 
     { 
      this.LeavingFocus(this.ItemIndex); 
     } 
    } 
    catch (Exception ex) 
    { 
     throw ex; 
    } 
} 
+1

在aspx中只有一个ListItem?如果只有一个元素,SelectedIndexChanged如何触发? –

+0

我已经检查了多个不会触发的元素 – user3224577

+0

您在下拉列表中只有1个项目。将更多列表项添加到下拉列表中。 –

回答

0

我已经在我身边只是测试你的代码,它的工作呸。请做一个测试项目,并尝试在Default.aspx其工作正常。

protected void cbAutoList_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    try 
    { 
     if (cbAutoList.Items.Count > 0) 
     { 

     } 
     else 
     { 

     } 
    } 
    catch (Exception ex) 
    { 
     throw ex; 
    } 
} 

<asp:DropDownList ID="cbAutoList" runat="server" CssClass="cbAutoList1" AutoPostBack="true" OnSelectedIndexChanged="cbAutoList_SelectedIndexChanged"> 
    <asp:ListItem Value="hai"></asp:ListItem> 
    <asp:ListItem Value="hello"></asp:ListItem> 
</asp:DropDownList> 
+0

我绑定到datalist的下拉列表将是一个问题 – user3224577

+0

@ user3224577在第一次它必须触发事件。你试过这个吗? –

+0

@ Afnan艾哈迈德它没有开火的事件。 – user3224577