473,545 Members | 1,773 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Buttons added dynamically, different behaviour

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")=mySu b
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=Serv er" 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
Dec 30 '06 #1
6 1482
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 MyButtonClickHa ndler( _
ByVal sender As Object, _
ByVal e As System.EventArg s)
'your Code goes here
end Sub

When creating your button

dim cmdMyButton as new button
with cmdMyButton
'...
end with
myTableCell.Con trols.Add(cmdMy Button)
AddHandler cmdMyButton.Cli ck, AddressOf MyButtonClickHa ndler
The same thing happens with textboxes, where I programatically add
("OnBlur")=mySu b
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
Dec 30 '06 #2
"Olaf Rabbachin" <Ol*********@In tuiDev.comwrote in message
news:eb******** ********@TK2MSF TNGP04.phx.gbl. ..
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 MyButtonClickHa ndler( _
ByVal sender As Object, _
ByVal e As System.EventArg s)
'your Code goes here
end Sub

When creating your button

dim cmdMyButton as new button
with cmdMyButton
'...
end with
myTableCell.Con trols.Add(cmdMy Button)
AddHandler cmdMyButton.Cli ck, AddressOf MyButtonClickHa ndler
Thanks, this makes sense. Only I'm not sure what to put in "AddressOf
MyButtonClickHa ndler"; 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")=myS ub
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
Dec 30 '06 #3
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" <bj*****@online .nowrote in message
news:HI******** ************@te lenor.com...
"Olaf Rabbachin" <Ol*********@In tuiDev.comwrote in message
news:eb******** ********@TK2MSF TNGP04.phx.gbl. ..
>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 MyButtonClickHa ndler( _
ByVal sender As Object, _
ByVal e As System.EventArg s)
'your Code goes here
end Sub

When creating your button

dim cmdMyButton as new button
with cmdMyButton
'...
end with
myTableCell.Co ntrols.Add(cmdM yButton)
AddHandler cmdMyButton.Cli ck, AddressOf MyButtonClickHa ndler

Thanks, this makes sense. Only I'm not sure what to put in "AddressOf
MyButtonClickHa ndler"; 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")=my Sub
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

Dec 30 '06 #4

Thanks! I will do some more testing with your tip in mind.

Bjorn

"Mark Fitzpatrick" <ma******@fitzm e.comwrote in message
news:eF******** ******@TK2MSFTN GP02.phx.gbl...
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" <bj*****@online .nowrote in message
news:HI******** ************@te lenor.com...
>"Olaf Rabbachin" <Ol*********@In tuiDev.comwrote in message
news:eb******* *********@TK2MS FTNGP04.phx.gbl ...
>>Hi,

Bjorn Sagbakken wrote:

I am adding buttons and textboxes dynamically into a table, that also
dynamicall y 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 MyButtonClickHa ndler( _
ByVal sender As Object, _
ByVal e As System.EventArg s)
'your Code goes here
end Sub

When creating your button

dim cmdMyButton as new button
with cmdMyButton
'...
end with
myTableCell.C ontrols.Add(cmd MyButton)
AddHandler cmdMyButton.Cli ck, AddressOf MyButtonClickHa ndler

Thanks, this makes sense. Only I'm not sure what to put in "AddressOf
MyButtonClickH andler"; 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")=m ySub
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


Dec 31 '06 #5
Hi,

Bjorn Sagbakken wrote:
Thanks, this makes sense. Only I'm not sure what to put in "AddressOf
MyButtonClickHa ndler"; 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
"MyButtonClickH andler" 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 <MyButtonClickH andler(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
Dec 31 '06 #6
"Olaf Rabbachin" <Ol*********@In tuiDev.comwrote in message
news:ue******** ******@TK2MSFTN GP03.phx.gbl...
Hi,

Bjorn Sagbakken wrote:
>Thanks, this makes sense. Only I'm not sure what to put in "AddressOf
MyButtonClickH andler"; 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
"MyButtonClickH andler" 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 <MyButtonClickH andler(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
Jan 1 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1118
by: RussC | last post by:
My C# app has a toolbar with 12 or so buttons on it, all created via the designer. In some circumstances we need to add one or two buttons to it dynamically. I have code that does this and it works fine -- provided that the buttons are added "early enough", that is, before the Form_Load. If I try to add the buttons after the Form_Load, the...
1
2277
by: andrei | last post by:
Hi Group, I have an aspx form with a variable number of buttons added dynamically, with id's like b01, b02, b03... Whenever I click one of these buttons, the form is posted back. I need to find out which of the buttons triggered the postback *in the Page_Load* - here I have some code that needs to know which button was clicked so it...
2
1935
by: djc | last post by:
On the page_load event I am querying a database and binding data to some text boxes, list boxes, and a repeater control. When the page loads it uses the value of one of the database fields (status) to determine what options should be available for this particular item (which is an issue... small issue tracking system). Each of these options is...
2
1930
by: RN | last post by:
I am dynamically adding a number of HTML buttons to a page... <input type="submit" name="alwaysthesame" id="different1" value="alwaysthesame"> <input type="submit" name="alwaysthesame" id="different2" value="alwaysthesame"> <input type="submit" name="alwaysthesame" id="different3" value="alwaysthesame"> I want them all to have the same...
3
1151
by: benoit | last post by:
using this usercontrol - http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx - I manage to create several inputfields and buttons, depending on what user needs. works fine However, I do not find much useable help on recreating eventhandlers for dynamic generated buttons. All I seem to find is when you know in advance...
3
2178
by: Byron Hopp | last post by:
Anybody have code to add a ToolBar, and its buttons to a MDI Child window. I have added the Toolbar, and added the buttons, but how do you determine what the button is going to execute upon the user clicking it. How do you add this code dynamically? Byron...
22
7926
by: Saul | last post by:
I have a set of radio buttons that are created dynamically, after rendered I try loop thru this set by getting the length of the set, but I keep getting an error stating the element is undefined. I am using getElelementsByName since these are radio buttons, but it seems that the dynamic element is not seen!!! This is my code... please let...
2
1806
by: Radu | last post by:
Hi. I have a page with the following html: __________________________________________________________ <h1>Dynamic Controls Test</h1> <hr /> <asp:TextBox ID="txtProduct1" Text="txtProduct 1" runat="server"></ asp:TextBox> <asp:Button ID="cmdAddRow" Text="+" AccessKey="+" runat="server" /> <asp:Button ID="cmdSubtractRow" Text="-"...
7
6638
by: RichB | last post by:
I am trying to get to grips with the asp.net ajaxcontrol toolkit, and am trying to add a tabbed control to the page. I have no problems within the aspx file, and can dynamically manipulate a tabcontainer which has 1 panel already, however I want to try create the TabPanels dynamically. I followed the advice here: ...
0
7457
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7651
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7802
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
5320
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4941
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3443
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3438
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1010
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
693
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.