CNSA-216-Personal/CH78/Home.aspx.cs

295 lines
9.7 KiB
C#
Raw Permalink Normal View History

2024-03-20 11:09:49 -07:00
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)
{
2024-03-26 17:22:41 -07:00
Cache.Remove("StudentData");
BindData();
}
else
{
if (Convert.ToString(Session["GRIDREFRESH"]) != "")
{
BindData();
}
2024-03-20 11:09:49 -07:00
}
}
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;
2024-03-25 09:09:20 -07:00
// if (textHasValues)
// {
2024-03-20 11:09:49 -07:00
ds = StudentDataTier.SearchStudents(FNAME, LNAME, stuID);
2024-03-25 09:09:20 -07:00
// }
2024-03-20 11:09:49 -07:00
// 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)
{
2024-03-25 09:09:20 -07:00
try
{
CheckBox chk = new CheckBox();
Label lbl = new Label();
string stuid = "";
StudentDataTier std = new StudentDataTier();
if (gvStudent.Rows.Count > 0) // only do it if there is a row
{
//For Each item As GridView In grdCustomer.items
foreach (GridViewRow row in gvStudent.Rows)
{
//get the selected checkbox
chk = (CheckBox)row.FindControl("chkStudentID");
if (chk.Checked)
{
lbl = (Label)row.Controls[0].FindControl("hidStudentID");
stuid = lbl.Text.Trim();
//delete the record one at a time
StudentDataTier.DeleteStudents(stuid);
}
}
//refresh datagrid
BindData();
}
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex.InnerException);
}
2024-03-20 11:09:49 -07:00
}
protected void lbtnEdit_Click(object sender, CommandEventArgs e)
{
2024-03-20 12:33:54 -07:00
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());
2024-03-26 17:22:41 -07:00
recordToBeEdited = StudentDataTier.EncryptQueryString(recordToBeEdited, "CNSAcnsa1", "salty");
2024-03-20 12:33:54 -07:00
// this script will open a popup
sb.Append("<script language='javascript'>");
2024-03-21 11:09:34 -07:00
sb.Append("window.open('Display.aspx?ID=" + recordToBeEdited.ToString() + "&type=Edit" +"' , 'DisplayEdit',");
2024-03-26 17:22:41 -07:00
sb.Append("'width=1025, height=525, menubar=no, resizable=yes, left=50, top=50, scrollbars=yes');");
2024-03-20 12:33:54 -07:00
sb.Append("</script>");
// register with ClientScript
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "PopupScript", sb.ToString());
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex.InnerException);
}
2024-03-20 11:09:49 -07:00
}
2024-03-25 09:09:20 -07:00
protected void btnNew_OnClick(object sender, EventArgs e)
{
2024-03-28 10:14:16 -07:00
string recordToBeEdited = "NEW";
2024-03-25 09:09:20 -07:00
Int64 mEditedRecord = 0;
System.Text.StringBuilder sb = new System.Text.StringBuilder();
2024-03-28 10:14:16 -07:00
recordToBeEdited = StudentDataTier.EncryptQueryString(recordToBeEdited, "CNSAcnsa1", "salty");
2024-03-25 09:09:20 -07:00
try
{
Session["vStudentID"] = txtStudentID.Text.Trim();
Session["vFName"] = txtFName.Text.Trim();
Session["vLName"] = txtLName.Text.Trim();
// this script will open a popup
sb.Append("<script language='javascript'>");
2024-03-28 10:14:16 -07:00
sb.Append("window.open('Display.aspx?ID="+ recordToBeEdited +"&type=NEW' , 'DisplayEdit',");
2024-03-26 17:22:41 -07:00
sb.Append("'width=1025, height=525, menubar=no, resizable=yes, left=50, top=50, scrollbars=yes');");
2024-03-25 09:09:20 -07:00
sb.Append("</script>");
// register with ClientScript
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "PopupScript", sb.ToString());
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex.InnerException);
}
}
2024-03-20 11:09:49 -07:00
}
}