Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   ASP .Net (http://www.velocityreviews.com/forums/f29-asp-net.html)
-   -   Events for programmatically created controls?? how.. (http://www.velocityreviews.com/forums/t84544-events-for-programmatically-created-controls-how.html)

Rob Meade 08-10-2004 12:01 PM

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



Oytun YILMAZ 08-10-2004 12:28 PM

Re: Events for programmatically created controls?? how..
 
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

Rob Meade 08-10-2004 12:39 PM

Re: Events for programmatically created controls?? how..
 
"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



Oytun YILMAZ 08-10-2004 01:05 PM

Re: Events for programmatically created controls?? how..
 
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

Rob Meade 08-10-2004 01:31 PM

Re: Events for programmatically created controls?? how..
 
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



Oytun YILMAZ 08-10-2004 01:55 PM

Re: Events for programmatically created controls?? how..
 
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

Rob Meade 08-10-2004 02:00 PM

Re: Events for programmatically created controls?? how..
 
"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



Rob Meade 08-10-2004 02:17 PM

Re: Events for programmatically created controls?? how..
 
"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



Oytun YILMAZ 08-10-2004 02:17 PM

Re: Events for programmatically created controls?? how..
 
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


Rob Meade 08-10-2004 02:40 PM

Re: Events for programmatically created controls?? how..
 
"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




All times are GMT. The time now is 08:48 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.