Results 1 to 7 of 7

Thread: Dynamic button event won't fire

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2002
    Location
    Florida
    Posts
    16

    Angry Dynamic button event won't fire

    Hi All,

    I use the event of a non-dynamic control to call a sub that creates dynamic labels, textboxes and a button and the button's event. The problem is the button's event never fires. I have autopostback set to True for the dropdownlist and the option buttons. Here follows code snippets...

    Public Class TroubleTicket
    Inherits System.Web.UI.Page

    Protected WithEvents optMenu As RadioButtonList
    Protected WithEvents lstUsers As System.Web.UI.WebControls.DropDownList

    ' the radio button option "Add new user" calls DisplaylstUsers()
    Private Sub DisplaylstUsers()
    'code here fills lstUser from a db

    lstUsers.AutoPostBack = True ' Negates need for a submit button
    AddHandler lstUsers.SelectedIndexChanged, AddressOf lstUsers_OnChange
    end sub

    Private Sub lstUsers_OnChange(ByVal sender As Object, ByVal e As EventArgs) Handles lstUsers.SelectedIndexChanged
    Select Case lstUsers.SelectedItem.Text
    Case "-Make a selection-"
    ' do nothing
    Case "Add a new user"
    Display_TextBoxes() ' Input boxes for new user info to add to database
    Case Else
    DisplayTickets(CInt(lstUsers.SelectedItem.Value)) ' All tickets for particular user
    End Select
    End Sub ' lstUsers_OnChange()

    ' in lstUsers I select "Add a new user" which calls Display_TextBoxes()
    Public Sub Display_TextBoxes()
    Dim btnNewUser As New System.Web.UI.HtmlControls.HtmlButton()
    With btnNewUser
    .ID = "btnNUser"
    .InnerText = "Add New User"
    .Visible = True
    End With
    AddHandler btnNewUser.ServerClick, New EventHandler(AddressOf dxaBtnClk)
    Me.FindControl("Form1").Controls.Add(btnNewUser)
    end sub

    ' I click btnNewUser but never get to...
    Public Sub dxaBtnClk(ByVal sender As Object, ByVal e As EventArgs)
    Response.Write("dxaBtnClk was clicked")
    End Sub ' dxaBtnClk
    End Class

  2. #2
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    Dim btnNewUser As New System.Web.UI.HtmlControls.HtmlButton()


    ^^^

    you are creating a client side html button, not a server side web forms button. Client side cant run server side functions.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jun 2002
    Location
    Florida
    Posts
    16

    Dynamic button event won't fire

    Thanks for the reply and sorry for the dumb code.

    That HTML button code was a desparate attempt to find a solution. The original and current code for the button is...

    Dim btnNewUser As New Button()
    With btnNewUser
    .ID = "btnNUser"
    .Text = "Add New User"
    .Style.Item("position") = "absolute" ' necessary to keep controls from laying out wacky
    .Style.Item("top") = "400px" 'iNewTop.ToString
    .Style.Item("left") = "200px" 'arrTextBoxes(iColCount - 1).Style.Item("left")
    .Visible = True
    .CausesValidation = True
    End With
    AddHandler btnNewUser.Click, AddressOf dxaBtnClk
    Me.FindControl("Form1").Controls.Add(btnNewUser)

    Any ideas why the dxaBtnClk() doesn't fire?

  4. #4
    Lively Member
    Join Date
    Mar 2002
    Posts
    89
    try this

    addhandler yourbuttonname.click, new system.eventhandler(addressOf the_function_that_you_want_to_call_when_pressing_the_button)
    BlahDoS . :P

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jun 2002
    Location
    Florida
    Posts
    16

    Dynamic button event won't fire

    My VS.NET has become corrupted and I'm having problems reinstalling. When I do reinstall I'll try out your suggestion.

    Thanks,
    David

  6. #6
    Fanatic Member
    Join Date
    Oct 1999
    Location
    England
    Posts
    982
    PopsMcF:
    Did this work?

    I am trying to do something similar.


    Things I do when I am bored: DotNetable

  7. #7
    Fanatic Member
    Join Date
    Oct 1999
    Location
    England
    Posts
    982

    Re: Dynamic button event won't fire

    Quote Originally Posted by davidrobin
    PopsMcF:
    Did this work?

    I am trying to do something similar.
    To answer my own question, it does work.

    Thanks BlahDos

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width