?
Solved

ASP.NET Listbox Not Recognizing MyListBox.Selected Value

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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

764 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