ListBox SelectedIndex is always -1

Discussion in 'ASP .Net Web Controls' started by b_gul_t, Mar 5, 2005.

  1. b_gul_t

    b_gul_t Guest

    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_gul_t, Mar 5, 2005
    #1
    1. Advertising

  2. b_gul_t

    b_gul_t Guest

    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;

    }
    }
    }



    "Calvin Luttrell" wrote:

    > Bilge,
    >
    > Did the viewstate for the control or form get turned off? You are sure that when the form loads it only rebinds the data to the control when it is not a post back.
    >
    > -Calvin Luttrell
    > ProjectThunder.com, Inc.
    > nothing is impossible
    >
    > > 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
     
    b_gul_t, Mar 7, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Giblin

    Asp:ListBox and selectedindex

    John Giblin, Nov 26, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    1,117
    John Giblin
    Nov 26, 2003
  2. S. Justin Gengo

    ListBox SelectedIndex is always -1

    S. Justin Gengo, Feb 17, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    2,787
  3. =?Utf-8?B?TGll?=
    Replies:
    5
    Views:
    687
    =?Utf-8?B?TGll?=
    Dec 14, 2004
  4. =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?=

    ListBox not returning correct SelectedIndex

    =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?=, Feb 4, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    4,320
    Karl Seguin
    Feb 4, 2005
  5. js
    Replies:
    0
    Views:
    563
Loading...

Share This Page