Addhandler, button.click not firing using VB.NET

后端 未结 2 666
南旧
南旧 2021-01-23 12:51

I am experiencing a problem with buttons and AddHandler. It only works if I use AddHandler Button1.click, AddressOf... in Page_load, but if I create the button dynamically in on

2条回答
  •  灰色年华
    2021-01-23 13:19

    You have to regenerate your dynamically created controls on every postback (at last in Page_Load, better in Page_Init). You have to set the ID of the controls accordingly because ASP.Net needs it to identify which control caused a Postback and to handle the appropriate events.

    You could save the number of created buttons in ViewState and use this to regenerate them on Page_Load. Increase the number when you add a new button. Use this number also to make the Button's ID unique(append it to the ID) to ensure that its the same on every postback.

    For further informations, have a look the Page-Lifecycle and ViewState with dynamically added controls.

    Edit: As Joel commented, if you only need one Button you can set it's ID statically, but you have to regenerate it on postback f.e. to handle its click-event.

提交回复
热议问题