selectedindexchanged事件未触发

时间:2014-01-29 11:37:47

标签: c# asp.net

在下面的代码中,当我选择onselectedindexchanged没有触发事件的值时,我有一个下拉列表。我已经尝试但是我无法获得预期的结果。那么有人可以帮助我吗?

<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 个答案:

答案 0 :(得分: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>