先看页面代码

  1. <asp:DataList id="DataList1" runat="server" Width="100%" RepeatColumns="4" Font-Size="10pt">
  2. <ItemTemplate>
  3. <TABLE id="Table2" cellSpacing="0" cellPadding="0" width="100%" bgColor="gainsboro" border="0">
  4. <TR>
  5. <TD align="center">
  6. <asp:Label id=lblOName runat="server" Font-Size="10pt" Text='<%# DataBinder.eval_r(Container.DataItem,"nmenu_name")%>'>
  7. </asp:Label>
  8. <asp:Label id=lblID runat="server" Font-Size="10pt" Text='<%# DataBinder.eval_r(Container.DataItem,"nmenu_id")%>' Visible="False">
  9. </asp:Label></TD>
  10. </TR>
  11. <TR>
  12. <TD align="center">
  13. <asp:CheckBoxList id="cbSelect" runat="server" Font-Size="10pt" RepeatColumns="2" Width="100%" BackColor="#FFE0C0"
  14. RepeatDirection="Horizontal" DataTextField="nmenu_tname" DataValueField="nmenu_tid"></asp:CheckBoxList></TD>
  15. </TR>
  16. </TABLE>
  17. </ItemTemplate>
  18. </asp:DataList>

后台绑定CheckBox:

  1. private void Page_Load(object sender, System.EventArgs e)
  2. {
  3. // 在此处放置用户代码以初始化页面
  4. if(!IsPostBack)
  5. {
  6. if(Session["UID"]!=null&&Session["UID"].ToString()!="")
  7. {
  8. BindList();
  9. BindCheckBox();
  10. }
  11. else
  12. {
  13. Server.Transfer("error.aspx");
  14. }
  15. }
  16. }
  17. private void BindList()
  18. {
  19. string sql = "select * from NewsOne";
  20. DataList1.DataSource = ort.ExecuteToTable(sql);
  21. DataList1.DataBind();
  22. }
  23. private void BindCheckBox()
  24. {
  25. foreach(DataListItem dli in DataList1.Items)
  26. {
  27. CheckBoxList cbl = (CheckBoxList)dli.FindControl("cbSelect");
  28. Label lbl = (Label)dli.FindControl("lblID");
  29. string sql = "select * from NewsTwo where nmenu_oid="+lbl.Text;
  30. cbl.DataSource = ort.ExecuteToTable(sql);
  31. cbl.DataBind();
  32. }
  33. }

CheckBox取值:

  1. string list = "";
  2. foreach(DataListItem dli in DataList1.Items)
  3. {
  4. CheckBoxList cbSel = (CheckBoxList)dli.FindControl("cbSelect");
  5. for(int i=;i<cbSel.Items.Count;i++)
  6. {
  7. if(cbSel.Items[i].Selected == true)
  8. {
  9. list += cbSel.Items[i].Value + ",";
  10. }
  11. }
  12. }
  13. list = list.Trim(",".ToCharArray());

有的时候会出现取不到值的情况,我的看法是缺少if(!IsPostBack)。

DataList、Repeater、GridView中的Checkbox取值问题的更多相关文章

  1. 转:GridView中RowDataBound的取值

    GridView是ASP.NET中功能强大的数据显示控件,它的RowDataBound事件为我们提供了方便的控制行.列数据的途径. 要获取当前行的某个数据列,我在实践中总结有如下几种方法: 1. Ce ...

  2. aspx中的checkbox 取值问题

    问题:前台checkbox控件,选中值为1,不选值为0: 解决方案: 插入一行 <input type="hidden" name="RemberPwd" ...

  3. jQuery radio取值,checkbox取值,select取值

    语法解释: $("#select_id").change(function(){//code...}); //为Select添加事件,当选择其中一项时触发 var checkTex ...

  4. jquery ajax post 传递数组 ,多checkbox 取值

    jquery ajax post 传递数组 ,多checkbox 取值 http://w8700569.iteye.com/blog/1954396 使用$.each(function(){});可以 ...

  5. jquery radio取值,checkbox取值,select取值,radio选中,checkbox选中,select选中

    jQuery获取Select选择的Text和Value: 语法解释: 1. $("#select_id").change(function(){//code...}); //为Se ...

  6. easyui 》 radio取值,checkbox取值,select取值,radio选中,checkbox选中,select选中

    获取一组radio被选中项的值var item = $('input[@name=items][@checked]').val();获取select被选中项的文本var item = $(" ...

  7. jquery radio取值,checkbox取值,select取值及选中

    jquery radio取值,checkbox取值,select取值,radio选中,checkbox选中,select选中,及其相关 获取一组radio被选中项的值 var item = $('in ...

  8. GridView中实现CheckBox的全选

    GridView中实现CheckBox的全选 用服务器端的方法: 在页面上放一个gridview控件,配置好数据源,编辑列, <asp:GridView ID="GridView1&q ...

  9. C#中float的取值范围和精度

    原文:C#中float的取值范围和精度 float类型的表现形式: 默认情况下,赋值运算符右侧的实数被视为 double. 因此,应使用后缀 f 或 F 初始化浮点型变量,如以下示例中所示: floa ...

随机推荐

  1. 重写OnPaint事件对窗体重绘 实例1

    public class WindowOne:Form { public WindowOne() { InitializeCompnent(); } public void InitializeCom ...

  2. First 5 minutes of SQLite

    What is SQLite? SQLite is light-weight RDBMS, it is use the file system rather than the C/S client, ...

  3. Eclipse MAT: Understand Incoming and Outgoing References

    引用:http://xmlandmore.blogspot.hk/2014/01/eclipse-mat-understand-incoming-and.html?utm_source=tuicool ...

  4. windows下配置svn的https访问

    svn是一个功能强大的代码版本管理系统,可以将服务端安装在linux.unix以及windows下.svn通常采用http方式进行代码提交与下载.由于密码采用明文传输,因此存在泄密的风险.若采用htt ...

  5. Python标准库内置函数——hasattr

    hasattr(object, name): # object 对象 name 特征名称 判断对象object是否包含名为name的特性(hasattr是通过调用getattr(ojbect, nam ...

  6. ognl.NoSuchPropertyException(没有对应属性异常)

    ognl.NoSuchPropertyException: com.xie.struts2.tags.modal.Student.sName(没有对应属性异常) at ognl.ObjectPrope ...

  7. 【笔记】Unix 平台标准

    POSIX 表示可移植操作系统接口(Portable Operating System Interface ,缩写为 POSIX ),POSIX标准定义了操作系统应该为应用程序提供的接口标准,是IEE ...

  8. TypeScript开发Vue

    用TypeScript开发Vue——如何通过vue实例化对象访问实际ViewModel对象 目录 背景 解决方案 关于Vue中的计算属性类型 TypeScript的强制类型声明语法 强制类型声明的局限 ...

  9. OC和C语言的混编注意点和好处

    苹果的Objective-C编译器批准用户在统一个源文件里自由地混杂利用C++和Objective-C,混编后的语言叫Objective-C++.有了它,你就能够在Objective-C利用过程中利用 ...

  10. ftp二进制与ascii传输方式区别

    ASCII 和BINARY模式区别:    用HTML 和文本编写的文件必须用ASCII模式上传,用BINARY模式上传会破坏文件,导致文件执行出错.    BINARY模式用来传送可执行文件,压缩文 ...