我目前面临一个问题.如何从asp.net复选框列表中获取最新的选定值?
从循环选中一个复选框列表的项目,我可以获得最高的选择索引及其值,但不期望用户从下到上依次选择复选框.那么,怎么处理呢?
是否有任何事件捕获系统,将帮助我识别产生事件的确切列表项目?
解决方法
如果我理解正确,这是我使用的代码:
protected void CheckBoxList1_SelectedIndexChanged(object sender,EventArgs e)
{
int lastSelectedIndex = 0;
string lastSelectedValue = string.Empty;
foreach (ListItem listitem in CheckBoxList1.Items)
{
if (listitem.Selected)
{
int thisIndex = CheckBoxList1.Items.IndexOf(listitem);
if (lastSelectedIndex < thisIndex)
{
lastSelectedIndex = thisIndex;
lastSelectedValue = listitem.Value;
}
}
}
}
是否有任何事件捕获系统,将帮助我识别产生事件的确切列表项目?
您使用CheckBoxList的事件CheckBoxList1_SelectedIndexChanged.当单击列表的复选框时,会调用此事件,然后可以检查所需的任何条件.
编辑:
以下代码允许您获取用户选择的最后一个复选框索引.使用这些数据,您可以得到用户最后选择的值.
protected void CheckBoxList1_SelectedIndexChanged(object sender,EventArgs e)
{
string value = string.Empty;
string result = Request.Form["__EVENTTARGET"];
string[] checkedBox = result.Split('$'); ;
int index = int.Parse(checkedBox[checkedBox.Length - 1]);
if (CheckBoxList1.Items[index].Selected)
{
value = CheckBoxList1.Items[index].Value;
}
else
{
}
}
