protected void grdvBook_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lblBookId = (Label)e.Row.FindControl("lblBookId");
Label lblAuthor = (Label)e.Row.FindControl("lblAuthor");
Label lblISBN = (Label)e.Row.FindControl("lblISBN");
Label lblYear = (Label)e.Row.FindControl("lblYear");
Label lblTitle = (Label)e.Row.FindControl("lblTitle");
Label lblEdition = (Label)e.Row.FindControl("lblEdition");
ImageButton imgEdit = (ImageButton)e.Row.FindControl("imgEdit");
ImageButton imgDelte = (ImageButton)e.Row.FindControl("imgDelte");
if (lblBookId != null)
lblBookId.Text = ((LINQ.Book)e.Row.DataItem).BookId.ToString();
if (lblAuthor != null)
lblAuthor.Text = ((LINQ.Book)e.Row.DataItem).Author.ToString();
if (lblISBN != null)
lblISBN.Text = ((LINQ.Book)e.Row.DataItem).ISBN.ToString();
if (lblYear != null)
lblYear.Text = ((LINQ.Book)e.Row.DataItem).Year.ToString();
if (lblTitle != null)
lblTitle.Text = ((LINQ.Book)e.Row.DataItem).Title.ToString();
if (lblEdition != null)
lblEdition.Text = ((LINQ.Book)e.Row.DataItem).Edition.ToString();
if (imgEdit != null)
{
imgEdit.CommandArgument = ((LINQ.Book)e.Row.DataItem).BookId.ToString();
imgEdit.CommandName = "Edit";
}
if (imgDelte != null)
{
imgDelte.CommandArgument = ((LINQ.Book)e.Row.DataItem).BookId.ToString();
imgDelte.CommandName = "Delete";
}
TextBox txtBookId = (TextBox)e.Row.FindControl("txtBookId");
TextBox txtAuthor = (TextBox)e.Row.FindControl("txtAuthor");
TextBox txtISBN = (TextBox)e.Row.FindControl("txtISBN");
TextBox txtYear = (TextBox)e.Row.FindControl("txtYear");
TextBox txtTitle = (TextBox)e.Row.FindControl("txtTitle");
TextBox txtEdition = (TextBox)e.Row.FindControl("txtEdition");
ImageButton imgSave = (ImageButton)e.Row.FindControl("imgSave");
ImageButton imgCancle = (ImageButton)e.Row.FindControl("imgCancle");
if (txtBookId != null)
txtBookId.Text = ((LINQ.Book)e.Row.DataItem).BookId.ToString();
if (txtAuthor != null)
txtAuthor.Text = ((LINQ.Book)e.Row.DataItem).Author.ToString();
if (txtISBN != null)
txtISBN.Text = ((LINQ.Book)e.Row.DataItem).ISBN.ToString();
if (txtYear != null)
txtYear.Text = ((LINQ.Book)e.Row.DataItem).Year.ToString();
if (txtTitle != null)
txtTitle.Text = ((LINQ.Book)e.Row.DataItem).Title.ToString();
if (txtEdition != null)
txtEdition.Text = ((LINQ.Book)e.Row.DataItem).Edition.ToString();
if (imgSave != null)
imgSave.CommandName = "Update";
if (imgCancle != null)
imgCancle.CommandName = "Cancel";
}
}
protected void grdvBook_RowEditing(object sender, GridViewEditEventArgs e)
{
grdvBook.EditIndex = e.NewEditIndex;
grdvBook.Columns[5].HeaderText = "Update";
grdvBook.Columns[6].Visible = false;
imgAddBook.Visible = false;
ShowTempBooks();
}
protected void grdvBook_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
grdvBook.EditIndex = -1;
grdvBook.Columns[5].HeaderText = "Edit";
grdvBook.Columns[6].Visible = true;
if (Convert.ToInt32(((TextBox)grdvBook.Rows[e.RowIndex].FindControl("txtBookId")).Text) == 0)
{
List<LINQ.Book> objBooks = (List<LINQ.Book>)Session["Books"];
objBooks.RemoveAt(objBooks.Count - 1);
Session["Books"] = objBooks;
}
imgAddBook.Visible = true;
ShowTempBooks();
}
protected void grdvBook_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
bool boolError = false;
int bookId = Convert.ToInt32(((TextBox)grdvBook.Rows[e.RowIndex].FindControl("txtBookId")).Text);
string author = ((TextBox)grdvBook.Rows[e.RowIndex].FindControl("txtAuthor")).Text;
string ISBN = ((TextBox)grdvBook.Rows[e.RowIndex].FindControl("txtISBN")).Text;
string year = ((TextBox)grdvBook.Rows[e.RowIndex].FindControl("txtYear")).Text;
string title = ((TextBox)grdvBook.Rows[e.RowIndex].FindControl("txtTitle")).Text;
string edition = ((TextBox)grdvBook.Rows[e.RowIndex].FindControl("txtEdition")).Text;
if (author == string.Empty)
{
Utilities.CreateMessageLabel(this, BLL.Constants.AuthorNameReq, false);
boolError = true;
}
if (ISBN == string.Empty)
{
Utilities.CreateMessageLabel(this, BLL.Constants.ISBNReq, false);
boolError = true;
}
if (ISBN != string.Empty)
{
try
{
Convert.ToInt32(ISBN);
}
catch (FormatException ex)
{
Utilities.CreateMessageLabel(this, BLL.Constants.ISBNNumeric, false);
boolError = true;
}
}
if (year == string.Empty)
{
Utilities.CreateMessageLabel(this, BLL.Constants.YearReq, false);
boolError = true;
}
if (year != string.Empty)
{
try
{
Convert.ToInt32(year);
}
catch (FormatException ex)
{
Utilities.CreateMessageLabel(this, BLL.Constants.YearNemeric, false);
boolError = true;
}
if (year.Length > 4)
{
Utilities.CreateMessageLabel(this, BLL.Constants.YearLength, false);
boolError = true;
}
}
if (title == string.Empty)
{
Utilities.CreateMessageLabel(this, BLL.Constants.BookTitleReq, false);
boolError = true;
}
if (edition == string.Empty)
{
Utilities.CreateMessageLabel(this, BLL.Constants.EditionReq, false);
boolError = true;
}
if (!boolError)
{
List<LINQ.Book> objBooks = (List<LINQ.Book>)Session["Books"];
LINQ.Book objBook = null;
if (bookId > 0)
{
objBook = objBooks.Where(B => B.BookId == bookId).Single();
objBook.CourseId = Convert.ToInt32(Session[Constants.ProgramSessionValues.courseId.ToString()]);
objBook.Author = author;
objBook.ISBN = ISBN;
objBook.Title = title;
objBook.Year = year;
objBook.Edition = edition;
}
else
{
objBook = objBooks.ElementAt(e.RowIndex);
objBook.CourseId = Convert.ToInt32(Session[Constants.ProgramSessionValues.courseId.ToString()]);
objBook.Author = author;
objBook.ISBN = ISBN;
objBook.Title = title;
objBook.Year = year;
objBook.Edition = edition;
}
Session["Books"] = objBooks;
grdvBook.EditIndex = -1;
grdvBook.Columns[5].HeaderText = "Edit";
grdvBook.Columns[6].Visible = true;
imgAddBook.Visible = true;
ShowTempBooks();
}
else
{
grdvBook.EditIndex = e.RowIndex;
}
}
protected void grdvBook_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
List<LINQ.Book> objBooks = (List<LINQ.Book>)Session["Books"];
objBooks.RemoveAt(e.RowIndex);
Session["Books"] = objBooks;
ShowTempBooks();
}
protected void imgAddBook_Click(object sender, ImageClickEventArgs e)
{
GenerateBookRow();
}
public void GenerateBookRow()
{
List<LINQ.Book> objBooks = (List<LINQ.Book>)Session["Books"];
LINQ.Book objBook = new LINQ.Book();
objBook.CourseId = Convert.ToInt32(Session[Constants.ProgramSessionValues.courseId.ToString()]);
objBook.Author = string.Empty;
objBook.ISBN = string.Empty;
objBook.Year = string.Empty;
objBook.Title = string.Empty;
objBook.Edition = string.Empty;
objBooks.Add(objBook);
Session["Books"] = objBooks;
grdvBook.EditIndex = objBooks.Count - 1;
grdvBook.Columns[5].HeaderText = "Update";
grdvBook.Columns[6].Visible = false;
imgAddBook.Visible = false;
ShowTempBooks();
}
No comments:
Post a Comment