解决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"];

  ...

  }

posted @ 2011-08-24 11:18  茶海  阅读(866)  评论(0编辑  收藏  举报