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.html
[解决办法]
http://topic.csdn.net/u/20081106/15/de43d6b2-9ac6-4a75-b39b-8e29a0d31b77.html
[解决办法]
//批量删除 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);
}
}
bool b = fbll.DeleteList(idList);
Server.Transfer("featured.aspx");
}
[解决办法]
如果Page_Load没有PostBack回发判断 ,当点击按钮的时候checkbox会被重新加载,你就去不到选中的值了
[解决办法]
不好意思看错了
是repeater的话就是,你就在每列的里面加个Literal绑同样的字段名
<asp:Literal id="Literal1" runat="server" Text='<%#Eval("字段名") %>' Visable="false"></Literal>
protected void Button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < Repeater1.Rows.Count; i++)
{
CheckBox cb = Repeater1.Items[i].FindControl("CheckBox1") as CheckBox;
Literal lt = Repeater1.Items[i].FindControl("Literal1") as Literal;
if (cb.Checked)
{
string a = lt.Text;
.....
}
}