locked
ASP ListBox wrong ID RRS feed

  • Question

  • User-902956371 posted

    So I have this 2 ListBox, everything works if I use only the second one, the problems start after clicking the first one, happens that the next time I click the second, it gets the ID of the firstone...

    so..

    click 2 -> Works

    click 2 -> Works

    click 1 -> works

    click 1 -> works

    click 2 -> I get the ID of the first one....

    and I have this problem with all the browsers...

    <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>

    with this code behind

    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);
    
    }

     

    Tuesday, November 25, 2014 7:40 PM

Answers

  • User-902956371 posted

    Well.. I fixed my problem in a dirty way... 

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Session["rootID"] = -1;
            Session["evidenceID"] = -1;
        }
    }

     and in the event

    protected void Index_Changed(Object sender, EventArgs e)
    {
        string text = "", value = "";
        int r = int.Parse(Session["rootID"].ToString());
        int ev = int.Parse(Session["evidenceID"].ToString());
    
        if ( r != lbRoot.SelectedIndex)
        {
            Session["rootID"] = lbRoot.SelectedIndex;
            text = lbRoot.SelectedItem.Text;
            value = lbRoot.SelectedItem.Value;
        }
        else if (ev != lbEvidence.SelectedIndex)
        {
            Session["evidenceID"] = lbEvidence.SelectedIndex;
            text = lbEvidence.SelectedItem.Text;
            value = lbEvidence.SelectedItem.Value; ;
        }
    
        doStuff(text, value);
    }

    I tried ViewState instead of Session.... but for a weird reason, the ViewState was losing the values going back to -1... 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 26, 2014 10:52 AM

All replies

  • User-1716253493 posted
    protected void Index_Changed(Object sender, EventArgs e)
    {
        string text = "", value = "";
        ListBox x = sender as ListBox;
        text = x.SelectedItem.Text;
        value = x.SelectedItem.Value;
        doStuff(text, value);
    }

    Tuesday, November 25, 2014 8:32 PM
  • User-1716253493 posted

    How you populate the ListBox Items?

    Seem like when the ListBox indexchanged causing another ListBox indexchanged

    For example changing category can cause subcategory selectedindexchanged event

    Tuesday, November 25, 2014 8:48 PM
  • User-902956371 posted

    haha I like your modification here, but still the same problem. Click lb2 and lb1 is the one that is sent.

    and yes, I tried with an event for each lb, and after clicking lb2, event of lb1 was triggered.

    Wednesday, November 26, 2014 9:24 AM
  • User-902956371 posted

    Well.. I fixed my problem in a dirty way... 

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Session["rootID"] = -1;
            Session["evidenceID"] = -1;
        }
    }

     and in the event

    protected void Index_Changed(Object sender, EventArgs e)
    {
        string text = "", value = "";
        int r = int.Parse(Session["rootID"].ToString());
        int ev = int.Parse(Session["evidenceID"].ToString());
    
        if ( r != lbRoot.SelectedIndex)
        {
            Session["rootID"] = lbRoot.SelectedIndex;
            text = lbRoot.SelectedItem.Text;
            value = lbRoot.SelectedItem.Value;
        }
        else if (ev != lbEvidence.SelectedIndex)
        {
            Session["evidenceID"] = lbEvidence.SelectedIndex;
            text = lbEvidence.SelectedItem.Text;
            value = lbEvidence.SelectedItem.Value; ;
        }
    
        doStuff(text, value);
    }

    I tried ViewState instead of Session.... but for a weird reason, the ViewState was losing the values going back to -1... 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 26, 2014 10:52 AM