ASP ListBox错误的ID

时间:2014-11-25 23:03:19

标签: c# asp.net listbox

所以我有这2个ListBox,一切正常,如果我只使用第二个,问题在点击第一个后开始,发生在下次我点击第二个时,它得到第一个的ID ... < / p>

所以..

点击2 - &gt;作品

点击2 - &gt;作品

点击1 - &gt;作品

点击1 - &gt;作品

点击2 - &gt;我得到了第一个的ID ......

我在所有浏览器中遇到此问题......

<div class="col-sm-3" style="overflow:auto;">
    <label for="lbRoot" class="control-label">Root Cause</label> 
    <asp:ListBox ID="lbRoot" 
        runat="server" 
        CssClass="form-control" 
        Height="250px" 
        Width="170px"
        DataTextField="name" 
        DataValueField="file"
        OnSelectedIndexChanged="Index_Changed"
        AutoPostBack="true">
    </asp:ListBox>
</div>


<div class="col-sm-3" style="overflow:auto;">
    <label for="lbEvidence" class="control-label ">Follow Up</label>
    <asp:ListBox ID="lbEvidence" 
        runat="server" 
        CssClass="form-control" 
        Height="250px" 
        Width="170px"
        DataTextField="name" 
        DataValueField="file"
        OnSelectedIndexChanged="Index_Changed"
        AutoPostBack="true">
    </asp:ListBox>
</div>

这个代码隐藏在

之后
protected void Index_Changed(Object sender, EventArgs e)
{
    string text = "", value = "";
    ListBox x = sender as ListBox;
    if (x.ID == "lbRoot")
    {
        text = lbRoot.SelectedItem.Text;
        value = lbRoot.SelectedItem.Value;
    }
    else if (x.ID == "lbEvidence")
    {
        text = lbEvidence.SelectedItem.Text;
        value = lbEvidence.SelectedItem.Value;
    }


    //tried this for fixing the bug

    lbRoot.SelectedIndexChanged -= Index_Changed;
    lbRoot.SelectedIndex = -1;
    lbRoot.SelectedIndexChanged += Index_Changed;

    lbEvidence.SelectedIndexChanged -= Index_Changed;
    lbEvidence.SelectedIndex = -1;
    lbEvidence.SelectedIndexChanged += Index_Changed;

    doStuff(text, value);

}

0 个答案:

没有答案