Solved

ASP.NET Listbox Not Recognizing MyListBox.Selected Value

Posted on 2013-02-03
2
291 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
2 Comments
 
LVL 20

Accepted Solution

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

Author Closing Comment

by:Dovberman
Comment Utility
Thank you.

I was hoping that I had missed something simple.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now