Add Events to Controls Nested in a Repeater ASP-C#
I'm currently working with a repeater which has some number of drop down lists in it determined by how many items are databound to it. I wanted to add an event to each of these drop down lists in the scenario that a user changes the selected index.
Here is what I have for the repeater (Note that I am doing all of the databinding in the codebhind.):
<ItemTemplate>
<tr>
<td>
<asp:DropDownList ID="DropDownList1" runat="server" Text='<%# Eval("Data") %>' AutoPostBack="True" OnTextChanged="TextChanged">
<asp:ListItem>Hello World</asp:ListItem>
<asp:ListItem>GoodBye Cruel World</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
I am setting the OnTextChanged event here, however I did some experimenting with using the different events. I found that my method will not run when I am using the OnTextChanged or OnSelectedIndexChanged events. However, if I use some other events like OnLoad or OnPreRender, the method I have it set to actually runs.
So in short, why is it that when I put this drop down list i开发者_C百科n the repeater only SOME of the events seem to work?
This is likely happening because you're rebinding the repeater in Page_Load. If you bind the repeater in CreateChildControls or OnPreRender, the OnTextChanged or OnSelectedIndexChanged events (both of which rely on ViewState) will fire correctly; otherwise, the control is rebound too early and no event is fired because no change is detected.
The other events are firing because they don't rely on ViewState.
Hope this helps.
精彩评论