Buttons added dynamically, different behaviour

B

Bjorn Sagbakken

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
 
O

Olaf Rabbachin

Hi,

Bjorn said:
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
 
B

Bjorn Sagbakken

Olaf Rabbachin said:
Hi,



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?
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
 
M

Mark Fitzpatrick

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.
 
O

Olaf Rabbachin

Hi,

Bjorn said:
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).
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
 
B

Bjorn Sagbakken

Olaf Rabbachin said:
Hi,



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.
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top