Help in asp.net addhandler

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi, I create an dynamic command button like that:
buttonCmd= New Button
buttonCmd.ID = "IDBTN1"
buttonCmd.Text = "Test"
AddHandler buttonCmd.Click, AddressOf MessageClick

I have this click sub:
Sub MessageClick(ByVal sender As Object, ByVal e As System.EventArgs)
'Do something
End Sub

For the first try I create this dynamic button on time in the form load
between
If Not Page.IsPostBack Then
 
Sorry I hit the Post buttun!

Here is the next part of my answers...

Like I said, I put the dynamic button in the
if not page.isPostBack then
'create the button
end if

When I hit the button, nothing append and my button disapear. I think that
my buttun is lost in the postbak add for that reason the event added by the
addhandler is not fired...

I change my code to initialize the button and add the handler in the
page_load without the if not page.isPostBack limitation.
When I start the web page I I click on the button I receive this error:

Multiple controls with the same ID 'IDBTN1' were found. FindControl requires
that controls have unique IDs.

Why? The postback is not supposed to lost this control? How can I fire my
event?

Thanks for your help
 
Pierre,

Dynamically created controls need to be re-created in code upon every
request to the page AND it has to be re-created before the Page_Load() event
fires, I suggest you override the Init() and put your code to create the
button in that method.

HTH,
Jorge
 
ok, but the example I post is verry simple, but in the real life, the number
of command button are deternimed by some querys to an SQL database in
function of the user logged in (form authentication).
Plus the command button are created in a cell of a static asp table. This
table is created before the Page_Init sub? I will be able to acces it and
populate it?
 
OK I make the test. I place the function that populate the static table and
create the dynamic button. All work good for acceding the static table to
when I hit the button a got the same errer :

Multiple controls with the same ID 'BTN11' were found. FindControl requires
that controls have unique IDs.
 
Nothing more to help?

Pierre said:
OK I make the test. I place the function that populate the static table and
create the dynamic button. All work good for acceding the static table to
when I hit the button a got the same errer :

Multiple controls with the same ID 'BTN11' were found. FindControl requires
that controls have unique IDs.
 
Back
Top