Losing value when clicking on button in ascx control that is rendered in asp:Repeater

StackOverflow https://stackoverflow.com/questions/22477919

  •  16-06-2023
  •  | 
  •  

Question

I created a asp:Repeater that I fill with .ascx controls:

protected void Page_Load(object sender, EventArgs e)
{
    Repeater1.DataSource = listOfData;
    Repeater1.DataBind();            
}

On page I have:

<uc:Product runat="server"                                             
    ImportantData='<% #Eval("ImportantData") %>'                                               
    id="DetailPanel1" />

Inside Product.ascx.cs I have:

public int ImportantData{ get; set; }
protected void Page_Load(object sender, EventArgs e)
{

}

On Product.ascx I have:

<asp:ImageButton ID="btn_Ok" runat="server" 
     onclick="btn_Ok_Click"  
     ImageUrl="~/image.png" />

Problem is when I click on image button I get error:

A critical error has occurred. Invalid postback or callback argument. Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page...

I have tried to change first code to this:

protected void Page_Load(object sender, EventArgs e)
{
    if(!Page.IsPostBack)
    {
        Repeater1.DataSource = listOfData;
        Repeater1.DataBind();            
    }
    // Repeater1.DataBind(); // and tried to put DataBind() here
}

But then when I click on image button ImportantData is empty.
What I did wrong here?

Était-ce utile?

La solution

PageLoad is happening before your postback event, change your event to PreRender:

protected void Page_PreRender(object sender, EventArgs e)
{
    Repeater1.DataSource = listOfData;
    Repeater1.DataBind();            
}

This will bind your repeater after the postback and maintain your postback value.

Edit:

Here's a good picture showing the entire WebForms page lifecycle:

As you can see

ProcessPostData event is called AFTER Load event.

So you want to bind your Repeater after the PostData has been processed.

This can be done on PreRender

Autres conseils

The problem is not that you are calling data bind in the wrong place, but most likely that the user control is not saving the data in viewstate correctly. It looks like you are using a custom control. What you can do if this is the case is to create a property on your user control that reads from the viewstate. Here is an example:

Public Property ImportantData() As Int32
    Get
        Return ViewState("_ImportantData")
    End Get
    Set(ByVal value As Int32)
        ViewState("_ImportantData") = value
    End Set
End Property

This will allow your data to persist and the necessary data to be present on postback.

and doing this is correct so that it only loads on the initial load.

protected void Page_Load(object sender, EventArgs e)
{
    if(!Page.IsPostBack)
    {
        Repeater1.DataSource = listOfData;
        Repeater1.DataBind();            
    }
    // Repeater1.DataBind(); // and tried to put DataBind() here
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top