Guest User

Guest User

  • Tech Writer
  • 271
  • 35.8k

CRUD with Repeater in asp.net Entity Framework

Aug 10 2020 5:42 AM
hello...
 
i want to do CRUD operation with Repeater in asp.net Entity Framework.
  1. <table class="style1">  
  2. <tr>  
  3. <td> CategoryName:</td>  
  4. <td>  
  5. <asp:TextBox ID="TxtCName" runat="server"></asp:TextBox>  
  6. </td>  
  7. <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"  
  8. Style="top:182px; left: 774px; position: absolute; height: 26px; width: 162px"  
  9. ControlToValidate="TxtCName" ErrorMessage="required**" font-size="Small"  
  10. ForeColor ="Red"></asp:RequiredFieldValidator>  
  11. </tr>  
  12. <tr>  
  13. <td>  
  14. <asp:Label ID="LblMesge" runat="server" ></asp:Label>  
  15. </td>  
  16. <td> </td>  
  17. </tr>  
  18. <tr>  
  19. <td>  
  20. <asp:Button ID="Button1" runat="server" Text="Save" class="btn"  
  21. onclick="Button1_Click" />  
  22. </td>  
  23. </tr>  
  24. </table>  
  25. <asp:repeater id="repWalCat" onitemcommand="repWalCat_ItemCommand" runat="server">  
  26. <itemtemplate>  
  27. <table class="style1">  
  28. <tr>  
  29. <td> CategoryName:</td>  
  30. <td>  
  31. <asp:TextBox ID="TxtCName" runat="server"></asp:TextBox>  
  32. </td>  
  33. <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"  
  34. Style="top:182px; left: 774px; position: absolute; height: 26px; width: 162px"  
  35. ControlToValidate="TxtCName" ErrorMessage="required**" font-size="Small"  
  36. ForeColor ="Red"></asp:RequiredFieldValidator>  
  37. </tr>  
  38. </table>  
  39. </itemtemplate></asp:repeater>">  
  40. </div>  
  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3. if (!Page.IsPostBack)  
  4. {  
  5. bindCategoryToRepeater();  
  6. }  
  7. }  
  8. protected void Button1_Click(object sender, EventArgs e)  
  9. {  
  10. Model.WallpaperCategory newusermodel = new Model.WallpaperCategory();  
  11. newusermodel.CategoryName = TxtCName.Text;  
  12. newusermodel.CreatedDate = DateTime.Now;  
  13. newusermodel.UploadedDate = DateTime.Now;  
  14. using (WallpaperEntities7 context = new WallpaperEntities7())  
  15. {  
  16. WallPaperCategry obj = new WallPaperCategry();  
  17. obj.CategoryName = newusermodel.CategoryName;  
  18. obj.CreatedDate = newusermodel.CreatedDate;  
  19. obj.UploadedDate = newusermodel.UploadedDate;  
  20. var cate = context.Set<WallPaperCategry>();  
  21. cate.Add(obj);  
  22. context.SaveChanges();  
  23. }  
  24. bindCategoryToRepeater();  
  25. }  
  26. void bindCategoryToRepeater()  
  27. {  
  28. using (WallpaperEntities7 context = new WallpaperEntities7())  
  29. {  
  30. repWalCat.DataSource = (from r in context.WallPaperCategries  
  31. select r).ToList();  
  32. repWalCat.DataBind();  
  33. }  
  34. }  
  35. protected void repWalCat_ItemCommand(object source, RepeaterCommandEventArgs e)  
  36. {  
  37. }  
  38. }  

Answers (4)