Reputation: 3610
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 the 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'm wondering why my repeater looses it's data after i click that button.(postback)
Upvotes: 4
Views: 7257
Reputation: 460058
Bind the Repeater
in OnInit
instead.
http://codinglifestyle.wordpress.com/2009/10/08/repeaters-and-lost-data-after-postback-viewstate/
Edit: I assume that you're handling the UserControl
's Load
event which gets raised after the Page
's Load
event. Dynamic controls must be created in Page_Load at the latest.
http://forums.asp.net/t/1191194.aspx
So either bind your Repeater in Page_Init or -what i would recommend:
Provide a public function like BindData
that can be called from your page's load-event. This is also the recommended way since the page is the controller of the UserControl.
public void BindData()
{
var products = (from p in context.Products
select p).Take(30);
rptProducts.DataSource = products;
rptProducts.DataBind();
}
Upvotes: 4