Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.ComponentModel;
- using System.Reflection;
- public partial class _Default : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- }
- protected void ObjectDataSource_Inserting(object sender, ObjectDataSourceMethodEventArgs e)
- {
- var contact = e.InputParameters[0] as Contact;
- if (contact == null)
- {
- Validators.Add(new CustomValidator
- {
- ErrorMessage = "Ett fel inträffade.",
- IsValid = false
- });
- e.Cancel = true;
- }
- else if (!contact.IsValid)
- {
- AddErrorMessage(contact);
- e.Cancel = true;
- }
- }
- private void AddErrorMessage(IDataErrorInfo obj)
- {
- var properties = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
- foreach (var property in properties)
- {
- if (!String.IsNullOrWhiteSpace(obj[property.Name]))
- {
- Validators.Add(new CustomValidator
- {
- ErrorMessage = obj[property.Name],
- IsValid = false
- });
- }
- }
- }
- protected void ObjectDataSource_Inserted(object sender, ObjectDataSourceStatusEventArgs e)
- {
- if (e.Exception != null)
- {
- AddErrorMessage("Ett fel inträffade då kontakten skulle läggas till.");
- e.ExceptionHandled = true;
- }
- else
- {
- InsertPanel.Visible = true;
- }
- }
- protected void ObjectDataSource_Updating(object sender, ObjectDataSourceMethodEventArgs e)
- {
- var contact = e.InputParameters[0] as Contact;
- if (contact == null)
- {
- AddErrorMessage("Ett oväntat fel inträffade då kontakten skulle uppdateras.");
- e.Cancel = true;
- }
- else if (!contact.IsValid)
- {
- AddErrorMessage(contact);
- e.Cancel = true;
- }
- }
- protected void ObjectDataSource_Updated(object sender, ObjectDataSourceStatusEventArgs e)
- {
- if (e.Exception != null)
- {
- AddErrorMessage("Ett fel inträffade då kontakten skulle uppdateras.");
- e.ExceptionHandled = true;
- }
- else
- {
- ChangePanel.Visible = true;
- }
- }
- protected void ObjectDataSource_Deleted(object sender, ObjectDataSourceStatusEventArgs e)
- {
- if (e.Exception != null)
- {
- AddErrorMessage("Ett fel inträffade då kontakten skulle tas bort.");
- e.ExceptionHandled = true;
- }
- }
- protected void ObjectDataSource_Selected(object sender, ObjectDataSourceStatusEventArgs e)
- {
- if (e.Exception != null)
- {
- AddErrorMessage("Ett fel inträffade då kontakten hämtades.");
- ListView.Visible = false;
- e.ExceptionHandled = true;
- }
- }
- private void AddErrorMessage(string message)
- {
- var validator = new CustomValidator
- {
- IsValid = false,
- ErrorMessage = message
- };
- Page.Validators.Add(validator);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement