Maintaining viewstate of a repeater

后端 未结 2 673
暗喜
暗喜 2021-01-12 09:57

I have a problem whereby the viewstate of a repeater i.e. the controls within the repeater are not maintaing their viewstate.

I have the following:

Repeater

相关标签:
2条回答
  • 2021-01-12 10:30

    If you are rebinding the repeater, you need to do it on Init before the ViewState is loaded.

    You should also check the IsPostback flag and only Bind the repeater when the page is not posted back.

    To clarify if your second repeater is bound on PreRender then ViewState cannot be used to persist the controls, because they simply don't exist when ViewState is loaded - after Init, and before PreLoad.

    You either need to continue binding on every postback, or store or list in Session so that you have access to the list to bind once on Init, (or on change).

    0 讨论(0)
  • 2021-01-12 10:42

    I don't see the point of copying the CommandArgument property to a hidden field. What you should do is to use the ItemCommand event on the Repeater and use event bubbling. You can handle the Click event on you LinkButton like this:

    repeater.ItemCommand += (sender, eventArgs) => {  
       var commandArgument = eventArgs.CommandArguments;
       ImageList.Add(commandArgument);
       rptSelectedImages.DataSource = ImageList;
       rptSelectedImages.DataBind();
    }
    
    0 讨论(0)
提交回复
热议问题