Bob Tway
Bob Tway

Reputation: 9603

CheckBox Controls in Repeater not maintaining state

Bit confused about this. I was under the impression that if you added server controls to the ItemTemplate of a repeater then the ID's assigned to those controls would persist across postbacks and the state would be maintained. But it doesn't seem to be happening. Here's my ItemTemplate:

<asp:HiddenField ID="hidPending" runat="server" value="<%# DataBinder.Eval(Container.DataItem, "Id")%>" />
<td class="leftpadd"><uc:restrictedtext ID="uclblCategory" runat="server" Width="125" /></td>
<td style="border-left:1px solid #528ABD;" class="leftpadd"><%# DataBinder.Eval(Container.DataItem, "SelectedOptions")%></td>
<td style="border-left:1px solid #528ABD;" class="leftpadd"><%# DataBinder.Eval(Container.DataItem, "Price.IncludingTax", "{0:C}")%></td>
<td style="border-left:1px solid #528ABD;" class="leftpadd"><%# DataBinder.Eval(Container.DataItem, "ExtrasCost", "{0:C}")%></td>
<td style="border-left:1px solid #528ABD;" class="leftpadd"><%# DataBinder.Eval(Container.DataItem, "Quantity", "{0:000}")%></td>
<td style="border-left:1px solid #528ABD;" class="leftpadd"><asp:CheckBox ID="chkPendingItems" runat="server" /></td> 

Which populates fine. What I'm looking to happen is for the user to be able to select certain items from the repeater using the checkbox and "process" them (i.e. perform some data operations on those items) on clicking a button which is outside the repeater. Here's my button click code:

Private Sub lnkPendingProcessSelected_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkPendingProcessSelected.Click
    For Each rItem As RepeaterItem In rptPendingItems.Items
        If rItem.ItemType = ListItemType.Item Or rItem.ItemType = ListItemType.AlternatingItem Then
            Dim chk As CheckBox = DirectCast(rItem.FindControl("chkPendingItems"), CheckBox)
            If chk.Checked Then
                Dim orderItemId As Integer
                Dim hid As HiddenField = DirectCast(rItem.FindControl("hidPending"), HiddenField)
                orderItemId = CInt(hid.Value)
                My.Application.ManagerFactory.OrderManagerInstance.ChangeOrderItemStatus(orderItemId, Concrete.Cms.DataTransferObjects.OrderItemStatus.Processing)
            End If
        End If
    Next
End Sub

But if you step through this, the checkboxes are found and assigned correctly but their Checked attribute is always False. Anyone have any suggestions as to why state isn't being maintained and what I can do about it?

Upvotes: 2

Views: 3639

Answers (2)

Andrew M
Andrew M

Reputation: 9459

Based on CyberDude's comment, you're probably reseting the values when you databind. If possibly, try using IsPostback to only databind on the first page load.

C#

if(!IsPostBack)
{
    rptPendingItems.DataBind();
}

VB

If Not IsPostBack Then
  rptPendingItems.DataBind()
End If

If that's not possible, or doesn't work, you'll probably have to get and set all of those checkbox values mannually, and persist them against your data set, or in session or summat.

Upvotes: 3

Angel Hawks
Angel Hawks

Reputation: 1

I had a repeater inside a user control on a page with other user controls which cause post backs. In some cases, I wanted to rebind on postback so I created a bool property (IsDirty) on the page that I could set to true when I wanted to rebind the data on postback. Then in my page_load, I checked if (!IsPostBack || IsDirty) before databinding.

Upvotes: 0

Related Questions