473,383 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Problem catching click event

I have not been able to figure out how to catch the click event from a
button that is added dynamically to a table. In priciple the process is as
follows:

When I first create the page I analyse data and if appropriate:
- add one or more row to a table in the webform
- add cells to the rows
- add a button (myButton) where the user can change some settings.
- enable/disable some other buttons to the form
This is all done in a function called within page_load.

myButton is declared as follows (global in the class):
Protected WithEvents myButton As System.Web.UI.WebControls.Button
and the function is declared:
Private Sub myButton_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles myButton.Click

When the page comes back (IsPostBack) the click event is not caught in my
function.

My guess :
I do not know if there will be any button until I am in the myButton_Click
so it has to be created there. Then as the button is not created when the
event is to be handled noone is there to handle it.

My tests that do not work:
Protected WithEvents myButton As New System.Web.UI.WebControls.Button
(Added New - does not work)
Created the following function called early in page_load
Private Sub NewPage()
' Here we set up what is needed to catch events etc
' =================================================
myButton= New Button
myButton.Text = "Change"
myButton.ID = "myButton"
myButton.Attributes.Add("Runat", "Server")
End Sub
Does not work as is and not with other ID and/or without the added
attributes

So how should ths be done?
Kjell K
Nov 21 '05 #1
2 1311
You need to add the event to the button manually, something like this:
AddHandler myButton.Click, AddressOf myButton_Click
"Kjell Kristiansson" <kj******@hotmail.com> wrote in message
news:yw*********************@newsc.telia.net...
I have not been able to figure out how to catch the click event from a
button that is added dynamically to a table. In priciple the process is as
follows:

When I first create the page I analyse data and if appropriate:
- add one or more row to a table in the webform
- add cells to the rows
- add a button (myButton) where the user can change some settings.
- enable/disable some other buttons to the form
This is all done in a function called within page_load.

myButton is declared as follows (global in the class):
Protected WithEvents myButton As System.Web.UI.WebControls.Button
and the function is declared:
Private Sub myButton_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles myButton.Click

When the page comes back (IsPostBack) the click event is not caught in my
function.

My guess :
I do not know if there will be any button until I am in the myButton_Click
so it has to be created there. Then as the button is not created when the
event is to be handled noone is there to handle it.

My tests that do not work:
Protected WithEvents myButton As New System.Web.UI.WebControls.Button
(Added New - does not work)
Created the following function called early in page_load
Private Sub NewPage()
' Here we set up what is needed to catch events etc
' =================================================
myButton= New Button
myButton.Text = "Change"
myButton.ID = "myButton"
myButton.Attributes.Add("Runat", "Server")
End Sub
Does not work as is and not with other ID and/or without the added
attributes

So how should ths be done?
Kjell K

Nov 21 '05 #2

Forgot to mention - but I have tested that too. Does not help.

I start to fear that this is by design ;-(

It seem like the control (the button myButton) has to be hooked into the
"webForm" hierarchy to receive the event. I have not tested that yet as that
is against the design.

Can anyone confirm this?
Is there any simple work-around?

Kjell K

"Robin Tucker" <id*************************@reallyidont.com> wrote in
message news:d5*******************@news.demon.co.uk...
You need to add the event to the button manually, something like this:
AddHandler myButton.Click, AddressOf myButton_Click
"Kjell Kristiansson" <kj******@hotmail.com> wrote in message
news:yw*********************@newsc.telia.net...
I have not been able to figure out how to catch the click event from a
button that is added dynamically to a table. In priciple the process is as follows:

When I first create the page I analyse data and if appropriate:
- add one or more row to a table in the webform
- add cells to the rows
- add a button (myButton) where the user can change some settings.
- enable/disable some other buttons to the form
This is all done in a function called within page_load.

myButton is declared as follows (global in the class):
Protected WithEvents myButton As System.Web.UI.WebControls.Button
and the function is declared:
Private Sub myButton_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles myButton.Click

When the page comes back (IsPostBack) the click event is not caught in my function.

My guess :
I do not know if there will be any button until I am in the myButton_Click so it has to be created there. Then as the button is not created when the event is to be handled noone is there to handle it.

My tests that do not work:
Protected WithEvents myButton As New System.Web.UI.WebControls.Button
(Added New - does not work)
Created the following function called early in page_load
Private Sub NewPage()
' Here we set up what is needed to catch events etc
' =================================================
myButton= New Button
myButton.Text = "Change"
myButton.ID = "myButton"
myButton.Attributes.Add("Runat", "Server")
End Sub
Does not work as is and not with other ID and/or without the added
attributes

So how should ths be done?
Kjell K


Nov 21 '05 #3

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

Similar topics

9
by: vijay21 | last post by:
hi all, This is about javascript window events. I have a simple html file like this <html> <head> </head> <body onmousedown="alert()"> <form> <input type="text">
5
by: Ron L | last post by:
I have an MDI application with a number of child windows. In each child window I am catching the Closing event and having the child window decide if it should set cancel to true. The intent here...
3
by: Ajay Bhalerao | last post by:
I am moving various control on datagrid by catching currentcellchanged event and leave event of corresponding control.However problem is i have to click first on a particular cell to move a...
0
by: Derrick | last post by:
Is there any way to catch a right mouse click on a menu item? The "click" event seems to only occur on left click. I'm writing an ie favorites like dynamic menu, want users to be able to right...
2
by: Sakharam Phapale | last post by:
Hi All, I am working on a project, where maximum operations carried out on Files and multi-dimensional arrays. Since array data is huge application takes too much memory. My problem is, after...
1
by: Nick Lewis | last post by:
I'm trying to process an event raised by a user control in the web form that contains that control. I've fathomed out how to handle the event within the control but how can I then pass it on to...
23
by: VB Programmer | last post by:
Variable scope doesn't make sense to me when it comes to Try Catch Finally. Example: In order to close/dispose a db connection you have to dim the connection outside of the Try Catch Finally...
2
by: Mac via DotNetMonster.com | last post by:
Hi all, In my VB windows application, I have created a User Control that contains a few buttons and a text box. This control is then placed on several forms. What I would like to do is capture...
5
by: Paul Bromley | last post by:
For some time now I have been struggling trying to understand how to handle events originating in a User Control that I have designed when using this in an application. Basically I need to respond...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.