Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么会触发此事件?_C#_Asp.net_Webforms_Repeater - Fatal编程技术网

C# 为什么会触发此事件?

C# 为什么会触发此事件?,c#,asp.net,webforms,repeater,C#,Asp.net,Webforms,Repeater,我有一个带有转发器和按钮的页面。(很简单) 我的中继器有一个已创建的事件 <asp:Repeater ID="rptEtats" runat="server" OnItemCreated="rptEtats_ItemCreated"> <ItemTemplate> </ItemTemplate> </asp:Repeater> 页面上的按钮将引发OnClick事件 <asp:Button ID="btnValid" ru

我有一个带有转发器和按钮的页面。(很简单)

我的中继器有一个已创建的事件

<asp:Repeater ID="rptEtats" runat="server" OnItemCreated="rptEtats_ItemCreated">
    <ItemTemplate>

    </ItemTemplate>

</asp:Repeater>
页面上的按钮将引发OnClick事件

<asp:Button ID="btnValid" runat="server" Text="Valid" OnClick="btnValid_click" />

在我点击按钮之前,它工作正常,我希望使用
btnValid\u click
方法,但是首先调用
rpteatas\u itemscreated
方法!我不明白为什么。在调用按钮方法之前是否再次加载页面?为什么中继器再次进行数据绑定?

我打赌您在页面加载事件中有一个
rpteatas.Databind()
调用

我说得对吗

如果为true,则按如下方式调用此方法:

protected void Page_Load(object sender, EventArgs e)
{
    if(!Page.IsPostBack)
    {
        rptEtats.Databind();
    }

}

您注意到的行为是由于页面生命周期造成的。页面加载事件在单击事件之前被调用。

中继器不会再次绑定。它存储视图状态中的项目计数。在回发时,它读取该值并创建其子控件(项)适当的次数,以便它们能够加载自己的视图状态。每次创建项目时都会触发
ItemCreated
事件。

您必须校准
rptEtats.Databind()在页面_Init,即加载视图状态之前。在这种情况下,控件将被正确地重新创建,按钮onclik将被触发。

Oups,我与项目_数据绑定混淆了。很抱歉
public void btnValid_click(Object sender, EventArgs e)
{
   //Stuff
}
protected void Page_Load(object sender, EventArgs e)
{
    if(!Page.IsPostBack)
    {
        rptEtats.Databind();
    }

}