gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 19:44:04 25 4
gpt4 key购买 nike

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

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

在我的页面上:

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

Product.ascx.cs 里面我有:

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

}

Product.ascx 我有:

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

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

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...

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

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

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

最佳答案

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

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

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

编辑:

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

如你所见

ProcessPostData 事件被称为 AFTER Load 事件。

所以你想在处理完 PostData 之后绑定(bind)你的 Repeater

这可以在 PreRender 上完成

关于c# - 单击在 asp :Repeater 中呈现的 ascx 控件中的按钮时丢失值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22477919/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com