Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

ASP.NET Listbox Not Recognizing MyListBox.Selected Value

Posted on 2013-02-03
2
Medium Priority
?
318 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 1600 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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

618 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