Maintaining the look of the repeater

I have a problem in which viewing the status of the repeater, that is, the controls in the repeater do not support their presentation.

I have the following:

Repeater 1:

<asp:Repeater ID="rptImages" runat="server"> <ItemTemplate> <asp:LinkButton Text="Add" CommandName="Add" CommandArgument=<%# Eval("ID") %> runat="server" /> </ItemTemplate> </asp:Repeater> 

When you click the link button, the CommandArgument value is stored in a hidden field on the page.

After the postback, I cannot get the value of the hidden field before loading the prerender event handler. Therefore, in my prerender event, I get the value of the hidden field and save it in the List property, for example:

 if (!string.IsNullOrEmpty(this.SelectedImageIDsInput.Text)) { this.ImageList.Add(this.SelectedImageIDsInput.Text); } 

And the List property looks like this:

 public List<string> ImageList { get { if (this.ViewState["ImageList"] == null) { this.ViewState["ImageList"] = new List<string>(); } return (List<string>)(this.ViewState["ImageList"]); } set { this.ViewState["ImageString"] = value; } } 

Once I have stored the value in my List Property, I bind my second repeater (again in the prerender event):

 this.rptSelectedImages.DataSource = this.LightBoxControl.ImageList; this.rptSelectedImages.DataBind(); 

The second repeater has a drop-down list and a text field inside it. The problem is that the viewstate of these child controls is not supported. I assume this is due to the fact that with every postback I rewrite the relay, so it is rebuilt. I do not know how I can get around this? The ImageList property is only updated after the postback, so I obviously have to retry the repeater with each postback - how else can this be done?

Any help would be greatly appreciated.

Thanks al

+7
source share
2 answers

If you are overwriting a repeater, you need to do this on Init before loading ViewState .

You should also check the IsPostback flag and only bind the relay when the page has not been sent back.

To clarify whether your second repeater is attached to PreRender , then ViewState cannot be used to save controls, because they simply do not exist when ViewState loads - after Init and before PreLoad .

You need to either continue the binding at each postback, or save or list in Session so that you have access to the list for binding once on Init (or when changing).

+13
source

I see no reason to copy the CommandArgument property to a hidden field. What you need to do is use the ItemCommand event on the Repeater and use the event bubbles. You can handle the Click event on you LinkButton as follows:

 repeater.ItemCommand += (sender, eventArgs) => { var commandArgument = eventArgs.CommandArguments; ImageList.Add(commandArgument); rptSelectedImages.DataSource = ImageList; rptSelectedImages.DataBind(); } 
0
source

All Articles