473,769 Members | 2,382 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help getting onClick to work with dynamic added control.

3 New Member
When the user clicks a radio button it creates a postback where I add a button control dynamically. My problem is I cannot get the dynamically added button to work with on an click event. The button was created in code behind page.

Expand|Select|Wrap|Line Numbers
  1. Public Sub rosterAddControls()
  2.     Dim myBtn As New Button()
  3.     myBtn.Style.Item("z-index") = "100"
  4.     myBtn.Style.Item("position") = "absolute"
  5.     myBtn.Style.Item("top") = (x - 30) & "px"
  6.     myBtn.Style.Item("left") = y & "px"
  7.     myBtn.ID = "myBtn"
  8.     myBtn.Text = "Button 8974"
  9.     myBtn.OnClientClick = "myBtn_Click()"
  10.     PlaceHolder1.Controls.Add(myBtn)
  11. End Sub
This above code works fine and the button myBtn is displayed when above method is called.
From the help forms that I have read, I gathered I needed to make an AddHandler statement in the Page_Init as shown below.

Expand|Select|Wrap|Line Numbers
  1. Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
  2.     AddHandler myBtn.Click, AddressOf myBtn_Click
  3. End Sub
Lastly, I have created a method that should be called when myBtn is clicked. But the below method is never called.

Expand|Select|Wrap|Line Numbers
  1. Protected Sub myBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs)
  2.     Dim test As String
  3.     test = "this is a test "
  4. End Sub
Can someone please help me get the dynamically added button myBtn to fire a onClick event.

Thanks
lance
Jul 1 '08 #1
4 1970
Frinavale
9,735 Recognized Expert Moderator Expert
The button has a scope that only exists in this sub. The button is added to the page but that's it....
Expand|Select|Wrap|Line Numbers
  1. Public Sub rosterAddControls()
  2. Dim myBtn As New Button()
  3.     myBtn.Style.Item("z-index") = "100"
  4.     myBtn.Style.Item("position") = "absolute"
  5.     myBtn.Style.Item("top") = (x - 30) & "px"
  6.     myBtn.Style.Item("left") = y & "px"
  7.     myBtn.ID = "myBtn"
  8.     myBtn.Text = "Button 8974"
  9.     myBtn.OnClientClick = "myBtn_Click()"
  10.     PlaceHolder1.Controls.Add(myBtn)
  11. End Sub
What you need to do is declare the button outside of this sub. Also, you need to declare it "WithEvents " or else no events will be fired...

eg:


Expand|Select|Wrap|Line Numbers
  1.  
  2. Private WithEvents myBtn As Button
  3.  
  4. Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
  5.     rosterAddControls()
  6. End Sub
  7.  
  8. Public Sub rosterAddControls()
  9.     myBtn = New Button
  10.     myBtn.Style.Item("z-index") = "100"
  11.     myBtn.Style.Item("position") = "absolute"
  12.     myBtn.Style.Item("top") = (x - 30) & "px"
  13.     myBtn.Style.Item("left") = y & "px"
  14.     myBtn.ID = "myBtn"
  15.     myBtn.Text = "Button 8974"
  16.     PlaceHolder1.Controls.Add(myBtn)
  17. End Sub
  18.  
  19.  
Now your button click event will fire:

Expand|Select|Wrap|Line Numbers
  1. Protected Sub myBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles myBtn.Click
  2.     Dim test As String
  3.     test = "this is a test "
  4. End Sub


Cheers!

-Frinny
Jul 1 '08 #2
mcelary
3 New Member
Frinavale thanks for your help.
I really appreciate the time you took and the corrected code you shared. I have been trying to solve this problem for about a week.

Thanks for the excellent help.
Jul 1 '08 #3
Frinavale
9,735 Recognized Expert Moderator Expert
Frinavale thanks for your help.
I really appreciate the time you took and the corrected code you shared. I have been trying to solve this problem for about a week.

Thanks for the excellent help.
Glad to have helped!

Cheers!
Jul 1 '08 #4
mcelary
3 New Member
Again thanks to Frinavale for help on this.
The problem now is being able to save the value of a textbox into a variable I dimed as string "oracleID"
I have added the following code in the Public Sub rosterAddContro ls()
txtEditOracle.S tyle.Item("z-index") = "100"
txtEditOracle.S tyle.Item("posi tion") = "absolute"
txtEditOracle.S tyle.Item("Font-Size") = fntSize & "px"
txtEditOracle.S tyle.Item("top" ) = x & "px"
txtEditOracle.S tyle.Item("left ") = y & "px"
txtEditOracle.V isible = showRosterTools
txtEditOracle.W idth = 40

oracleID = txtOracleID.Tex t.ToString
Problem is when this sub is called ...
Protected Sub myBtn_Click(ByV al sender As Object, ByVal e As System.EventArg s) Handles myBtn.Click
addToDatabase()
End Sub
... the postback calls the page init
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArg s) Handles Me.Init
rosterAddContro ls()
End Sub
... this recreates the textbox with no value. If I do not call "rosterAddContr ols()" then the textbox control is not even created.

any help on this would be appreciated.
thanks
lance
Jul 8 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
2842
by: Alex | last post by:
Hi all, I'm writing a small web application which searches a database based on a date field, and populates a datagrid control with the results. The datagrid control has selection buttons added to it to view additional details about the selected result (a second database query is triggered). I want this second query to pop up in a new window, the way it would if I used "window.open" in javascript. I've added a function in the
5
19666
by: RA | last post by:
I have created a button dynamically; which has been added to a TableCell of a TableRow of a Table control. Is there a way to add onclick event which calls a procedure on the Server-side itself. Any suggestions? I tried with btnAdd.Attributes.Add("onClick","AddItem();")
5
3735
by: Dennis Fazekas | last post by:
Greetings, I am creating a web form which will all the user to add an unlimited number of email addresses. Basically I have 3 buttons, "Add Another Email", "-" to remove, and a "Save" button. When the user clicks the "Add another email" it will call a client side JavaScript function, add_email, which will dynamically add a new set of controls to the webpage using the innerHTML method. It appears to work perfectly fine in the browser. The...
8
1470
by: funkychicken818 | last post by:
hi i have recently created a JavaScript DOM script and well for some reason i t does not work. can you help me find the problem? here is the script <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"
7
2795
by: extremerep | last post by:
My task is to change the value of a button and then make it functional with the onClick handler. Changing the value to "Play Again" works, but making the onClick work accordingly does not. The following is a snippet of the script. What is keeping it from working? function displaycards1(){ document.form1.reveal1.value="Play Again"; document.form1.reveal1.onClick="setcards();"; } </script>
8
3716
by: | last post by:
The problem lies here eval("document.TeeForm.amt.value(S+M)"); S and M suppose to add up and the total suppose to appear on the AMT field but it didn't. Any help? ============================ CODE ==============================================
15
2581
by: Jay | last post by:
I have a multi threaded VB.NET application (4 threads) that I use to send text messages to many, many employees via system.timer at a 5 second interval. Basically, I look in a SQL table (queue) to determine who needs to receive the text message then send the message to the address. Only problem is, the employee may receive up to 4 of the same messages because each thread gets the recors then sends the message. I need somehow to prevent...
36
3110
by: aljamala | last post by:
Hi, I keep getting this warning on a page, but I do not know what the problem is...does anyone have an idea about what could be wrong? line 88 column 7 - Warning: missing </formbefore <td> it highlights this line: <form name="frmCurrency" action="" method="post"> Page source....
0
5575
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted ******************************************************** For this teeny job, please refer to: http://feeds.reddit.com/feed/8fu/?o=25
0
9586
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10210
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9990
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9861
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8869
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7406
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5298
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3561
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.