Events for programmatically created controls?? how..

Discussion in 'ASP .Net' started by Rob Meade, Aug 10, 2004.

  1. Rob Meade

    Rob Meade Guest

    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
     
    Rob Meade, Aug 10, 2004
    #1
    1. Advertising

  2. Rob Meade

    Oytun YILMAZ Guest

    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
     
    Oytun YILMAZ, Aug 10, 2004
    #2
    1. Advertising

  3. Rob Meade

    Rob Meade Guest

    "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
     
    Rob Meade, Aug 10, 2004
    #3
  4. Rob Meade

    Oytun YILMAZ Guest

    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
     
    Oytun YILMAZ, Aug 10, 2004
    #4
  5. Rob Meade

    Rob Meade Guest

    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
     
    Rob Meade, Aug 10, 2004
    #5
  6. Rob Meade

    Oytun YILMAZ Guest

    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
     
    Oytun YILMAZ, Aug 10, 2004
    #6
  7. Rob Meade

    Rob Meade Guest

    "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 :eek:)

    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, Aug 10, 2004
    #7
  8. Rob Meade

    Rob Meade Guest

    "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
     
    Rob Meade, Aug 10, 2004
    #8
  9. Rob Meade

    Oytun YILMAZ Guest

    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 :eek:)
    >
    > 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
     
    Oytun YILMAZ, Aug 10, 2004
    #9
  10. Rob Meade

    Rob Meade Guest

    "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
     
    Rob Meade, Aug 10, 2004
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?anRlbmNhdGU=?=

    Click Events in programmatically created GridView Template

    =?Utf-8?B?anRlbmNhdGU=?=, Jul 28, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    810
    =?Utf-8?B?anRlbmNhdGU=?=
    Jul 28, 2006
  2. Jonas

    Programmatically created web user control events

    Jonas, Aug 27, 2003, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    207
    Lucas Tam
    Aug 27, 2003
  3. Replies:
    1
    Views:
    351
    Teemu Keiski
    Apr 17, 2006
  4. Jonas

    Programmatically created web user control events

    Jonas, Aug 27, 2003, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    163
    Lucas Tam
    Aug 27, 2003
  5. Anna Schmidt

    Add events to a programmatically created DG

    Anna Schmidt, Feb 28, 2004, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    134
    Marshal Antony
    Mar 29, 2004
Loading...

Share This Page