CNSA-216-Personal/CH78/Home.aspx.cs
2024-03-20 15:33:54 -04:00

226 lines
7.0 KiB
C#

using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace CH78
{
public partial class Home : Page
{
protected void Page_Load(object sender, EventArgs e)
{
Page.ClientScript.RegisterClientScriptInclude("Test", "MyScript.js");
// Page.
gvStudent.RowDataBound += new GridViewRowEventHandler(grdStudent_RowDataBound); //row databound event
gvStudent.PageIndexChanging += new GridViewPageEventHandler(grdStudent_PageIndexChanging);
gvStudent.Sorting += new GridViewSortEventHandler(grdStudent_Sorting);
if (!IsPostBack)
{
}
}
private void grdStudent_Sorting(object sender, GridViewSortEventArgs e)
{
SortRecords(e.SortExpression);
}
public string sortDir
{
get => (string)ViewState["sortDir"];
set => ViewState["sortDir"] = value;
}
private void SortRecords(string sortExpress)
{
string oldExpression = gvStudent.SortExpression;
String newExpression = sortExpress;
String lastValue, theSortField;
String sortExpression;
DataView source;
String theDirection;
string oldSortExp, newSortExp;
string wildChar;
theDirection = " ";
wildChar = " %";
lastValue = (string) ViewState["sortValue"];
sortExpression = sortExpress;
oldSortExp = (string)ViewState["oldSortExpression"];
//get the last sort field and the new sort field
// if (oldSortExp != sortExpression) //check for new expression, that is DB field
// {
if (this.sortDir == "desc")
{
this.sortDir = "asc";
}
else if (this.sortDir == null)
{
this.sortDir = "asc";
}
else if (this.sortDir == "asc")
{
this.sortDir = "desc";
}
else //sort in ascending order the first time, column may not change if already sorted that way
{
this.sortDir = "asc";
}
theSortField = (string)ViewState["sortField"];
source = (DataView)Cache["StudentData"]; // use the cache
source.Sort = (" " + sortExpression + " " + this.sortDir);
ViewState["oldSortExpression"] = sortExpress; // save the sort as old sort
gvStudent.DataSource = source;
gvStudent.DataBind();
// }
}
private void grdStudent_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
Int32 pageNum = 0;
pageNum = e.NewPageIndex;
if (txtStudentID.Text.Trim().Length > 0 || txtFName.Text.Trim().Length > 0 || txtLName.Text.Trim().Length > 0)
{
textHasValues = true;
}
Paging(pageNum);
}
private void Paging(Int32 page)
{
gvStudent.PageIndex = page;
BindData();
}
private void BindData()
{
DataSet ds = new DataSet();
string stuID = Convert.ToString(Session["vStuID"]);
string LNAME = Convert.ToString(Session["vLNAME"]);
string FNAME = Convert.ToString(Session["vFNAME"]);
txtStudentID.Text = stuID;
txtFName.Text = FNAME;
txtLName.Text = LNAME;
if (textHasValues)
{
ds = StudentDataTier.SearchStudents(FNAME, LNAME, stuID);
}
// ds = dt.GetStudents();
gvStudent.DataSource = ds.Tables[0];
if (Cache["StudentData"] == null)
{
Cache.Add("StudentData", new DataView(ds.Tables[0]),
null,System.Web.Caching.Cache.NoAbsoluteExpiration,
System.TimeSpan.FromMinutes(10), System.Web.Caching.CacheItemPriority.Default,
null);
}
gvStudent.DataBind();
}
protected void grdStudent_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header) // header add click event to select checkboxes
{
((CheckBox)e.Row.FindControl("cbSelectAll")).Attributes.Add("onclick", "javascript:SelectAll('" + ((CheckBox)e.Row.FindControl("cbSelectAll")).ClientID + "')");
}
}
private bool textHasValues = false;
protected void btnSearch_Click(object sender, EventArgs e)
{
if (txtStudentID.Text.Trim().Length > 0 || txtFName.Text.Trim().Length > 0 || txtLName.Text.Trim().Length > 0)
{
try
{
Session["vStuID"] = txtStudentID.Text.Trim();
Session["vFNAME"] = txtFName.Text.Trim();
Session["vLNAME"] = txtLName.Text.Trim();
Cache.Remove("StudentData");
textHasValues = true;
BindData();
}
catch (Exception exception)
{
}
}
}
protected void grdStudent_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void Delete_Click(object sender, CommandEventArgs e)
{
}
protected void lbtnEdit_Click(object sender, CommandEventArgs e)
{
string recordToBeEdited;
Int64 mEditedRecord = 0;
System.Text.StringBuilder sb = new System.Text.StringBuilder();
try
{
Session["vStudentID"] = txtStudentID.Text.Trim();
Session["vFName"] = txtFName.Text.Trim();
Session["vLName"] = txtLName.Text.Trim();
// Get the record
recordToBeEdited = (e.CommandArgument.ToString().Trim());
// this script will open a popup
sb.Append("<script language='javascript'>");
sb.Append("window.open('Display.aspx?ID=" + recordToBeEdited.ToString() + "&typeEdit=" + "'DisplayEdit';");
sb.Append(", 'width=525, height=525, menubar=no, resizable=yes, left=50, top=50, scrollbars=yes');");
sb.Append("</script>");
// register with ClientScript
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "PopupScript", sb.ToString());
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex.InnerException);
}
}
}
}