DataChanged
-
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
-
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
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
-
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
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 thatMyControls.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 thevalue
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? -
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
-
Well whats your data source? If this one record or many? 1 line of code equals many bugs. So don't write any!!
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.
-
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.
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!!
-
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!!
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 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.
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!!
-
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.
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!!
-
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!!
-
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 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!!