Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.45 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.ComponentModel;
  8. using System.Reflection;
  9.  
  10. public partial class _Default : System.Web.UI.Page
  11. {
  12.     protected void Page_Load(object sender, EventArgs e)
  13.     {
  14.  
  15.     }
  16.  
  17.     protected void ObjectDataSource_Inserting(object sender, ObjectDataSourceMethodEventArgs e)
  18.     {
  19.         var contact = e.InputParameters[0] as Contact;
  20.  
  21.         if (contact == null)
  22.         {
  23.             Validators.Add(new CustomValidator
  24.             {
  25.                 ErrorMessage = "Ett fel inträffade.",
  26.                 IsValid = false
  27.             });
  28.             e.Cancel = true;
  29.            
  30.         }
  31.  
  32.         else if (!contact.IsValid)
  33.         {
  34.             AddErrorMessage(contact);
  35.             e.Cancel = true;
  36.         }
  37.        
  38.     }
  39.         private void AddErrorMessage(IDataErrorInfo obj)
  40.         {
  41.             var properties = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
  42.             foreach (var property in properties)
  43.             {
  44.                  if (!String.IsNullOrWhiteSpace(obj[property.Name]))
  45.             {
  46.                 Validators.Add(new CustomValidator
  47.                 {
  48.                     ErrorMessage = obj[property.Name],
  49.                     IsValid = false
  50.                 });
  51.             }
  52.         }
  53.    
  54.     }
  55.  
  56.     protected void ObjectDataSource_Inserted(object sender, ObjectDataSourceStatusEventArgs e)
  57.     {
  58.         if (e.Exception != null)
  59.         {
  60.             AddErrorMessage("Ett fel inträffade då kontakten skulle läggas till.");
  61.             e.ExceptionHandled = true;
  62.         }
  63.         else
  64.         {
  65.             InsertPanel.Visible = true;
  66.         }
  67.     }
  68.  
  69.     protected void ObjectDataSource_Updating(object sender, ObjectDataSourceMethodEventArgs e)
  70.     {
  71.         var contact = e.InputParameters[0] as Contact;
  72.  
  73.      
  74.         if (contact == null)
  75.         {
  76.             AddErrorMessage("Ett oväntat fel inträffade då kontakten skulle uppdateras.");
  77.             e.Cancel = true;
  78.         }
  79.         else if (!contact.IsValid)
  80.         {
  81.             AddErrorMessage(contact);
  82.             e.Cancel = true;
  83.         }
  84.     }
  85.     protected void ObjectDataSource_Updated(object sender, ObjectDataSourceStatusEventArgs e)
  86.     {
  87.         if (e.Exception != null)
  88.         {
  89.             AddErrorMessage("Ett fel inträffade då kontakten skulle uppdateras.");
  90.             e.ExceptionHandled = true;
  91.         }
  92.         else
  93.         {
  94.             ChangePanel.Visible = true;
  95.         }
  96.     }
  97.     protected void ObjectDataSource_Deleted(object sender, ObjectDataSourceStatusEventArgs e)
  98.     {
  99.         if (e.Exception != null)
  100.         {
  101.             AddErrorMessage("Ett fel inträffade då kontakten skulle tas bort.");
  102.             e.ExceptionHandled = true;
  103.         }
  104.     }
  105.     protected void ObjectDataSource_Selected(object sender, ObjectDataSourceStatusEventArgs e)
  106.     {
  107.  
  108.         if (e.Exception != null)
  109.         {
  110.             AddErrorMessage("Ett fel inträffade då kontakten hämtades.");
  111.             ListView.Visible = false;
  112.  
  113.             e.ExceptionHandled = true;
  114.         }
  115.  
  116.     }
  117.     private void AddErrorMessage(string message)
  118.     {
  119.         var validator = new CustomValidator
  120.         {
  121.             IsValid = false,
  122.             ErrorMessage = message
  123.         };
  124.  
  125.         Page.Validators.Add(validator);
  126.     }
  127.  
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement