我使用列表框以字符串形式查看数据库的某些记录。但现在有两个不同的问题: 1.选择更改不会通过双击或单击调用,只会在您选择项目并按返回时调用。 2.即使选择更改调用,selectedIndex返回-1,选中的值为null。 ASP代码:
<form runat="server">
<div class="clearfix">
<asp:ListBox runat="server" ID="g1SeasonsListView" CssClass="adminSeasonsListView" OnSelectedIndexChanged="g1SeasonsListView_SelectedIndexChanged" />
<asp:ListBox runat="server" ID="g2SeasonsListView" CssClass="adminSeasonsListView" />
<asp:ListBox runat="server" ID="g3SeasonsListView" CssClass="adminSeasonsListView" />
</div>
<div class="clearfix">
<asp:Button ID="g1DeleteBtn" Text="Delete" runat="server" OnClick="g1DeleteBtn_Click" CssClass="adminDeleteBtn" />
<asp:Button ID="g2DeleteBtn" Text="Delete" runat="server" OnClick="g2DeleteBtn_Click" CssClass="adminDeleteBtn" />
<asp:Button ID="g3DeleteBtn" Text="Delete" runat="server" OnClick="g3DeleteBtn_Click" CssClass="adminDeleteBtn" />
</div>
</form>
我在VS2013中通过观察窗检查的Cs代码并没有什么特别之处:
protected void g1DeleteBtn_Click(object sender, EventArgs e)
{
}
protected void g2DeleteBtn_Click(object sender, EventArgs e)
{
}
protected void g3DeleteBtn_Click(object sender, EventArgs e)
{
}
protected void g1SeasonsListView_SelectedIndexChanged(object sender, EventArgs e)
{
}
当我想更新列表框时,我使用:
private void UpdateListeView()
{
List<string> seasonsListTemp = null;
g1SeasonsListView.Items.Clear();
seasonsListTemp = (from s in database.Gs where s.grade == 1 select s.season).ToList();
for (int i = 0; i < seasonsListTemp.Count; i++)
g1SeasonsListView.Items.Add(seasonsListTemp[i]);
g2SeasonsListView.Items.Clear();
seasonsListTemp = (from s in database.Gs where s.grade == 2 select s.season).ToList();
for (int i = 0; i < seasonsListTemp.Count; i++)
g2SeasonsListView.Items.Add(seasonsListTemp[i]);
g3SeasonsListView.Items.Clear();
seasonsListTemp = (from s in database.Gs where s.grade == 3 select s.season).ToList();
for (int i = 0; i < seasonsListTemp.Count; i++)
g3SeasonsListView.Items.Add(seasonsListTemp[i]);
}
当我按下删除按钮时,我甚至尝试获取所选索引,但结果是相同的。
答案 0 :(得分:0)
将您的代码绑定在!Page.IsPostBack块中。确保代码不会在回发时执行。如果你在PostBack上绑定ListView,你将失去GridView的所有状态,包括被触发的事件,SelectedIndex等。
获取一个值,该值指示是否为该页面呈现页面 第一次或正在加载以响应回发。
if(!Page.IsPostBack)
{
UpdateListeView();
}
通常控件是在Page_Load方法中绑定的,你可能需要在那里检查Page.IsPostBack。