Search code examples
asp.neteventsdetailsview

How to modify input data in ItemUpdating Event of Detailsview?


Can you direct me how can i access input data of DetailsView in ItemUpdating event ?
I want do some modification on data that user input to Detailsview . Thank you


Solution

  • The DetailsView control's ItemUpdating event has arguments that contain both the original data (if available) as well as the new data that the user typed in. Here's an example of how to check the data and optionally modify it:

    private void OnDetailsViewItemUpdating(object sender, DetailsViewUpdateEventArgs e) {
        if (String.Equals((string)e.NewValues["firstName"], "john", StringComparison.OrdinalIgnoreCase)) {
            // "John" is not a valid name, so change it to "Steve":
            e.NewValues["firstName"] = "Steve";
        }
        if (String.Equals((string)e.NewValues["lastName"], "doe", StringComparison.OrdinalIgnoreCase)) {
            // If "Doe" is the last name, cancel the whole operation
            e.Cancel = true;
        }
    }
    

    See MSDN for more info on the DetailsViewUpdateEventArgs type.