Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is what I came up with, which is basically your option #2. In the <code>ItemTemplate</code> of the repeater, I use a Literal control (Visible set to false) which has the argument you wish to pass to the <code>CheckedChanged</code> function. The reason for using a control is because the control will retain its value in the ViewState after a post back, whereas the original data source for the Repeater will be lost.</p> <p>In the <code>OnItemCreated</code> function, I bind the <code>CheckChanged</code> function for all of the check boxes to pass in the right argument. Here's my complete example. In this case, I want to pass the <code>Id</code> property of my data to the <code>CheckChanged</code> function.</p> <p>Markup:</p> <pre><code>&lt;asp:Repeater ID="Repeater1" runat="server" OnItemCreated="ItemCreated"&gt; &lt;ItemTemplate&gt; &lt;asp:Literal ID="litArg" runat="server" Visible="false" Text='&lt;%# Eval("Id") %&gt;'&gt; &lt;/asp:Literal&gt;&lt;%# Eval("Name") %&gt; &lt;asp:CheckBox ID="chkCool" runat="server" AutoPostBack="true" Checked='&lt;%# Eval("IsCool") %&gt;' /&gt;&lt;br /&gt; &lt;/ItemTemplate&gt; &lt;/asp:Repeater&gt; </code></pre> <p>Code behind:</p> <pre><code>public class SomeClass { public SomeClass(bool c, string n, int id) { IsCool = c; Name = n; Id = id; } public bool IsCool { get; set; } public string Name { get; set; } public int Id { get; set; } } . . . protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { List&lt;SomeClass&gt; people = new List&lt;SomeClass&gt;(); people.Add(new SomeClass(true, "Will", 666)); people.Add(new SomeClass(true, "Dan", 2)); people.Add(new SomeClass(true, "Lea", 4)); people.Add(new SomeClass(false, "Someone", 123)); Repeater1.DataSource = people; Repeater1.DataBind(); } } private void CheckChanged(int id) { Response.Write("CheckChanged called for item #" + id.ToString()); } protected void ItemCreated(object sender, RepeaterItemEventArgs e) { //this needs to be set again on post back CheckBox chk = (CheckBox)e.Item.FindControl("chkCool"); Literal arg = (Literal)e.Item.FindControl("litArg"); Action&lt;object, EventArgs&gt; handler = (s, args) =&gt; CheckChanged(Convert.ToInt32(arg.Text)); chk.CheckedChanged += new EventHandler(handler); } </code></pre> <p>Hope that helps.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload