0

I have a Repeater and a Button control. Within the Repeater I have a HiddenField control:

<asp:Repeater runat="server" ID="rptItems">           
    <ItemTemplate>                          
        <asp:HiddenField runat="server" ID="hfReportId"></asp:HiddenField>
    </ItemTemplate>
</asp:Repeater>

<asp:Button runat="server" ID="btnSave" Text="Save" /> 

In the code behind, I am binding the ItemDataBound event handler to the Repeater in the Page_Load:

this.rptItems.ItemDataBound += new RepeaterItemEventHandler(rptItems_ItemDataBound);

In the event handler, I am setting the value of the HiddenField control programmatically:

protected void rptItems_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
    {
        var hfReportId = (HiddenField)e.Item.Controls[0].FindControl("hfReportId");
        hfReportId.Value = "TestValue";
    }    
}

So far, this works as expected and the value of the HiddenField is set to "TestValue".

The problem occurs on postback. If I click the Save button, the ItemDataBound event handler is fired again, and the value of the HiddenField is set once again, however the original value is maintained and I end up with a value of "TestValue,TestValue". I have swapped the HiddenField for a label control and this bahaviour does not happen.

I have stepped through the code, and when the ItemDataBound event handler fires on postback there is no value for the HiddenField.

Any help is appreciated.

Ash
  • 2,108
  • 2
  • 17
  • 22
  • 1
    Don't use `(HiddenField)e.Item.Controls[0].FindControl("hfReportId");` but `(HiddenField)e.Item.FindControl("hfReportId");`. He RepeaterItem is the NamingContainer, so this is more fail-safe. Try to register the event declaratively: `` – Tim Schmelter Oct 08 '15 at 09:46
  • 1
    Also, are you using JQuery and/or ASP.NET Ajax? Is this repeater in a jQuery UI Dialog? – Tim Schmelter Oct 08 '15 at 09:49
  • Yes - I am using ASP.NET Ajax. This appears to be the cause of the problem - when I remove it the HiddenField is updated as expected. Seems like HiddenField controls are handled differently to Label controls then? – Ash Oct 08 '15 at 09:59
  • @Ash - What you are doing with `Ajax`? Also, how your binding your repeater? Make sure you do that by checking `!IsPostBack`. – Rahul Singh Oct 08 '15 at 10:02
  • 1
    @Ash: i guess that it's a `ViewState` issue, the `HiddenField.Value` is stored in ViewState the `Label.Text` not. If you're using a client library like `jQUery` it could also cause controls to be rendered multiple times which repeats the value and appends a comma. [I've also noticed it some time ago](http://stackoverflow.com/questions/5662263/jquery-dialog-postback-but-updatepanel-doesnt-get-updated) (search for **Update**). – Tim Schmelter Oct 08 '15 at 10:07
  • @TimSchmelter That must be it. I have now swapped to a Label control hidden by CSS to achieve the same thing. Thanks for your help - appreciated. – Ash Oct 08 '15 at 10:19

2 Answers2

0

You should have to bind event of item data bound in repeater in your aspx page like

OnItemDataBound="repeater_ItemDataBound"

use page load ispostback property while binding the repeater

if(!IsPostback)
{
   //Bind repeater
}

then in item data bound use

var hfReportId = (HiddenField)e.Item.FindControl("hfReportId");

instead of

var hfReportId = (HiddenField)e.Item.Controls[0].FindControl("hfReportId");

Happy Coding

Nikunj Soni
  • 297
  • 2
  • 13
0

I had the same problem with an asp:HiddenField inside a asp:Repeater. The issue was : On PostBack, the values inside my asp:Repeater where refreshed with DataBind() in the PageLoad() method instead of the ButtonSearch_click() event method. That's why I was still getting old values in asp:HiddenField of my asp:Repeater.

Jipge
  • 1