Why is this event fired?

I have a page with a repeater and a button. (pretty simple)

My repeater has an event rptEtats_ItemCreatedraisedOnItemCreated

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

    </ItemTemplate>

</asp:Repeater>

CodeBehind:

public void rptEtats_ItemCreated(object sender, RepeaterItemEventArgs e)
{
    //Stuff
}

The button on the page raises the OnClick event

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

CodeBehind:

public void btnValid_click(Object sender, EventArgs e)
{
   //Stuff
}

It works fine until I press the button, I expect the method btnValid_click, but the method rptEtats_ItemCreatedis called first! I do not understand why. Does page loading repeat before calling the button method? Why does the repeater bind the data again?

+3
source share
3 answers

You must execute cal rptEtats.Databind();in Page_Init, that is, before loading the view state. In this case, the controls will be recreated correctly and the onclik button will start.

+1
source

. . () , . ItemCreated , .

+2

I am sure that you are in the Page_Load event, a rptEtats.Databind().

I'm right?

If true, connect the call in the following way:

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

}

The behavior you notice is related to the life cycle of the page. The page load event is fired before Click events.

+1
source

All Articles