Asp.net relay data after postback

In my .ascx control:

<asp:Repeater ID="rptProducts" runat="server"> <ItemTemplate> <asp:Label ID="lblProductName" runat="server"> <%# Eval("Name") %> </asp:Label> </ItemTemplate> </asp:Repeater> <asp:Button ID="btnGo" runat="server" Text="Postback" onclick="btnGo_Click" /> 

And in codebehind:

 protected void Page_Load(object sender, EventArgs e) { if(!this.IsPostBack){ var products = (from p in context.Products select p).Take(30); rptProducts.DataSource = products; rptProducts.DataBind(); } } 

And I wonder why my repeater is losing data after clicking this button. (postback)

+4
source share
1 answer

Bind Repeater to OnInit .

http://codinglifestyle.wordpress.com/2009/10/08/repeaters-and-lost-data-after-postback-viewstate/

Change I assume that you are handling the UserControl Load event, which receives the Page Load event after . Dynamic controls must be created in Page_Load no later than.

http://forums.asp.net/t/1191194.aspx

Thus, either bind the repeater to Page_Init, or -that I would recommend:

Provide a public function, such as BindData , that can be called from your page load event. This is also recommended since the page is a UserControl.

 public void BindData() { var products = (from p in context.Products select p).Take(30); rptProducts.DataSource = products; rptProducts.DataBind(); } 
+3
source

Source: https://habr.com/ru/post/1403987/


All Articles