Dovberman
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_SelectedIndexC hanged">
</asp:ListBox>
<br />
</div>
</form>
</body>
</html>
Code behind:
private void FillKeyWordList()
{
lstKeyword.Items.Clear();
string selectSQL = "SELECT KeyWordID, Keyword + ' - ' + CAST(Keywords.MonthlySearc hes AS varchar(100)) AS Info FROM Keywords Keywords WHERE IsActive = 1 ORDER BY keyword ";
string strConnection = connString.ToString();
SqlConnection conStockSelect = new SqlConnection(strConnectio n);
// 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_SelectedIndexCh anged(obje ct 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
<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_SelectedIndexC
</asp:ListBox>
<br />
</div>
</form>
</body>
</html>
Code behind:
private void FillKeyWordList()
{
lstKeyword.Items.Clear();
string selectSQL = "SELECT KeyWordID, Keyword + ' - ' + CAST(Keywords.MonthlySearc
string strConnection = connString.ToString();
SqlConnection conStockSelect = new SqlConnection(strConnectio
// 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_SelectedIndexCh
{
// 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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I was hoping that I had missed something simple.