views:

234

answers:

1

Hi I have a repeater and an unbound checkbox in each item. I want to do something for items that are checked. but the problem is here! when I click on my button outside the repeater, just my page refreshes and nothing happens.

these are my code :

<asp:Repeater ID="Repeater1" runat="server" >    
           <ItemTemplate>
               <asp:CheckBox ID="ChBox" runat="server" />
               <asp:Label ID="rptBody" runat="server" Text='<%#Eval("subject") %>
               <hr />
           </ItemTemplate>
    </asp:Repeater>
    <asp:Button ID="btnDelete" runat="server" Text="Delete" Width="90px"     onclick="btnDelete_Click" />

code in cs file :

  protected void btnDelete_Click(object sender, EventArgs e)
        {
             foreach (RepeaterItem item in Repeater1.Items)
            {
                CheckBox ch = item.FindControl("ChBox") as CheckBox;
               if (ch.Checked)
                {
                   ch.Text = "IT is selected now";
                }
            }
        }
+2  A: 

I think what you are missing is, you should bind or set data source in when you page is not postback

if (!Page.IsPostBack)
    {
        rpt.DataSourceID = "";
        rpt.DataBind();
    }

I think What is happening in your situation is, on postback your repeater is rebinded.

Muhammad Akhtar
thank you. it worked
LIX