Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Determining if property changed during postback
Message
 
À
10/12/2003 02:34:20
Keith Payne
Technical Marketing Solutions
Floride, États-Unis
Information générale
Forum:
ASP.NET
Catégorie:
Formulaires Web
Divers
Thread ID:
00855573
Message ID:
00857660
Vues:
21
Hello Keith,

>>
Using this code, you should be able to compare the Checked property with the Boolean value in the DataSource to see if it has changed.
>>

I do use the FindControl method to get the state of the Checked property. My problem is that the datagrid is loaded with a reader and maintained in viewstate between postbacks. I would have to make a call to the Database to get the original values again - something I don't want to do. I probably should be using a DataTable which has built in functionality for managing state changes. But someone else made the decision to not use DataSets on this project.

In the DataGrid I currently add a hidden column with the original values and compare them at postback - that works okay. We use a CheckBoxList in an other place where we have the same problem. For it, I load it, then copy the checked state of the CheckBoxes in the collection to an array that I store in ViewState for later comparison. That works too. I just feel that I am missing a much more elegant way of doing this.

Parsing the ViewState of the controls seeems like the way to go - I jsut can't figure out how to do it.

Thanks,

~dab


>Dean,
>
>I haven't used the ViewState property myself. But it sounds like the root problem is getting a reference the individual checkbox control by using the FindControl method. Here is a snippet from a page that contains a checkbox in a DataGrid to mark row(s) for deletion. It illustrates how to access the properties of the checkbox in each row of the grid:
>
>
>        Dim DataGridRow As DataGridItem
>        For Each DataGridRow In dgListing.Items
>            If CType(DataGridRow.FindControl("chkSelect"), CheckBox).Checked() Then
>                DeleteKeys.Add(CInt(CType(DataGridRow.FindControl("lblListingID"), Label).Text))
>            End If
>        Next
>
>
>This code is called during postback when the user clicks the "Submit" button which is outside of the DataGrid. It examines each row in the DataGrid and adds the Primary Key value of the DataSource to an ArrayList when the checkbox is checked. The PK value is stored in a label in the grid, but you could alternatively use a For i = 0 To dgListing.Items.Count - 1 loop to get the PK value from the DataSource using the index (don't forget to account for paging).
>
>Using this code, you should be able to compare the Checked property with the Boolean value in the DataSource to see if it has changed.
>
>>Thanks Keith, I'm just getting back here to respond. The only way I see the ViewState property of a control is via the Parent property. I that case I get access issues because it is a protected property of the parent class. I've found lots written about the ViewState, but the examples of accessing it are few and tend to be about items manually aded to ViewState. I don't have a problem there. I can work with them fine. Have you retrieved values of controls from ViewState? I'd love to see some code doing it. Again retrieving the original value and the new value.
>>
>>Thanks again for your reply,
>>
>>~dab
>>
>>>>I am not using datasets. Am loading data into web page controls on first pass and using viewstate between posts. I have a datagrid that I've added a checkbox that is bound to a bool value - the datagrid is loaded with a datareader. The only actionable columns are the checkbox and a link column. I want to use a button on the form to update the database if the checkbox.checked property has changed (checked/unchecked).
>>>>
>>>>I only want to write to the database if there is a change.
>>>>
>>>>Is there a way in my codebehind to know if the value of the checkbox in the grid has changed? A reference to the old and new values?
>>>>
>>>>I am making it work with an additional hidden column that is bound to the same field as the checkbox, but there must be a cleaner way, right?
>>>>
>>>>Any help greatly appreciated,
>>>>
>>>>~dab
>>>
>>>Dean,
>>>
>>>You can check the control's ViewState property to see the value that was saved during the postback.
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform