Solved

ASP.NET Listbox Not Recognizing MyListBox.Selected Value

Posted on 2013-02-03
2
308 Views
Last Modified: 2013-02-03
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
0
Comment
Question by:Dovberman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 20

Accepted Solution

by:
informaniac earned 400 total points
ID: 38848847
if(!IsPostBack)
{
FillKeyWordList()
}
0
 

Author Closing Comment

by:Dovberman
ID: 38849459
Thank you.

I was hoping that I had missed something simple.
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Reactjs with .NET 3 78
using CK editor in iframes 5 28
IDE for Python 5 73
How to downgrade visual studio 2015 enterprise to professional 6 58
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question