Ayush-projects / team3

0 stars 0 forks source link

aspx #3

Open AvirupBh opened 1 year ago

AvirupBh commented 1 year ago

using BookCrudOperationASP_Net.Data; using System; using System.Collections.Generic; using System.Web.UI; using System.Web.UI.WebControls;

namespace BookCrudOperationASP_Net { public partial class Book : System.Web.UI.Page { public IBooksRepository Repository { get; set; } public Book() { }

    public Book(IBooksRepository booksRepository)
    {
        Repository = booksRepository;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGrid();
            lblInfoMessage.Text = "";
        }

    }
    public void BindGrid()
    {
        //Bind Data to Gridview
        grdBook.DataSource = GetAllBook();
        grdBook.DataBind();
    }
    public List<Data.Book> GetAllBook()
    {
        return Repository.GetAllBooks();
    }

    protected void btnSaveAndUpdate_Click(object sender, EventArgs e)
    {
        Data.Book objBook = new Data.Book();
        objBook.isbn = txtIsbn.Text;
        objBook.Pages = txtPages.Text;
        objBook.Author = txtAuthor.Text;
        objBook.Title = txtTitle.Text;
        objBook.Publisher = txtPublisher.Text;
        if (hdbookId.Value == "")
        {
            Create(objBook);
            lblInfoMessage.Text = "Save Successfully";
        }
        else
        {
            objBook.Id = Convert.ToInt32(hdbookId.Value);
            Update(objBook.Id, objBook);
            lblInfoMessage.Text = "Update Successfully";
            hdbookId.Value = "";
        }
        BindGrid();
        ClearTextBoxes();
    }
    public bool Create(Data.Book objBook)
    {
        return Repository.AddBook(objBook);
    }
    public bool Update(int id, Data.Book objBook)
    {
        objBook.Id=id;
        return Repository.UpdateBook(objBook);
       }

    protected void grdBook_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        BookEditAndDelete(e);

    }
    public void BookEditAndDelete(GridViewCommandEventArgs e)
    {
        GridViewRow gvr = (GridViewRow)((Control)e.CommandSource).NamingContainer;
        int rowIndex = gvr.RowIndex;
        GridViewRow row = grdBook.Rows[rowIndex];

        if (e.CommandName == "editbook")
        {
            txtIsbn.Text = (row.FindControl("lblIsbn") as Label).Text;

            hdbookId.Value = (row.FindControl("lblId") as Label).Text;
            txtTitle.Text = (row.FindControl("lblTitle") as Label).Text;
            txtPublisher.Text = (row.FindControl("lblPub") as Label).Text;
            txtAuthor.Text = (row.FindControl("lblAuth") as Label).Text;
            txtPages.Text = (row.FindControl("lblPages") as Label).Text;

        }
        else if (e.CommandName == "deletebook")
        {
            LinkButton lbkbtn = row.FindControl("lnkDelete") as LinkButton;
            int id = Convert.ToInt32(lbkbtn.CommandArgument);
            if (id > 0)
            {
                DeleteBook(id);
                BindGrid();
                lblInfoMessage.Text = "Book deleted successfully";
            }

        }
    }
    public bool DeleteBook(int id)
    {
        return Repository.DeleteBook(id);
    }
    public void ClearTextBoxes()
    {
        txtIsbn.Text = String.Empty;
        txtTitle.Text = String.Empty;
        txtPublisher.Text = String.Empty;
        txtAuthor.Text = String.Empty;
        txtPages.Text = String.Empty;
    }
}

}