开发者

Why this event is fired?

开发者 https://www.devze.com 2023-02-17 20:07 出处:网络
I have a page with a repeater and a button. (quite simple) My repeater have an event rptEtats_ItemCreated raised OnItemCreated

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

My repeater have an event rptEtats_ItemCreated raised OnItemCreated

<asp:Repeater ID="rptEtats" 开发者_StackOverflow社区runat="server" OnItemCreated="rptEtats_ItemCreated">
    <ItemTemplate>

    </ItemTemplate>

</asp:Repeater>

CodeBehind:

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

The button on the page raise a OnClick event

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

CodeBehind:

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

It's work fine until I click on the button, I expect the btnValid_click method but the rptEtats_ItemCreated method is called first! I don't understand why. Is the page load again before call the button method? Why the repeater is databinding again?


The repeater is not bound again. It stores a count of items in the view state. On post-back it reads this value and creates its child controls (items) the appropriate number of times in order for them to be able to load their own view state. The ItemCreated event is fired every time an item is created.


You have to cal rptEtats.Databind(); at Page_Init, that is before view state is loaded. In that case controls would be re-created properly and button onclik would be fired.


I bet you have in the Page_Load event, a rptEtats.Databind() call.

Am I right ?

If true, sorround the call this method like this :

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

}

The behavior you notice is due of the page lifecycle. The Page Load event is called before the Click events.

0

精彩评论

暂无评论...
验证码 换一张
取 消