Buttons added dynamically, different behaviour

Discussion in 'ASP .Net' started by Bjorn Sagbakken, Dec 30, 2006.

  1. Hello

    In VS2005:
    I am adding buttons and textboxes dynamically into a table, that also
    dynamically expands.
    So far, so good, actually very nice.
    But I am having trouble starting the desired subroutine on postback. To me
    it seems like the button click event looks for a client script instead of
    going to the aspx codepage and execute the sub there.
    The same thing happens with textboxes, where I programatically add
    ("OnBlur")=mySub
    The browser raises an error, saying "mySub is undefined". In the aspx
    codepage mySub is set as Public, by to no help. The browser obviously looks
    for a client sub called mySub.
    Note: I have added "Runat=Server" to the controls, and I thought this would
    do.

    As with many things, I guess there is a simple solution to this, only I
    haven't found it (yet).

    Regards
    Bjorn
    Bjorn Sagbakken, Dec 30, 2006
    #1
    1. Advertising

  2. Hi,

    Bjorn Sagbakken wrote:

    > I am adding buttons and textboxes dynamically into a table, that also
    > dynamically expands.
    > So far, so good, actually very nice.
    > But I am having trouble starting the desired subroutine on postback. To me
    > it seems like the button click event looks for a client script instead of
    > going to the aspx codepage and execute the sub there.


    that simply depends on what you do. If you want your dynamic button to
    execute server-side code, then you need to define a handler for it and link
    your button to that event. Create your event, i.e.:

    protected Sub MyButtonClickHandler( _
    ByVal sender As Object, _
    ByVal e As System.EventArgs)
    'your Code goes here
    end Sub

    When creating your button

    dim cmdMyButton as new button
    with cmdMyButton
    '...
    end with
    myTableCell.Controls.Add(cmdMyButton)
    AddHandler cmdMyButton.Click, AddressOf MyButtonClickHandler

    > The same thing happens with textboxes, where I programatically add
    > ("OnBlur")=mySub
    > The browser raises an error, saying "mySub is undefined". In the aspx
    > codepage mySub is set as Public, by to no help. The browser obviously looks
    > for a client sub called mySub.


    Yes, as OnBlur is a JS-event.

    Cheers,
    Olaf
    Olaf Rabbachin, Dec 30, 2006
    #2
    1. Advertising

  3. "Olaf Rabbachin" <> wrote in message
    news:ebdPy%...
    > Hi,
    >
    > Bjorn Sagbakken wrote:
    >
    >> I am adding buttons and textboxes dynamically into a table, that also
    >> dynamically expands.
    >> So far, so good, actually very nice.
    >> But I am having trouble starting the desired subroutine on postback. To
    >> me
    >> it seems like the button click event looks for a client script instead of
    >> going to the aspx codepage and execute the sub there.

    >
    > that simply depends on what you do. If you want your dynamic button to
    > execute server-side code, then you need to define a handler for it and
    > link
    > your button to that event. Create your event, i.e.:
    >
    > protected Sub MyButtonClickHandler( _
    > ByVal sender As Object, _
    > ByVal e As System.EventArgs)
    > 'your Code goes here
    > end Sub
    >
    > When creating your button
    >
    > dim cmdMyButton as new button
    > with cmdMyButton
    > '...
    > end with
    > myTableCell.Controls.Add(cmdMyButton)
    > AddHandler cmdMyButton.Click, AddressOf MyButtonClickHandler


    Thanks, this makes sense. Only I'm not sure what to put in "AddressOf
    MyButtonClickHandler"; i.e. like the name of the server sub, which in next
    turn have two arguments(ByVal Sender as System.Object, ByVal e as
    EventArgs), where sender should be the button, but what goes in for the
    eventargs?

    >> The same thing happens with textboxes, where I programatically add
    >> ("OnBlur")=mySub
    >> The browser raises an error, saying "mySub is undefined". In the aspx
    >> codepage mySub is set as Public, by to no help. The browser obviously
    >> looks
    >> for a client sub called mySub.

    >
    > Yes, as OnBlur is a JS-event.


    I missed that. I was trying all sorts of things. As soon as I get the button
    to work, I will apply the same idea on the textboxes. What I really was
    after was the OnTextChanged-event posted back. I want to check if the user
    entered a valid partnumber (against a SQL table); if not, I want to make a
    panel visible containing search-help to find the right part.

    With fixed texboxes this is easy, but the dynamically generated ones
    troubled me.
    So if you have more specific code as discussed above, about adding the
    handler I would be most happy.

    Bjorn
    Bjorn Sagbakken, Dec 30, 2006
    #3
  4. Make sure that you are creating the dynamic controls early enough in the
    page lifecycle. One of the most difficult problems with dynamically created
    controls is they need to be created early in the lifecycle in order to catch
    the postback event. Placing them into the pageload event is usually too late
    since when you click on the button and initiate postback, the postback is
    processed before the pageload event. Try creating the dynamic controls in
    the page's init event. That should create them before the postback is called
    so the click event can be handled properly.


    --

    Hope this helps,
    Mark Fitzpatrick
    Former Microsoft FrontPage MVP 199?-2006


    "Bjorn Sagbakken" <> wrote in message
    news:...
    > "Olaf Rabbachin" <> wrote in message
    > news:ebdPy%...
    >> Hi,
    >>
    >> Bjorn Sagbakken wrote:
    >>
    >>> I am adding buttons and textboxes dynamically into a table, that also
    >>> dynamically expands.
    >>> So far, so good, actually very nice.
    >>> But I am having trouble starting the desired subroutine on postback. To
    >>> me
    >>> it seems like the button click event looks for a client script instead
    >>> of
    >>> going to the aspx codepage and execute the sub there.

    >>
    >> that simply depends on what you do. If you want your dynamic button to
    >> execute server-side code, then you need to define a handler for it and
    >> link
    >> your button to that event. Create your event, i.e.:
    >>
    >> protected Sub MyButtonClickHandler( _
    >> ByVal sender As Object, _
    >> ByVal e As System.EventArgs)
    >> 'your Code goes here
    >> end Sub
    >>
    >> When creating your button
    >>
    >> dim cmdMyButton as new button
    >> with cmdMyButton
    >> '...
    >> end with
    >> myTableCell.Controls.Add(cmdMyButton)
    >> AddHandler cmdMyButton.Click, AddressOf MyButtonClickHandler

    >
    > Thanks, this makes sense. Only I'm not sure what to put in "AddressOf
    > MyButtonClickHandler"; i.e. like the name of the server sub, which in next
    > turn have two arguments(ByVal Sender as System.Object, ByVal e as
    > EventArgs), where sender should be the button, but what goes in for the
    > eventargs?
    >
    >>> The same thing happens with textboxes, where I programatically add
    >>> ("OnBlur")=mySub
    >>> The browser raises an error, saying "mySub is undefined". In the aspx
    >>> codepage mySub is set as Public, by to no help. The browser obviously
    >>> looks
    >>> for a client sub called mySub.

    >>
    >> Yes, as OnBlur is a JS-event.

    >
    > I missed that. I was trying all sorts of things. As soon as I get the
    > button to work, I will apply the same idea on the textboxes. What I really
    > was after was the OnTextChanged-event posted back. I want to check if the
    > user entered a valid partnumber (against a SQL table); if not, I want to
    > make a panel visible containing search-help to find the right part.
    >
    > With fixed texboxes this is easy, but the dynamically generated ones
    > troubled me.
    > So if you have more specific code as discussed above, about adding the
    > handler I would be most happy.
    >
    > Bjorn
    >
    Mark Fitzpatrick, Dec 30, 2006
    #4
  5. Thanks! I will do some more testing with your tip in mind.

    Bjorn

    "Mark Fitzpatrick" <> wrote in message
    news:...
    > Make sure that you are creating the dynamic controls early enough in the
    > page lifecycle. One of the most difficult problems with dynamically
    > created controls is they need to be created early in the lifecycle in
    > order to catch the postback event. Placing them into the pageload event is
    > usually too late since when you click on the button and initiate postback,
    > the postback is processed before the pageload event. Try creating the
    > dynamic controls in the page's init event. That should create them before
    > the postback is called so the click event can be handled properly.
    >
    >
    > --
    >
    > Hope this helps,
    > Mark Fitzpatrick
    > Former Microsoft FrontPage MVP 199?-2006
    >
    >
    > "Bjorn Sagbakken" <> wrote in message
    > news:...
    >> "Olaf Rabbachin" <> wrote in message
    >> news:ebdPy%...
    >>> Hi,
    >>>
    >>> Bjorn Sagbakken wrote:
    >>>
    >>>> I am adding buttons and textboxes dynamically into a table, that also
    >>>> dynamically expands.
    >>>> So far, so good, actually very nice.
    >>>> But I am having trouble starting the desired subroutine on postback. To
    >>>> me
    >>>> it seems like the button click event looks for a client script instead
    >>>> of
    >>>> going to the aspx codepage and execute the sub there.
    >>>
    >>> that simply depends on what you do. If you want your dynamic button to
    >>> execute server-side code, then you need to define a handler for it and
    >>> link
    >>> your button to that event. Create your event, i.e.:
    >>>
    >>> protected Sub MyButtonClickHandler( _
    >>> ByVal sender As Object, _
    >>> ByVal e As System.EventArgs)
    >>> 'your Code goes here
    >>> end Sub
    >>>
    >>> When creating your button
    >>>
    >>> dim cmdMyButton as new button
    >>> with cmdMyButton
    >>> '...
    >>> end with
    >>> myTableCell.Controls.Add(cmdMyButton)
    >>> AddHandler cmdMyButton.Click, AddressOf MyButtonClickHandler

    >>
    >> Thanks, this makes sense. Only I'm not sure what to put in "AddressOf
    >> MyButtonClickHandler"; i.e. like the name of the server sub, which in
    >> next turn have two arguments(ByVal Sender as System.Object, ByVal e as
    >> EventArgs), where sender should be the button, but what goes in for the
    >> eventargs?
    >>
    >>>> The same thing happens with textboxes, where I programatically add
    >>>> ("OnBlur")=mySub
    >>>> The browser raises an error, saying "mySub is undefined". In the aspx
    >>>> codepage mySub is set as Public, by to no help. The browser obviously
    >>>> looks
    >>>> for a client sub called mySub.
    >>>
    >>> Yes, as OnBlur is a JS-event.

    >>
    >> I missed that. I was trying all sorts of things. As soon as I get the
    >> button to work, I will apply the same idea on the textboxes. What I
    >> really was after was the OnTextChanged-event posted back. I want to check
    >> if the user entered a valid partnumber (against a SQL table); if not, I
    >> want to make a panel visible containing search-help to find the right
    >> part.
    >>
    >> With fixed texboxes this is easy, but the dynamically generated ones
    >> troubled me.
    >> So if you have more specific code as discussed above, about adding the
    >> handler I would be most happy.
    >>
    >> Bjorn
    >>

    >
    >
    Bjorn Sagbakken, Dec 31, 2006
    #5
  6. Hi,

    Bjorn Sagbakken wrote:

    > Thanks, this makes sense. Only I'm not sure what to put in "AddressOf
    > MyButtonClickHandler"; i.e. like the name of the server sub, which in next
    > turn have two arguments(ByVal Sender as System.Object, ByVal e as
    > EventArgs), where sender should be the button, but what goes in for the
    > eventargs?


    the AddressOf-operator takes the *name* of your event-handler (i.e. the sub
    that is to react to the click-event). In my sample, this is
    "MyButtonClickHandler" and should be replaced by what *you* name your
    handler. Don't worry about the sender and e arguments - these simply
    correspond to the expected structure of click-event handlers. If you do not
    manually raise the event from your code (in which case you could call it as
    in <MyButtonClickHandler(nothing, nothing)> or need to retrieve information
    about the args provided, you don't need to worry about it.
    As Mark stated, make sure you use the Page_Init-event in order to actually
    create your dynamic button (or other dynamic controls).

    >> Yes, as OnBlur is a JS-event.

    >
    > I missed that. I was trying all sorts of things. As soon as I get the button
    > to work, I will apply the same idea on the textboxes. What I really was
    > after was the OnTextChanged-event posted back. I want to check if the user
    > entered a valid partnumber (against a SQL table); if not, I want to make a
    > panel visible containing search-help to find the right part.


    Explore the validator-controls that you find in the toolbox. If you'd like
    to avoid postbacks and the number of possible records to be entered isn't
    all to large, I'd recommend to better provide a DropDownList-control that
    has been filled with the valid numbers. If this isn't possible, you'll have
    to do server-side validation. Again, this can be handled without you
    writing any JavaScript.

    > With fixed texboxes this is easy, but the dynamically generated ones
    > troubled me.


    The principle remains just the same. Simply create a regular textbox and
    then choose the appropriate event, this way the correct event-handler will
    be created. Then delete the textbox again and use the handler as you do for
    your button.

    Cheers,
    Olaf
    Olaf Rabbachin, Dec 31, 2006
    #6
  7. "Olaf Rabbachin" <> wrote in message
    news:...
    > Hi,
    >
    > Bjorn Sagbakken wrote:
    >
    >> Thanks, this makes sense. Only I'm not sure what to put in "AddressOf
    >> MyButtonClickHandler"; i.e. like the name of the server sub, which in
    >> next
    >> turn have two arguments(ByVal Sender as System.Object, ByVal e as
    >> EventArgs), where sender should be the button, but what goes in for the
    >> eventargs?

    >
    > the AddressOf-operator takes the *name* of your event-handler (i.e. the
    > sub
    > that is to react to the click-event). In my sample, this is
    > "MyButtonClickHandler" and should be replaced by what *you* name your
    > handler. Don't worry about the sender and e arguments - these simply
    > correspond to the expected structure of click-event handlers. If you do
    > not
    > manually raise the event from your code (in which case you could call it
    > as
    > in <MyButtonClickHandler(nothing, nothing)> or need to retrieve
    > information
    > about the args provided, you don't need to worry about it.
    > As Mark stated, make sure you use the Page_Init-event in order to actually
    > create your dynamic button (or other dynamic controls).


    Thanks again. I just missed a minor, but essential detail, that "AddressOf"
    actually is an operator that should be in the commend line. I replaced the
    whole thing with my sub's name, which was no good. But now that I keep the
    operator in too, everything works well, both the text-boxes and the buttons.

    Also, this works well when the controls are created at Page_Load. I tried to
    move that to Page_Init and that worked also, only then I got trouble with
    keeping the viewstate of these controls. So I moved back to Page_Load.

    >>> Yes, as OnBlur is a JS-event.

    >>
    >> I missed that. I was trying all sorts of things. As soon as I get the
    >> button
    >> to work, I will apply the same idea on the textboxes. What I really was
    >> after was the OnTextChanged-event posted back. I want to check if the
    >> user
    >> entered a valid partnumber (against a SQL table); if not, I want to make
    >> a
    >> panel visible containing search-help to find the right part.

    >
    > Explore the validator-controls that you find in the toolbox. If you'd like
    > to avoid postbacks and the number of possible records to be entered isn't
    > all to large, I'd recommend to better provide a DropDownList-control that
    > has been filled with the valid numbers. If this isn't possible, you'll
    > have
    > to do server-side validation. Again, this can be handled without you
    > writing any JavaScript.


    I understand your point, and I will re-think if neccessary while the
    application is developing. But so far I find the SQL search to be quick
    enough. Besides; when the user hits a valid partnumber, I want to fill
    several text-boxes on that table-row with parts data, like price, quantity
    on stock, description and so on, so I need more than a list of valid parts.
    Next, if invalid partnumber (the user can type in free-text) there will a
    search to the SQL for suggestions, presented in a listbox for an easy pick
    of the desired part.

    Now that some of the basics are in place, I think these latter issues will
    be solved too.

    (I am actually trying to replace a windows-based order system with a
    web-based one, so there will be a lot of data-lookup, and data in-and out)

    Thanks again.

    Regards
    Bjorn
    Bjorn Sagbakken, Jan 1, 2007
    #7
    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. Fredrik Rodin

    Multiple dynamically added link buttons

    Fredrik Rodin, Jan 9, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    425
    Fredrik Rodin
    Jan 9, 2004
  2. Andy Chambers
    Replies:
    1
    Views:
    384
    Daniel Dyer
    May 14, 2007
  3. Radu
    Replies:
    2
    Views:
    397
  4. Geoff Pennington

    Dynamically added command buttons

    Geoff Pennington, Sep 8, 2003, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    118
    Ken Cox [Microsoft MVP]
    Sep 9, 2003
  5. Fredrik Rodin

    Multiple dynamically added link buttons

    Fredrik Rodin, Jan 9, 2004, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    100
    Fredrik Rodin
    Jan 9, 2004
Loading...

Share This Page