I have multiple asp:button that I created dynamically with jQuery.
For each of them, I have OnClick="ibtn_Click" .
But on the code behind, I have to manually register each one of them on Page_Load..like.. button.Click += new EventHandler(this.ibtn_Click); .
Is there a way I can register this handler automatically? Or is it possible to register it with ajax jquery?
Thanks!
You can’t add ASP.NET web controls in the client’s browser. ASP.NET transforms those
<asp:button>tags into regular HTML, something like this:When a user clicks one of these submit controls, the browser sends a POST request, which includes the button’s
nameas part of the posted data (which you can access via theFormcollection in ASP.NET):ASP.NET automatically binds server control clicks to the methods specified in the
OnClickattribute. It calls the method afterPage.Loadcompletes (for the gory details, see “ASP.NET Page Life Cycle Overview“).If you’re adding the buttons on the client, you can do all this yourself. For example you could “bind” the dynamically generated button in
Page_PreRender:Finally, in this day and age, if it’s appropriate, you should definitely consider just calling page methods via jQuery’s
ajax. See this excellent Encosia article for an introduction to that technique.