解决asp.net中listbox的SelectedIndex的取值为-1

 

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

...

                string[] maindir = stypes.Split(new char[] { ',' });
                for (int i = 0; i < maindir.Length; i++)
                {
                    lbclasss.Items.Add(new ListItem(maindir[i]));
                    }

...

}

//lbclasss.SelectedIndex 每次取值都为-1

        protected void btntypedel_Click(object sender, EventArgs e)
        {
            if (lbclasss.SelectedIndex > -1)
            {
                lbclasss.Items.RemoveAt(lbclasss.SelectedIndex);
            }
        }

 

解决方法:

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

            lbclasss.SelectedValue = Request["lbclasss"];

  ...

  }

你可能感兴趣的:(asp.net)