今天绑定ListBox出现了一个有趣的问题。
如:这个ListBox正常情况下可以显示3行数据。多余的行需要下拉才可以显示。
现在数据库有这些数据。
11111
22222
33333
44444
55555
66666
当正常绑定以后:
ListBox显示为:
11111
22222
33333(其他的下拉才可以看见)
当选择第2行时,因为设置了AutoPostBack,所以会刷新一次。
刷新后。
ListBox行定位发生了改变。
于是成为了:
22222
33333
44444
-----------也就是说。当选择某一行时候。这一行就会置顶。
为什么会这样?
怎么解决这个问题?
--------------------源:
<asp:ListBox ID="ListBox1" runat="server" AutoPostBack="True" Height="396px" OnSelectedIndexChanged="ListBox1_SelectedIndexChanged"
Width="200px"></asp:ListBox>
CS:
SqlDataAdapter da = new SqlDataAdapter(sql, MyConn);
DataSet ds = new DataSet();
da.Fill(ds, "class");
ListBox1.DataSource = ds;
ListBox1.DataTextField = "classname";
ListBox1.DataValueField = "classid";
ListBox1.DataBind();
如:这个ListBox正常情况下可以显示3行数据。多余的行需要下拉才可以显示。
现在数据库有这些数据。
11111
22222
33333
44444
55555
66666
当正常绑定以后:
ListBox显示为:
11111
22222
33333(其他的下拉才可以看见)
当选择第2行时,因为设置了AutoPostBack,所以会刷新一次。
刷新后。
ListBox行定位发生了改变。
于是成为了:
22222
33333
44444
-----------也就是说。当选择某一行时候。这一行就会置顶。
为什么会这样?
怎么解决这个问题?
--------------------源:
<asp:ListBox ID="ListBox1" runat="server" AutoPostBack="True" Height="396px" OnSelectedIndexChanged="ListBox1_SelectedIndexChanged"
Width="200px"></asp:ListBox>
CS:
SqlDataAdapter da = new SqlDataAdapter(sql, MyConn);
DataSet ds = new DataSet();
da.Fill(ds, "class");
ListBox1.DataSource = ds;
ListBox1.DataTextField = "classname";
ListBox1.DataValueField = "classid";
ListBox1.DataBind();
要达到的目的是。选择前后listbox里面的行定位保持不变。
this.ListBox1.SelectedIndex = 0;
ListBox1_SelectedIndexChanged里加
this.ListBox1.SelectedIndex = 0;
-------------------------------------------------
加了以后。
每选择一次,就会所有位置重置一次。
是否在选择的时候读取他的SelectedIndex 值?选择后再绑定一次??????