ListBox SelectedIndex is always -1

B

b_gul_t

Hi,

I have a simple web page containing a listbox, a button and a label. I load
the listbox items in codebehind such as this:
Page_load
{
if (!page.ispostback)
{
filllistbox()
}
}

When a list item is selected and the button is pressed, I want to display
the selected item in the label. The problem is, when the button is pressed
all the values in the listbox are lost, and the selectedindex is -1. Why does
it happen, how can I persist the selectedindex value? I am stuck at this for
several days, any help will be appreciated.

Thanks,
Bilge Gul
 
B

b_gul_t

EnableViewState property of the listbox, the button, the label and the
document are set to True. The webform(System.Web.UI.Page) does not have this
property, is this normal? Can a build setting be the reason of the behavior
of the code?

The code is pretty small, so I'll paste it here.
Thanks in advance,
Bilge Gul

The aspx file:

<%@ Page language="c#" Codebehind="WebForm2.aspx.cs" AutoEventWireup="true"
Inherits="UpgradeTest.WebForm2" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>

</HEAD>
<body>
<h3>ListBox Example</h3>
<form runat="server" ID="Form1" method="post">
<asp:ListBox id="ListBox1" Rows="6" Width="100px" SelectionMode="Single"
runat="server" EnableViewState="true"></asp:ListBox>
<asp:button id="Button1" Text="Submit" OnClick="Button1_Click"
runat="server" EnableViewState="true" />
<asp:Label id="Label1" Font-Name="Verdana" Font-Size="10pt"
runat="server" EnableViewState="true" />
</form>
</body>
</HTML>

The aspx.cs file:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace UpgradeTest
{
/// <summary>
/// Summary description for WebForm2.
/// </summary>
public class WebForm2 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.ListBox ListBox1;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Label Label1;

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if (!this.IsPostBack)
{
ListBox1.Items.Add("1");
ListBox1.Items.Add("2");
ListBox1.Items.Add("3");
ListBox1.Items.Add("4");
}
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

protected void Button1_Click(object sender, System.EventArgs e)
{

if (ListBox1.SelectedIndex > -1)
Label1.Text="You chose: " + ListBox1.SelectedItem.Text;

}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top