0
votes

There is a nested repeater in my work. I'm responsible to add asp:checkbox on every underneath repeater item, so that we can control each item by checkbox.

The markup code is:

<ItemTemplate>
  <li class="<%# GetCategoryClass(Container.DataItem) %> cl-li">    
    <asp:CheckBox runat="server" ID="checkBox" AutoPostBack="true" OnCheckedChanged="CheckedChanged" />
    <a class="cl-a excludeLink" visible='<%# GetCategoryClass(Container.DataItem) == "DISPLAYED" %>' href="<%# GetExcludeCategoryCommand(Container.DataItem) %>" runat="server" id="butExclude">x</a>
    <a class="cl-a" href="<%# GetCategoryCommand(Container.DataItem) %>" runat="server" id="butCategory"><%# GetCategoryTitle(Container.DataItem, true) %></a>
  </li>
</ItemTemplate>

The c# code is:

protected void CheckedChanged(object obj, EventArgs e)
{
    // some works    
}

I start to work on this problem and find the CheckedChanged function isn't fired via debugging. But, when I add a line in

if(IsPostBack){
   CheckedChanged(sender, e);  //add
}

It works and goes into CheckedChanged function when I debug. I have read many articles, none of them say I need to add that line in IsPostBack block. Is there anyone can show me the principle of that?

1
You say you are "adding" asp:checkbox to each repeater item. Does this mean you are doing this programmatically? How is that happening. How is the markup you show us here added to the repeater items? Or is it already there in the item template? - user4843530
@GiliusMaximus I changed my markup code, just add a checkbox control. - Xiufeng Chen
I think I found a away to resolve it, but thanks to everyone following this question. The solution: stackoverflow.com/questions/9527908/… looks good for me. - Xiufeng Chen

1 Answers

4
votes

Your code should work. It depends how you wrote it.

I made a example which works :

Default.aspx :

<table>
<asp:Repeater ID="RepeaterCB" runat="server">
<ItemTemplate>
     <tr>
         <td><%# Container.DataItem %></td>
         <td><asp:CheckBox runat="server" OnCheckedChanged="OnCheckedChange" AutoPostBack="true"/></td>
      </tr>
</ItemTemplate>
</asp:Repeater>
</table>

Default.aspx.cs :

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        RepeaterCB.DataSource = new List<string> { "tom", "fred", "pijule" };
        RepeaterCB.DataBind();
    }

}

protected void OnCheckedChange(object sender, EventArgs e)
{
    Response.Write("<script>alert('Fire');</script>");
}

This code fires an alert each time I check or uncheck a textbox.

Hope it helps.