I'd like to use a custom control within a repeater with access to the data item of the repeater from within the custom control. This works fine when loading the page, but when binding data to the repeater from a button click event it will not work.
Over the past fwe days I have tried so many different approaches, using ViewStates, Session, disabling ViewState, doing things with and without update panels but I always end at the same issue. For some reason when binding data on the repeater in the button click event handler the "AssignedValue" property of the custom control will not be set, which works on page load without postback.
I am confused, as the repeater item is present but the custom control is loaded without it being assigned to "AssignedValue". How else would I bind a different data set to the repeater when clicking a button?
Any ideas how I can solve this issue?
Demo solution download: https://drive.google.com/file/d/10TvaCr0p6wPkQ6HoA0PnjwXgYgVJEKp8/view?usp=sharing or see the code below.
Default.aspx
<%@ Page Title="Home Page" Language="VB" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.vb" Inherits="WebApplication1._Default" %>
<%@ Register TagPrefix="test" Src="~/WebUserControl1.ascx" TagName="ctrl" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<asp:UpdatePanel runat="server">
<ContentTemplate>
<asp:Repeater runat="server" ID="rptTest" ItemType="WebApplication1.Test">
<ItemTemplate>
<div>
<test:ctrl runat="server" AssignedValue="<%# Item.Value %>" /> (Item: <%# Item.Value %>)
</div>
</ItemTemplate>
</asp:Repeater>
<br />
<asp:LinkButton runat="server" ID="btnLoadMore" OnClick="btnLoadMore_Click">Load More!</asp:LinkButton>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
Default.aspx.vb
Public Class _Default
Inherits Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
rptTest.DataSource = Test.Set1
rptTest.DataBind()
End If
End Sub
Protected Sub btnLoadMore_Click(sender As Object, e As EventArgs)
rptTest.DataSource = Test.Set2
rptTest.DataBind()
End Sub
End Class
Test.vb
Public Class Test
Public Property Value As String
Public Shared Property Set1 As Test() = {New Test() With {.Value = "a"}, New Test() With {.Value = "b"}}
Public Shared Property Set2 As Test() = {New Test() With {.Value = "c"}, New Test() With {.Value = "d"}}
End Class
WebUserControl1.aspx
<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="WebUserControl1.ascx.vb" Inherits="WebApplication1.WebUserControl1" %>
Value: <asp:Literal runat="server" ID="litValue" />
WebUserControl.aspx.vb
Public Class WebUserControl1
Inherits System.Web.UI.UserControl
Public Property AssignedValue As String
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
litValue.Text = AssignedValue
End Sub
End Class
What I expect:
Value: a (Item: a)
Value: b (Item: b)
*Click on Load More!*
Value: c (Item: c)
Value: d (Item: d)
What I get:
Value: a (Item: a)
Value: b (Item: b)
*Click on Load More!*
Value: (Item: c)
Value: (Item: d)