单击在asp:Repeater中呈现的ascx控件中的按钮时丢失值

111*_*110 8 c# asp.net repeater

我创建了一个asp:Repeater我填充.ascx控件:

protected void Page_Load(object sender, EventArgs e)
{
    Repeater1.DataSource = listOfData;
    Repeater1.DataBind();            
}
Run Code Online (Sandbox Code Playgroud)

在页面上我有:

<uc:Product runat="server"                                             
    ImportantData='<% #Eval("ImportantData") %>'                                               
    id="DetailPanel1" />
Run Code Online (Sandbox Code Playgroud)

里面Product.ascx.cs我有:

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

}
Run Code Online (Sandbox Code Playgroud)

Product.ascx我有:

<asp:ImageButton ID="btn_Ok" runat="server" 
     onclick="btn_Ok_Click"  
     ImageUrl="~/image.png" />
Run Code Online (Sandbox Code Playgroud)

问题是当我点击图像按钮时出现错误:

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...
Run Code Online (Sandbox Code Playgroud)

我试图将第一个代码更改为:

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

但是当我点击图片按钮时ImportantData是空的.
我在这里做错了什么?

Phi*_*ill 3

PageLoad 在回发事件之前发生,请将事件更改为 PreRender:

protected void Page_PreRender(object sender, EventArgs e)
{
    Repeater1.DataSource = listOfData;
    Repeater1.DataBind();            
}
Run Code Online (Sandbox Code Playgroud)

这将在回发后绑定您的转发器并保持您的回发值。

编辑:

这是一张显示整个 WebForms 页面生命周期的好图

如你看到的

ProcessPostData事件称为AFTER Load事件。

所以你想Repeater在处理 PostData 后绑定你的。

这可以在PreRender