Adding buttons with events runtime

Discussion in 'ASP .Net Web Controls' started by Klaus Jensen, Apr 28, 2005.

  1. Klaus Jensen

    Klaus Jensen Guest

    Hi!

    This has been annoying me for a while now, and I can't get it to work It is
    really driving me nuts!

    Basicly this simple webapp created to illustrate my problem, renders five
    buttons, and adds a handler to the click event. When a button is clicked,
    the background-color is set to blue.

    Try running the page, and clicking the "Do stuff and update controls
    again"-button. Now click one of the test-buttons. Notice the click-event
    does not fire, the button is not made blue. Click a test-button again, and
    notice now the event is firing.

    Now try clicking the "Do stuff and do NOT update controls"-button. Then
    click the test-buttons. Notice how the events fire nicely!

    I am sure this is not a bug, I think it is simply a matter of me not
    understanding the flow of the page-events somehow, but I am stuck and would
    really appreciate any help.

    The demo-app in itself does not make sense, but I think it is the easiest
    way to illustrate my problem. In my real-life app, the buttons are rendered
    one for each row in an databasetable, and the buttons remove an item from
    the databasetable on click. Therefore, when I click a button (testX in the
    example) to remove an item, I need to re-render all the buttons, because one
    of them has now been removed.

    How do I make this work?

    Thanks in advance

    - Klaus

    I have uploaded the Asp.Net project zipped to this url:

    http://www.agilator.dk/temp/delegatetest.zip



    [aspx]
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    <HEAD>
    <title>WebForm1</title>
    </HEAD>
    <body>
    <form id="Form1" method="post" encType="multipart/form-data"
    runat="server">
    <P><asp:button id="Button1" runat="server" Text="Do Stuff and Show
    Controls again"></asp:button>
    <asp:Button id="Button2" runat="server" Text="Do Stuff and do not update
    Controls"></asp:Button></P>
    <P><asp:placeholder id="PlaceHolder1"
    runat="server"></asp:placeholder></P>
    </form>
    </body>
    </HTML>
    [/aspx]

    [codebehind, vb]

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load
    ShowMeTheButtons()
    End Sub

    Sub ShowMeTheButtons()
    PlaceHolder1.Controls.Clear()
    PlaceHolder1.Controls.Add(MakeFiveTestButtons)
    End Sub

    Function MakeFiveTestButtons() As PlaceHolder

    Dim plc As New PlaceHolder
    Dim btnTest As System.Web.UI.WebControls.Button
    For i As Integer = 1 To 5
    btnTest = New System.Web.UI.WebControls.Button
    btnTest.Text = "Test" & i.ToString
    btnTest.CommandArgument = i
    AddHandler btnTest.Click, AddressOf TestButtonClick
    plc.Controls.Add(btnTest)
    Next

    Return plc

    End Function

    Private Sub TestButtonClick(ByVal sender As System.Object, ByVal e As
    System.EventArgs)
    Dim btn As System.Web.UI.WebControls.Button = CType(sender,
    System.Web.UI.WebControls.Button)
    btn.BackColor = System.Drawing.Color.Blue
    End Sub


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles Button1.Click
    'Do stuff here which affects the data, the buttons are generated
    from in the real-life app
    ShowMeTheButtons()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles Button2.Click
    'Do stuff here which affects the data, the buttons are generated
    from in the real-life app
    End Sub
    [/codebehind, vb]
     
    Klaus Jensen, Apr 28, 2005
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Doug Slocum

    Help adding buttons at runtime...

    Doug Slocum, May 17, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    466
    Doug Slocum
    May 17, 2004
  2. Klaus Jensen

    Adding buttons with events runtime

    Klaus Jensen, Apr 28, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    488
    Klaus Jensen
    Apr 28, 2005
  3. newbye
    Replies:
    0
    Views:
    593
    newbye
    Jul 6, 2006
  4. Andrés Giraldo

    Adding buttons to a datagrid doesn't fire its events

    Andrés Giraldo, Sep 19, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    192
    Andrés Giraldo
    Sep 19, 2003
  5. Chris

    Events Events Events Please Help

    Chris, Aug 30, 2005, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    424
    Chris
    Aug 30, 2005
Loading...

Share This Page