Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why this event is fired?

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" 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?

like image 928
bAN Avatar asked Nov 26 '25 21:11

bAN


2 Answers

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.

like image 181
thorn0 Avatar answered Nov 28 '25 10:11

thorn0


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.

like image 42
Roman Asanov Avatar answered Nov 28 '25 09:11

Roman Asanov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!