Link to home
Start Free TrialLog in
Avatar of Dovberman
DovbermanFlag for United States of America

asked on

ASP.NET Listbox Not Recognizing MyListBox.Selected Value

Listbox getes populated and appears to work well.

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
         <br />
                    <asp:ListBox ID="lstKeyword" runat="server" AutoPostBack="True" Height="260px"
                        Width="2.5in" BackColor="Azure"  
                        OnSelectedIndexChanged = "lstKeyword_SelectedIndexChanged">
                    </asp:ListBox>
                    <br />
   
    </div>
    </form>
</body>
</html>

Code behind:

private void FillKeyWordList()
    {
        lstKeyword.Items.Clear();

         string selectSQL = "SELECT KeyWordID, Keyword + ' - ' + CAST(Keywords.MonthlySearches AS varchar(100)) AS Info FROM Keywords Keywords WHERE IsActive = 1 ORDER BY keyword ";
       
        string strConnection = connString.ToString();

        SqlConnection conStockSelect = new SqlConnection(strConnection);

        // Try to open the connection.
        conStockSelect.Open();

        SqlCommand cmd = new SqlCommand(selectSQL, conStockSelect);

        lstKeyword.DataSource = cmd.ExecuteReader();
        lstKeyword.DataTextField = "Info";
        lstKeyword.DataValueField = "KeyWordID";
        lstKeyword.DataBind();
        conStockSelect.Close();
        cmd.Dispose();
    }

// Get listbox value

  protected void lstKeyword_SelectedIndexChanged(object sender, System.EventArgs e)
    {
        // Get the currently selected item in the ListBox.
// Row appears to be selected by clicking.

        int intSelected = lstKeyword.SelectedIndex;
        intSelected = lstKeyword.SelectedIndex; // Returns -1, row not selected.
       
        lstKeyword.SelectedIndex = 5;  // The selection can be forced
ListBoxBroken.jpg
ASKER CERTIFIED SOLUTION
Avatar of Obadiah Christopher
Obadiah Christopher
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Dovberman

ASKER

Thank you.

I was hoping that I had missed something simple.