复选框的OnClick / ItemCommand在中继器或DataList控件 [英] Checkbox OnClick/ItemCommand in Repeater or DataList
本文介绍了复选框的OnClick / ItemCommand在中继器或DataList控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要做一些服务器端逻辑上排在我的中继器点击一个复选框Repeater控件内部时。
I need to do some server side logic on a row in my repeater when a CheckBox is clicked inside the repeater control.
任何人都知道如何去吗?
我看到它你不能火项命令,如果你使用的复选框的OnClick你不能得到中继行的方式。
The way I see it you cant fire item command and if you use the CheckBoxes OnClick you cant get the repeater row.
推荐答案
下面是我所如何完成过类似的快速实体模型。
Here is a quick mock-up of how I have done similar in the past.
<asp:Repeater id="repeater1" runat="server" OnItemDataBound="repeater1_OnItemDataBound" >
<ItemTemplate>
<asp:CheckBox ID="chk" runat="server" OnCheckedChanged="Check_Changed" AutoPostBack="true" />
</ItemTemplate>
</asp:Repeater>
codebehind:
codebehind:
public class Model {
public int Id { get; set; }
public string Name { get; set; }
}
public partial class Checkboxes : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) {
if(!IsPostBack ) {
repeater1.DataSource = new List<Model> {
new Model { Id = 1, Name = "a" },
new Model { Id = 2, Name = "b" },
new Model { Id = 3, Name = "c" } };
repeater1.DataBind();
}
}
protected void repeater1_OnItemDataBound(Object sender, RepeaterItemEventArgs e) {
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
var item = e.Item.DataItem as Model;
if (item != null) {
var chk = e.Item.FindControl("chk") as CheckBox;
if (chk != null) {
chk.Text = item.Name;
chk.InputAttributes.Add("value", item.Id.ToString());
}
}
}
}
protected void Check_Changed(Object sender, EventArgs e) {
var id = ((CheckBox) sender).InputAttributes["value"];
//you now have access to the item id and can manipulate at will.
}
}
这篇关于复选框的OnClick / ItemCommand在中继器或DataList控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文