Cancellazione multipla con Repeater e ObjectDataSource
Sempre a seguito di una domanda su NG ho scritto un breve esempio che mostra come eseguire la cancellazione multipla di "record" usando un Repeaer e un'ObjectDataSource.

L'esempio è composto da un'entity Student e da una classe che si occupa della persistenza StudentDataSource.

Qui è possibile scaricare l'esempio, di seguito il codice.

Classe Student:
   1:  public class Student
   2:  {
   3:      public Student() { }
   4:   
   5:      public Student(Guid id, string name) {
   6:          this.id = id;
   7:          this.name = name;
   8:      }
   9:   
  10:      Guid id;
  11:      public Guid Id
  12:      {
  13:          get { return id; }
  14:          set { id = value; }
  15:      }
  16:      
  17:      string name;
  18:      public string Name
  19:      {
  20:          get { return name; }
  21:          set { name = value; }
  22:      }
  23:   
  24:      public override bool Equals(object obj) {
  25:          if (obj == null || GetType() != obj.GetType())
  26:              return false;
  27:   
  28:          Student other = obj as Student;
  29:          return (this.Id.Equals(other.Id));
  30:      }
  31:   
  32:      public override int GetHashCode() {
  33:          return Id.GetHashCode();
  34:      }
  35:  }

Classe StudentDataSource:
   1:  public class StudentDataSource
   2:  {
   3:      IList<Student> Students {
   4:          get {
   5:              if (HttpContext.Current.Cache["Students"] == null) {
   6:                  HttpContext.Current.Cache["Students"] = new List<Student>();
   7:                  Fill();
   8:              }
   9:              return HttpContext.Current.Cache["Students"] as IList<Student>; 
  10:          }        
  11:      }
  12:   
  13:      void Fill() {
  14:          Students.Add(new Student(Guid.NewGuid(), "Bill"));
  15:          Students.Add(new Student(Guid.NewGuid(), "Steve"));
  16:          Students.Add(new Student(Guid.NewGuid(), "John"));
  17:          Students.Add(new Student(Guid.NewGuid(), "Martin"));
  18:      }
  19:   
  20:      public IList<Student> GetAll() {
  21:          return Students;
  22:      }
  23:   
  24:      public Student Get(Guid id) {
  25:          Student student = new Student();
  26:          student.Id = id;
  27:          int position = Students.IndexOf(student);
  28:          if (position >= 0)
  29:              return Students[position];
  30:          else
  31:              return null;
  32:      }
  33:   
  34:      public void Delete(Student value) {        
  35:          Students.Remove(value);
  36:      }
  37:  }

Il codice HTML:
   1:  <div>
   2:      <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
   3:          DataObjectTypeName="Student" 
   4:          SelectMethod="GetAll" 
   5:          TypeName="StudentDataSource" />
   6:      <asp:Repeater ID="Repeater1" runat="server" DataSourceID="ObjectDataSource1">
   7:          <ItemTemplate>
   8:              <input type="hidden" id="hidKey" runat="server" value='<%# DataBinder.Eval(Container.DataItem, "Id") %>' />
   9:              <asp:CheckBox ID="chkDelete" runat="server" />
  10:              &nbsp;
  11:              <asp:Label ID="lblName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Name") %>' />
  12:              <br />
  13:          </ItemTemplate>
  14:      </asp:Repeater>
  15:      <br />
  16:      <br />
  17:      <asp:Button ID="btnDelete" runat="server" Text="Delete" OnClick="btnDelete_Click" />
  18:  </div>

Il codice che esegue la cancellazione:
   1:  protected void btnDelete_Click(object sender, EventArgs e)
   2:  {
   3:      StudentDataSource studentDataSource = new StudentDataSource();
   4:   
   5:      foreach (RepeaterItem item in Repeater1.Items) {
   6:          CheckBox chkDelete = item.FindControl("chkDelete") as CheckBox;
   7:          if (chkDelete.Checked) {
   8:              HtmlInputHidden hidKey = item.FindControl("hidKey") as HtmlInputHidden;
   9:              Guid key = new Guid(hidKey.Value);
  10:              Student student = studentDataSource.Get(key);
  11:              Response.Write(student.Name + " deleted<br/>");
  12:              studentDataSource.Delete(student);                
  13:          }
  14:      }
  15:      Repeater1.DataBind();
  16:  }

Matteo Migliore.
2 Comments Filed Under [ ASP.NET ]

Comments

# re: Cancellazione multipla con Repeater e ObjectDataSource
Gravatar Ciao Andrea, mi fa piacere che sia d'aiuto :-).

Grazie a te!
Left by Matteo Migliore on 8/29/2007 7:25 PM
Comments have been closed on this topic.