当前位置: 代码迷 >> ASP.NET >> ASP.NET 后台怎样获取repeater控件中选中行(checkbox选择)的数据!
  详细解决方案

ASP.NET 后台怎样获取repeater控件中选中行(checkbox选择)的数据!

热度:3565   发布时间:2013-02-25 00:00:00.0
ASP.NET 后台怎样获取repeater控件中选中行(checkbox选择)的数据!在线等!
详细的问题是:在一个repeater控件中,每行(数据)有一个checkbox子控件(标记需要选择操作的数据行),现在的问题是,单击一个repeater控件外的一个button,来获取checkbox选中行的数据。前台代码(.aspx)要怎么写,后台(C#)要怎么写。

------解决方案--------------------------------------------------------
如果有翻页的话 每次选中checkbox把ID记在cookies里吧 
如果没有 后台随便弄个list啥的存放ID呗
------解决方案--------------------------------------------------------
refer:
http://www.cnblogs.com/insus/archive/2011/09/07/2169742
------解决方案--------------------------------------------------------
http://topic.csdn.net/u/20081106/15/de43d6b2-9ac6-4a75-b39b-8e29a0d31b77
------解决方案--------------------------------------------------------
C# code
//批量删除        protected void btnAll_Click(object sender, ImageClickEventArgs e)        {            int items = rptCart.Items.Count;            string[] goodsid = new string[99];            for (int i = 0; i < items; i++)            {                bool flag = (rptCart.Items[i].FindControl("checkgood") as CheckBox).Checked;                if (flag == true)                {                    goodsid[i] = (rptCart.Items[i].FindControl("btnDelete") as ImageButton).CommandArgument;                }            }            for (int j = 0; j < goodsid.Length; j++)            {                if (goodsid[j] != null)                {                    DeleteGood(goodsid[j].ToString());                }            }        }
------解决方案--------------------------------------------------------
获取选中checkbox的记录主要看红色标记部分,不要忘了在Page_Load事件里加IsPostBack判断

前台aspx页面: 一个checkbox , 一个Label记录该条记录的ID
<asp:Repeater ID="Repeater2" runat="server">
<HeaderTemplate><ul class="ul_company"></HeaderTemplate>
<ItemTemplate><li style="margin:3px">
<asp:Label ID="Label2" runat="server" Text=<%#Eval("ID") %> Visible="false"></asp:Label> <asp:CheckBox ID="CheckBox1" runat="server" /><%#Eval("CompanyName") %></li></ItemTemplate> <FooterTemplate></ul></FooterTemplate>
</asp:Repeater>
后台cs代码:

 按钮单击事件
protected void Button3_Click(object sender, EventArgs e)
{
CheckBox CheckBox2;
string idList = "";//用来存放选中的多条记录的ID

for (int i = 0; i < Repeater2.Items.Count; i++)
{
CheckBox2 = (CheckBox)Repeater2.Items[i].FindControl("CheckBox1");
if (CheckBox2.Checked == true)
{
//如果选中 就取得该行的Label2标签的值,也就是该记录的ID
Label lbId = (Label)Repeater2.Items[i].FindControl("Label2");


idList = idList.Trim() + lbId.Text.ToString() + ",";

}



}
if (idList.Substring(idList.Length - 1) == ",")
{

idList = idList.Substring(0, idList.Length - 1);
}
string[] num = idList.Split(',');
for (int j = 0; j < num.Length; j++)
{

long id = Convert.ToInt64(num[j]);
string strWhere = "FeaturedId=" + id;
List<CityHall.Model.T_Products> products = pbll.GetModelList(strWhere);
foreach (CityHall.Model.T_Products t_product in products)
{
t_product.FeaturedId = null;
pbll.Update(t_product);
  相关解决方案