Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. DataChanged

DataChanged

Scheduled Pinned Locked Moved ASP.NET
questioncsharpasp-netdatabase
11 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    matsnas
    wrote on last edited by
    #1

    Hi! New to ASP.NET but been fiddeling with VB.NET for a while. I'm loading a number of textfields from a database (Id, PersonName) and dynamically creating a corresponding number of textboxes in a code behind page on the load event. How can I associate the textbox with my primary key field? myTextBox.ID = "Person" & vPersonId or myTextBox.Tag = vPerson?? On a button click event I want to save changes to the database. How do I do this? I can store the original value and the created textboxes in two collections and loop through them to see if the value changed but maby there is a better way? Again how do I know for which "Id" to update? Also using the buttons click event actually does a page postback doesn't it? Do I really want to send the collections back and forth to the webserver? Optimally I'd like something like this: private function SavePersons   for each vTextbox as Textbox in MyPage.Controls     if vTextbox.DataChanged then       vPerson = DirectCast(vTextbox.Tag, Person)       vNewName = vTextbox.Text       UpdatePerson(vPerson.Id, vNewName)     end if   next end function

    T I 2 Replies Last reply
    0
    • M matsnas

      Hi! New to ASP.NET but been fiddeling with VB.NET for a while. I'm loading a number of textfields from a database (Id, PersonName) and dynamically creating a corresponding number of textboxes in a code behind page on the load event. How can I associate the textbox with my primary key field? myTextBox.ID = "Person" & vPersonId or myTextBox.Tag = vPerson?? On a button click event I want to save changes to the database. How do I do this? I can store the original value and the created textboxes in two collections and loop through them to see if the value changed but maby there is a better way? Again how do I know for which "Id" to update? Also using the buttons click event actually does a page postback doesn't it? Do I really want to send the collections back and forth to the webserver? Optimally I'd like something like this: private function SavePersons   for each vTextbox as Textbox in MyPage.Controls     if vTextbox.DataChanged then       vPerson = DirectCast(vTextbox.Tag, Person)       vNewName = vTextbox.Text       UpdatePerson(vPerson.Id, vNewName)     end if   next end function

      T Offline
      T Offline
      ToddHileHoffer
      wrote on last edited by
      #2

      Let me make two suggestions, since simplest is usually best. First of why not use a datagird (1.1) or gridview (2.0) that is if you want the user to be able to update many records at once. Remember this method can cause issues if multiple users are loading a large recordset, by the time they hit save another user could also have updated the records. I suggest updating the records one at a time and pulling back all the fields into textboxes instead of generating them dynamically. There is no harm in having a couple blank textboxes. If you really need to do generate them dynamically, then store the values in a hash table or collection on page load. Then store the hashtable in viewstate so it persists until the postback, on the click event compare the values in the textboxes with those in the ht retreived from the viewstate and update accordingly. If you need any more assistance I can post some code examples tomorrow when I'm back at work. One more thing you'll need a recursive function to loop through all the textboxes in a page because they could be controls within a control. "People who never make mistakes, never do anything." My Blog

      M 1 Reply Last reply
      0
      • T ToddHileHoffer

        Let me make two suggestions, since simplest is usually best. First of why not use a datagird (1.1) or gridview (2.0) that is if you want the user to be able to update many records at once. Remember this method can cause issues if multiple users are loading a large recordset, by the time they hit save another user could also have updated the records. I suggest updating the records one at a time and pulling back all the fields into textboxes instead of generating them dynamically. There is no harm in having a couple blank textboxes. If you really need to do generate them dynamically, then store the values in a hash table or collection on page load. Then store the hashtable in viewstate so it persists until the postback, on the click event compare the values in the textboxes with those in the ht retreived from the viewstate and update accordingly. If you need any more assistance I can post some code examples tomorrow when I'm back at work. One more thing you'll need a recursive function to loop through all the textboxes in a page because they could be controls within a control. "People who never make mistakes, never do anything." My Blog

        M Offline
        M Offline
        matsnas
        wrote on last edited by
        #3

        I'm allergic to everything called databinding and that's probably why I haven't dug deeper into the datagrid but I'll have a look. However my actual UI layout is more complex than the fictive (Id, Person) I used to present my problem. I have a questionary form with freetext fields, radio and checkboxes. Perhaps the datagrid supports these as well. Static number of controls is out of the question as it may wary from 1 to 100 (theroeticaly ;)). Correct me if I'm wrong but doesn't the click event also perform a postback? I'm trying to avoid too many server round trips. I'd like the user to be able to work with the entire form and on save update the database. Yes, I guessed that MyPage.Controls would not work and that I might need a recursive function. But instead I'm thinking of putting the dynamically created controls in a hash and loop that MyControls.Item.Count. From your reply I guess that there is no way to associate a control with the object that represent it's value? Can I subclass the <asp:textbox... to implement a "Public Property Tag as Object" attribute? Where's the value attribut on the HTML checkbox control? Maby I don't see the whole picture but isn't it a huge disadvantage to not be able to catch a data changed property?

        1 Reply Last reply
        0
        • M matsnas

          Hi! New to ASP.NET but been fiddeling with VB.NET for a while. I'm loading a number of textfields from a database (Id, PersonName) and dynamically creating a corresponding number of textboxes in a code behind page on the load event. How can I associate the textbox with my primary key field? myTextBox.ID = "Person" & vPersonId or myTextBox.Tag = vPerson?? On a button click event I want to save changes to the database. How do I do this? I can store the original value and the created textboxes in two collections and loop through them to see if the value changed but maby there is a better way? Again how do I know for which "Id" to update? Also using the buttons click event actually does a page postback doesn't it? Do I really want to send the collections back and forth to the webserver? Optimally I'd like something like this: private function SavePersons   for each vTextbox as Textbox in MyPage.Controls     if vTextbox.DataChanged then       vPerson = DirectCast(vTextbox.Tag, Person)       vNewName = vTextbox.Text       UpdatePerson(vPerson.Id, vNewName)     end if   next end function

          I Offline
          I Offline
          Ista
          wrote on last edited by
          #4

          Well whats your data source? If this one record or many? 1 line of code equals many bugs. So don't write any!!

          M 1 Reply Last reply
          0
          • I Ista

            Well whats your data source? If this one record or many? 1 line of code equals many bugs. So don't write any!!

            M Offline
            M Offline
            matsnas
            wrote on last edited by
            #5

            It does not matter what the datasource is, for the question it could just as well have been a flat file. My question is how do I associate an asp control with a specific value object (or primary key) so that I can verify if the user have changed the value and I need to update the data source.

            I 1 Reply Last reply
            0
            • M matsnas

              It does not matter what the datasource is, for the question it could just as well have been a flat file. My question is how do I associate an asp control with a specific value object (or primary key) so that I can verify if the user have changed the value and I need to update the data source.

              I Offline
              I Offline
              Ista
              wrote on last edited by
              #6

              Well you would need to trap the change. So your data source should be aware. CollectionBase has the framework for these. You must notify through several events to the asp control what is happening with the source. So it does actually matter what your source is, because if its not aware of changes, how can the control? If your data source has data changed notifications then it will work just fine. Nick 1 line of code equals many bugs. So don't write any!!

              M 1 Reply Last reply
              0
              • I Ista

                Well you would need to trap the change. So your data source should be aware. CollectionBase has the framework for these. You must notify through several events to the asp control what is happening with the source. So it does actually matter what your source is, because if its not aware of changes, how can the control? If your data source has data changed notifications then it will work just fine. Nick 1 line of code equals many bugs. So don't write any!!

                M Offline
                M Offline
                matsnas
                wrote on last edited by
                #7

                I think you have got it backwards. I wan't to update the data source with what the user have done in the textbox. I fail to see why it matters if the data source is an Oracle, MsAccess, SQL server och even an xml-file or csv-file? The "code behind" should be the logic in the application not the datasouce. In fact I should be able to change datasource at any given time.

                I 2 Replies Last reply
                0
                • M matsnas

                  I think you have got it backwards. I wan't to update the data source with what the user have done in the textbox. I fail to see why it matters if the data source is an Oracle, MsAccess, SQL server och even an xml-file or csv-file? The "code behind" should be the logic in the application not the datasouce. In fact I should be able to change datasource at any given time.

                  I Offline
                  I Offline
                  Ista
                  wrote on last edited by
                  #8

                  to tie into into a datasource call you textbox.Databindings.add( property, object); But the data source is basically and mostly an IList. Whether its a file, table, xml it doesn't matter it is inherently some sort of IList. I think your confused about a data source and how data bound controls ineract with a data source. With complex data controls you MUST tell the control whether data has changed But effectively the data source knows all and if the events are propagated correctly the form can know also 1 line of code equals many bugs. So don't write any!!

                  M 1 Reply Last reply
                  0
                  • M matsnas

                    I think you have got it backwards. I wan't to update the data source with what the user have done in the textbox. I fail to see why it matters if the data source is an Oracle, MsAccess, SQL server och even an xml-file or csv-file? The "code behind" should be the logic in the application not the datasouce. In fact I should be able to change datasource at any given time.

                    I Offline
                    I Offline
                    Ista
                    wrote on last edited by
                    #9

                    And to add. VS2003 doesn't have a mechanism to set the data back. If the data source extends the collectionbase then your can do a cross talk with a data grid. You can, using an observer design pattern, simulate this. VS2005 caomes with this support for databinding. 1 line of code equals many bugs. So don't write any!!

                    1 Reply Last reply
                    0
                    • I Ista

                      to tie into into a datasource call you textbox.Databindings.add( property, object); But the data source is basically and mostly an IList. Whether its a file, table, xml it doesn't matter it is inherently some sort of IList. I think your confused about a data source and how data bound controls ineract with a data source. With complex data controls you MUST tell the control whether data has changed But effectively the data source knows all and if the events are propagated correctly the form can know also 1 line of code equals many bugs. So don't write any!!

                      M Offline
                      M Offline
                      matsnas
                      wrote on last edited by
                      #10

                      Ah, that explains your replies. You are assuming that I'm using databinding. Personally I am allergic to databinding and further it doesn't fit well with our value object based corporate development platform. Thanks anyway!

                      I 1 Reply Last reply
                      0
                      • M matsnas

                        Ah, that explains your replies. You are assuming that I'm using databinding. Personally I am allergic to databinding and further it doesn't fit well with our value object based corporate development platform. Thanks anyway!

                        I Offline
                        I Offline
                        Ista
                        wrote on last edited by
                        #11

                        I wouldn't say value based. That would relate to a function based company. If it were object based you would have Collections and tie to them and save tons of extra coding. But thats from an object point of view, instead of a PL1 perspective. You should fight databinding, its the object oriented way of represnting a datasource. Good luck with re-writing the wheel. But, If it is object based you would use an observer pattern if you dont like MS's databinding. Either way your trying to re-create databinding This whole question has been odd 1 line of code equals many bugs. So don't write any!!

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups