qhnokia

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
ValueCheckBox
using System;
using System.ComponentModel;
using System.Security.Permissions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace CustomerControl
{
public class ValuedCheckBox : CheckBox {
[Bindable(
true), Category("Data"), DefaultValue("")]
public object Value
{
get { return ViewState["Value"]; }
set { ViewState["Value"] = value; }
}
}
}

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register TagPrefix="ValueCheckBox" Namespace="CustomerControl" %>

<!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:Repeater ID="Repeater1" runat="server">
        <HeaderTemplate>
             <table border="1">
                <tr>
                    <td>
                        <asp:CheckBox ID="CheckBox1" runat="server" oncheckedchanged="CheckBox2_CheckedChanged" AutoPostBack="true"/></td>
                   <td><b>ID</b></td>
                   <td><b>Name</b></td>
                </tr>
          </HeaderTemplate>

          <ItemTemplate>
             <tr>
                <td><ValueCheckBox:ValuedCheckBox ID="valuecheckbox1" Value='<%# DataBinder.Eval(Container.DataItem, "ID")%>' runat="server"/></td>
                <td> <%# DataBinder.Eval(Container.DataItem, "ID") %></td>
                <td> <%# DataBinder.Eval(Container.DataItem, "Name") %></td>
             </tr>
          </ItemTemplate>

          <FooterTemplate>
             </table>
          </FooterTemplate>

        </asp:Repeater>
        <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
        <br />

        <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
    </div>
    </form>
</body>
</html>

 

后台

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CustomerControl;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ArrayList values = new ArrayList();

            values.Add(new PositionData(1, "Msft"));
            values.Add(new PositionData(2, "Intc"));
            values.Add(new PositionData(3, "Dell"));
            values.Add(new PositionData(4, "Msft"));
            values.Add(new PositionData(5, "Intc"));
            values.Add(new PositionData(6, "Dell"));

            values.Add(new PositionData(7, "Msft"));
            values.Add(new PositionData(8, "Intc"));
            values.Add(new PositionData(9, "Dell"));


            Repeater1.DataSource = values;
            Repeater1.DataBind();           
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Label1.Text = string.Empty;
        foreach (RepeaterItem ri in Repeater1.Items)
        {
            ValuedCheckBox cb = (ValuedCheckBox)ri.FindControl("valuecheckbox1");
            if (cb != null && cb.Checked)
            {

                Label1.Text += cb.Value + "<br/>";
            }
        }
    }
    protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
    {
        foreach (RepeaterItem ri in Repeater1.Items)
        {
            ValuedCheckBox cb = (ValuedCheckBox)ri.FindControl("valuecheckbox1");
            if (cb != null )
            {

                cb.Checked = !cb.Checked;
            }
        }
    }
}

 

实体类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
///Class1 的摘要说明
/// </summary>
public class PositionData
{

private int _id;
private string _name;

public PositionData(int id, string name)
{
this._id = id;
this._name = name;
}

public int ID
{
get
{
return _id;
}
}

public string Name
{
get
{
return _name;
}
}
}

 

posted on 2010-02-25 19:54  其乐无穷  阅读(2766)  评论(1编辑  收藏  举报