473,394 Members | 1,742 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,394 software developers and data experts.

Events for programmatically created controls?? how..

Hi all,

I have a form which is programmatically created from reading values from a
database table.

There is a 'form' for each DocumentType - when I say form I mean as in a
different form will be displayed depending on the DocumentType.

If the querystring contains the DocumenType then the drop down menu is
removed and a text box (readonly) is put in its place to show the users what
the DocumentType is, I have to cover the possibility that a DocumentType may
not be specified in the querystring, and therefore if its not there the drop
down menu is displayed with its various options, again from the database.

All of the above is fine - I have this working - my problem is that I now
need to have a "when somethings changed" event for this object - but I cant
add one in the normal fasion for a control that you may have dragged and
dropped onto the page because in the code view the control doesn't exist
yet! Can someone please advise me as to how I can create the relevant event
(I think its SelectedIndex_Changed or something isn't it?). It will
obviously only be called when it is visible on the page.

Any information would be appreciated.

Regards

Rob
Nov 18 '05 #1
9 1377
On Tue, 10 Aug 2004 13:01:12 +0100, Rob Meade wrote:
Hi all,

I have a form which is programmatically created from reading values from a
database table.

There is a 'form' for each DocumentType - when I say form I mean as in a
different form will be displayed depending on the DocumentType.

If the querystring contains the DocumenType then the drop down menu is
removed and a text box (readonly) is put in its place to show the users what
the DocumentType is, I have to cover the possibility that a DocumentType may
not be specified in the querystring, and therefore if its not there the drop
down menu is displayed with its various options, again from the database.

All of the above is fine - I have this working - my problem is that I now
need to have a "when somethings changed" event for this object - but I cant
add one in the normal fasion for a control that you may have dragged and
dropped onto the page because in the code view the control doesn't exist
yet! Can someone please advise me as to how I can create the relevant event
(I think its SelectedIndex_Changed or something isn't it?). It will
obviously only be called when it is visible on the page.

Any information would be appreciated.

Regards

Rob


when you create a control by dragndrop it is declared with "WithEvents"
keyword to implement it programaticly you should use addhandler:

Dim txt As new TextBox
AddHandler txt.TextChanged, AddressOf txt_TextChanged

Public Sub txt_TextChanged(ByVal sender As Object, ByVal e As EventArgs)

End Sub

hope this helps

-Oytun YILMAZ
Nov 18 '05 #2
"Oytun YILMAZ" wrote ...
Dim txt As new TextBox
AddHandler txt.TextChanged, AddressOf txt_TextChanged

Public Sub txt_TextChanged(ByVal sender As Object, ByVal e As EventArgs)

End Sub

hope this helps

-Oytun YILMAZ


Hi there Oytun,

Many thanks for your reply - I had actually tried something similar myself
yet it doesn't seem to work...

I've not pasted in all of the code as the section in question is in a rather
large set of 'If-thens' etc...however I believe the relevant stuff's below..
Here's the code that creates my drop down menu:

Dim lstDataItem As DropDownList

' create a new instances of our objects
lstDataItem = New DropDownList
documentTypes = New SortedList

' in this area I add to documentTypes all of the key/values from my
database table to use as the source below...

' set list box properties
lstDataItem.ID = m_params.GetParameterByIndex(intLoop).m_objectName '
this sets it to = "txtDocumentType"
lstDataItem.CssClass = "txt"
lstDataItem.DataSource = documentTypes
lstDataItem.DataTextField = "Key"
lstDataItem.DataValueField = "Value"
lstDataItem.DataBind()

' add event handler
AddHandler lstDataItem.SelectedIndexChanged, AddressOf
txtDocumentType_SelectedIndexChanged
Here's the code that I have added for the event handler:

Private Sub txtDocumentType_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs)

lblTest.Text = "it works!"

End Sub


Does the above look correct to you - have I missed something daft..

I've tried it but nothing happens the label never updates - and I tried it
with a messagebox too but it never appears - I assumed it wasn't calling the
event handler..

Any further help would be really appreciated.

Regards

Rob
Nov 18 '05 #3
On Tue, 10 Aug 2004 13:39:46 +0100, Rob Meade wrote:
"Oytun YILMAZ" wrote ...
Dim txt As new TextBox
AddHandler txt.TextChanged, AddressOf txt_TextChanged

Public Sub txt_TextChanged(ByVal sender As Object, ByVal e As EventArgs)

End Sub

hope this helps

-Oytun YILMAZ


Hi there Oytun,

Many thanks for your reply - I had actually tried something similar myself
yet it doesn't seem to work...

I've not pasted in all of the code as the section in question is in a rather
large set of 'If-thens' etc...however I believe the relevant stuff's below..
Here's the code that creates my drop down menu:

Dim lstDataItem As DropDownList

' create a new instances of our objects
lstDataItem = New DropDownList
documentTypes = New SortedList

' in this area I add to documentTypes all of the key/values from my
database table to use as the source below...

' set list box properties
lstDataItem.ID = m_params.GetParameterByIndex(intLoop).m_objectName '
this sets it to = "txtDocumentType"
lstDataItem.CssClass = "txt"
lstDataItem.DataSource = documentTypes
lstDataItem.DataTextField = "Key"
lstDataItem.DataValueField = "Value"
lstDataItem.DataBind()

' add event handler
AddHandler lstDataItem.SelectedIndexChanged, AddressOf
txtDocumentType_SelectedIndexChanged
Here's the code that I have added for the event handler:

Private Sub txtDocumentType_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs)

lblTest.Text = "it works!"

End Sub


Does the above look correct to you - have I missed something daft..

I've tried it but nothing happens the label never updates - and I tried it
with a messagebox too but it never appears - I assumed it wasn't calling the
event handler..

Any further help would be really appreciated.

Regards

Rob


Hi Rob,

The problem is about dropdownlist's lifecycle I store in in a session
variable the delegate function worked with success you may try something
different than session.. Here is the working codeÇ


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
Dim lstDataItem As DropDownList

' create a new instances of our objects
lstDataItem = New DropDownList

lstDataItem.Items.Add("test")
lstDataItem.Items.Add("test2")
lstDataItem.Items.Add("test3")
lstDataItem.AutoPostBack = True

' add event handler
AddHandler lstDataItem.SelectedIndexChanged, AddressOf
txtDocumentType_SelectedIndexChanged
Session("dummy") = lstDataItem

End If

PlaceHolder1.Controls.Add(Session("dummy"))
End Sub

Private Sub txtDocumentType_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs)

CType(Session("dummy"), DropDownList).Items.Add("OYTUN")
End Sub

'Happy Coding

- Oytun YILMAZ
Nov 18 '05 #4
Hi Oytun,

I'm sorry you've lost me a bit now - I'm new to .Net, making a little
progress I think - but you lost me with the life cycle stuff...

Could you explain it a little - why is it that I need to store the drop down
list in a session variable?

Thanks again for any help - sorry if I seem a bit slow with this - just
trying to find my feet etc...

Regards

Rob
Nov 18 '05 #5
On Tue, 10 Aug 2004 14:31:24 +0100, Rob Meade wrote:
Hi Oytun,

I'm sorry you've lost me a bit now - I'm new to .Net, making a little
progress I think - but you lost me with the life cycle stuff...

Could you explain it a little - why is it that I need to store the drop down
list in a session variable?

Thanks again for any help - sorry if I seem a bit slow with this - just
trying to find my feet etc...

Regards

Rob

You declare the dropdownlist in a sub so after the sub is executed the
dropdownlist variable is no more there since it is a local variable.

Normally in a windows application you declare it as a global variable, but
since web applications are stateless this won't work. After server finishes
sending the response all variables belonging to process is lost.

So in ASP.NET we may use viewstate to store variables in a page scope but
dropdownlist is not serielazible by default so we can't use viewstate.
Because of this I store it in session.

hope I didn't confuse you more :)

-Oytun YILMAZ
Nov 18 '05 #6
"Oytun YILMAZ" wrote ...
You declare the dropdownlist in a sub so after the sub is executed the
dropdownlist variable is no more there since it is a local variable.

Normally in a windows application you declare it as a global variable, but
since web applications are stateless this won't work. After server finishes sending the response all variables belonging to process is lost.

So in ASP.NET we may use viewstate to store variables in a page scope but
dropdownlist is not serielazible by default so we can't use viewstate.
Because of this I store it in session.

hope I didn't confuse you more :)


hehe, nope I think I'm following...

I *think* I had a similar problem with the rest of the code previously which
a colleague helped me out with.

Basically my entire form is generated from a database, when the onLoad is
called I run my buildPage routine which programmatically adds all the
controls - my problem previously was that I couldn't then reference any of
these controls in the sense of : strText = txtTextbox.Text for example -
as it didn't know what the hell i was talking about :o)

The solution to this was to run the 'buildPage' routine again when it was a
post back - this way it seems to know what everything is - I'd have assumed
the drop down would have worked in the same way - but obviously not...or
maybe it does...I dont know...

Would be posted all of the code for this page be of any use?? There is a
*lot* of it ... what you think?

I'm happy to try the session variable just a bit concerned its a different
way than how I manage the rest of the objects - not criticising - just dont
know enough about it myself etc.

Regards

Rob
Nov 18 '05 #7
"Oytun YILMAZ" wrote ...

[code]

Hi Oytun,

I just tried adding this code but it didn't do what I expected...

I assumed from the code that the SelectedIndexChanged event would fire when
I selected something in the drop down menu, and then using your example code
it would added your name as an item in the list? This didn't happen. I
also changed it afterwards to simply update a test label - this also didnt
work. The only thing that has changed is that now the drop down menu
appears in the top left of my page where I dropped the placeholder - other
than that I kinda have the same problem still...

Any ideas?

Regards

Rob
Nov 18 '05 #8
On Tue, 10 Aug 2004 15:00:02 +0100, Rob Meade wrote:
"Oytun YILMAZ" wrote ...
You declare the dropdownlist in a sub so after the sub is executed the
dropdownlist variable is no more there since it is a local variable.

Normally in a windows application you declare it as a global variable, but
since web applications are stateless this won't work. After server

finishes
sending the response all variables belonging to process is lost.

So in ASP.NET we may use viewstate to store variables in a page scope but
dropdownlist is not serielazible by default so we can't use viewstate.
Because of this I store it in session.

hope I didn't confuse you more :)


hehe, nope I think I'm following...

I *think* I had a similar problem with the rest of the code previously which
a colleague helped me out with.

Basically my entire form is generated from a database, when the onLoad is
called I run my buildPage routine which programmatically adds all the
controls - my problem previously was that I couldn't then reference any of
these controls in the sense of : strText = txtTextbox.Text for example -
as it didn't know what the hell i was talking about :o)

The solution to this was to run the 'buildPage' routine again when it was a
post back - this way it seems to know what everything is - I'd have assumed
the drop down would have worked in the same way - but obviously not...or
maybe it does...I dont know...

Would be posted all of the code for this page be of any use?? There is a
*lot* of it ... what you think?

I'm happy to try the session variable just a bit concerned its a different
way than how I manage the rest of the objects - not criticising - just dont
know enough about it myself etc.

Regards

Rob


Hi Rob,

If I was writing such a code I would put all the required type of controls
to page and modify their visibility depending on content type(if you do not
need so much type). This could be a better solution then generating entire
form. (I may be wrong since I don't know the exact stuation) Generating
entire form is a bit like clasical asp.

I must admit that using session is not such a good idea since it stay in
server memory all the time user is connected.

You may mail me the code if you want but It won't help too much...

- Oytun YILMAZ

Nov 18 '05 #9
"Oytun YILMAZ" wrote ...
If I was writing such a code I would put all the required type of controls
to page and modify their visibility depending on content type(if you do not need so much type).
Thats what I'm aiming to do - either load in all of the relevant ones for
that document type - OR - load them all and then hide the ones we dont
want...

The problem is the drop down though - it isn't firing the
selectedindexchanged event - or if it is it isn't being detected - if I
could get this bit to work I could do all of the visible=true/false stuff or
load in the relevent controls.
This could be a better solution then generating entire
form. (I may be wrong since I don't know the exact stuation) Generating
entire form is a bit like clasical asp.
To summarise the application...

Its a document storage application for Clinical letters - patients can have
any number of documents, of various types, each document type (perhaps a
Physio Letter) will have a number of 'properties' - these would be things
like 'appointment date', 'hospital number' and so on - these are all stored
in a table in sql server.

When a user comes into the application they will either be sending a
document type in the querystring (as the letter is created in Word and sent
to the web app from a macro) OR - if for some reason this doesn't happen
(some one else is writing the Word stuff) - then the drop down menu must
allow them to select the document type (ie, Physio Letter) - upon selecting
this, all of the properties should appear on the page with their relevant
controls (textboxes and so on) which are also stored in the database.

So - I run off and get all the properties, then start adding table rows,
table cells and controls to these cells, most of the controls are text
boxes, but their ID's are stored in the database table as well...so - the
controls are plonked on the page, and its all looking good - I then get the
user to fill in any missing details which didnt also come across in the
querystring from the word macro - once submitted it iterates back through
the same document properties sent from sql server - and takes the values
from each control on the form where there's a value and writes it to a table
in sql server.

These values would then be used to search against and display various
details about the documents.
I must admit that using session is not such a good idea since it stay in
server memory all the time user is connected.
indeed
You may mail me the code if you want but It won't help too much...


I've sent 2 files to your email address viewable from this group - hope
thats ok - any problems let me know...I couldnt send all of it because some
stuff is locked within source safe and we cant send .zip files (in or out of
our organisation).

thanks for any help you can offer - for reference the code we have been
discussing is in the 'AddDocument.aspx.vb' file - the function is the
buildDocumentDataItems - and the event handler is near the bottom of the
code.

Thanks again

Rob
Nov 18 '05 #10

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

Similar topics

4
by: blue | last post by:
I have a drop-down list, a radio button list and a submit button. I'm adding these controls to a table and I'm adding the table to a Placeholder. I'm adding it to the Placeholder because I don't...
2
by: RAJ | last post by:
In our multi-tier application, we have several ASP.NET user controls which will update the same data source provided by middle tier logic. In this particular scenario we have one user control...
0
by: Scott McChesney | last post by:
I have a problem I hope you folks can help me with. I have an application that is using a tab-based interface, with the ability for users to drag an item from a ListBox onto the tab control. ...
5
by: snesbit | last post by:
If a screen is made up of several user controls and those user controls contain various packaged or standard controls such as a grid, how do you raise both standard and custom events from the user...
8
by: Xero | last post by:
When my program is launched, there is a textbox on the form. When the user enters a character into the textbox (TextChanged), a second, declared textbox is added using this block of code. Dim...
5
by: Steve Moreno | last post by:
Hi all, I've got a web form that I've written code to create an array of DropDownList controls on the page depending on how many records are pulled back. The code to create the controls is...
3
by: Jose Fernandez | last post by:
Hello. I would like to know how could i get all the subscriptions that my form has with the events of their controls. For example. I have a form with a textbox, a button and a dropdown. I...
8
by: hunanwarrior | last post by:
I added textbox controls to a form when user selects amount to create from a combobox as follows: 'Load up the combobox Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As...
1
by: Klaus Jensen | last post by:
Subject: Handling events of controls created at runtime I need to render some buttons at runtime based on a database, tie events to them, and handle the events when they fire. In Page_Load I...
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...
0
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...

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.