ASP.NET中获取CheckBoxList的当前选择项
2008-08-27 17:45
691 查看
CheckBoxList中有多个项,当选择/不选择某项时如果其AutoPostBack为True,则会触发SelectedIndexChanged,但是CheckBoxList及其Items属性都没有直接能获取当前选择的项的属性,想了一下,可以先将上一次的勾选状态存到ViewState中,在触发SelectedIndexChanged的时候进行比较,具体代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:CheckBoxList ID="CheckBoxList1" runat="server" AutoPostBack="True"
onprerender="CheckBoxList1_PreRender"
onselectedindexchanged="CheckBoxList1_SelectedIndexChanged"
RepeatDirection="Horizontal">
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem Selected="True">3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
<asp:ListItem>5</asp:ListItem>
</asp:CheckBoxList>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Dictionary<int, bool> dic = new Dictionary<int, bool>();
for (int i = 0; i < CheckBoxList1.Items.Count; i++)
dic.Add(i, CheckBoxList1.Items[i].Selected);
if (ViewState["cblChecked"] == null)
ViewState["cblChecked"] = dic;
}
protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (ViewState["cblChecked"] != null)
{
Dictionary<int, bool> dic = ViewState["cblChecked"] as Dictionary<int,bool>;
for (int i = 0; i < CheckBoxList1.Items.Count; i++)
{
if (dic[i] != CheckBoxList1.Items[i].Selected)
Response.Write("当前操作项为:" + i.ToString());
dic[i] = CheckBoxList1.Items[i].Selected;
}
ViewState["cblChecked"] = dic;
}
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:CheckBoxList ID="CheckBoxList1" runat="server" AutoPostBack="True"
onprerender="CheckBoxList1_PreRender"
onselectedindexchanged="CheckBoxList1_SelectedIndexChanged"
RepeatDirection="Horizontal">
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem Selected="True">3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
<asp:ListItem>5</asp:ListItem>
</asp:CheckBoxList>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Dictionary<int, bool> dic = new Dictionary<int, bool>();
for (int i = 0; i < CheckBoxList1.Items.Count; i++)
dic.Add(i, CheckBoxList1.Items[i].Selected);
if (ViewState["cblChecked"] == null)
ViewState["cblChecked"] = dic;
}
protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (ViewState["cblChecked"] != null)
{
Dictionary<int, bool> dic = ViewState["cblChecked"] as Dictionary<int,bool>;
for (int i = 0; i < CheckBoxList1.Items.Count; i++)
{
if (dic[i] != CheckBoxList1.Items[i].Selected)
Response.Write("当前操作项为:" + i.ToString());
dic[i] = CheckBoxList1.Items[i].Selected;
}
ViewState["cblChecked"] = dic;
}
}
}
}
相关文章推荐
- 用JavaScript获取Asp.net服务器端控件CheckBoxList的选中值数组
- 用JavaScript获取Asp.net服务器端控件CheckBoxList的选中值数组
- ASP.NET DEMO18 验证GridView中由DropDownList控制CheckBoxList的选择项个数
- asp.net绑定checkboxlist--------jquery绑定click事件,获取checkbox的value和显示值
- 用JavaScript获取Asp.net服务器端控件CheckBoxList的选中值数组
- 用JavaScript获取Asp.net服务器端控件CheckBoxList的选中值数组
- Asp.Net中在客户端判断checkboxlist是否选择方法
- Asp.net js获取CheckBoxlist 和 Radiobuttonlist
- [asp.net] 设置与获取CheckBoxList多选的值
- 获取CheckBoxList当前选择项索引
- 如何获取CheckBoxList当前选择个数?
- Jquery 获取Asp.Net CheckBoxList的Value,Text
- asp.net 中如何获取CheckBoxList的值
- asp.net Javascript获取CheckBoxList的value
- ASP.NET绑定CHECKBOXLIST--------JQUERY绑定CLICK事件,获取CHECKBOX的VALUE和显示值
- asp.net绑定checkboxlist--------jquery绑定click事件,获取checkbox的value和显示值
- ASP.NET 后台设置CheckBoxList不可选择。
- ASP.NET 获取多个 checkbox,radio 被选择的值
- 用JavaScript获取Asp.net服务器端控件CheckBoxList的选中值数组
- 如何获取CheckBoxList当前选择个数?