protected void Page_Load(object sender, EventArgs e) { // if user typed link directly in addresss bar then redirect it to home page (user can only come to this page by clicking on a link - "View Details") if (Request.UrlReferrer != null) { lblMessage.Text = "Came from" + Request.UrlReferrer.ToString() + "page."; } else { Response.Redirect("~/Default.aspx"); } // check QueryString not null OR empty - if null OR empty redirect to Vehicles.aspx if (Request.QueryString["personID"] == null || Request.QueryString["personID"].ToString() == "") { Response.Redirect("Default.aspx"); } personID = Convert.ToInt32(Request.QueryString["personID"]); if (!IsPostBack) { if (User.Identity.IsAuthenticated) { AddressBookRepository context2 = new AddressBookRepository(); try { IQueryable<Person> query2 = context2.GetPersonByID(personID); personDetailsView.DataSource = query2.ToList(); personDetailsView.DataBind(); } catch (Exception) { lblMessage.Text = "An error occurred. Please try again."; } } else { //ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('Access denied - You are not authorized to access this page. Please Login or Register to view this page.');", true); ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", " alert('Access denied - You are not authorized to access this page. Please Login or Register to view this page.'); window.location='../Account2/Login.aspx';", true); } } }
protected void contactsGridView_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { /* cell refers to the column index of your CommandField and ctrl refers to the control index (Delete button) within the cell you're referencing. stackoverflow.com/questions/14397309/how-to-call-javascript-function-in-gridview-command-field */ // reference Delete button LinkButton btnDelete = (LinkButton)e.Row.Cells[7].Controls[0]; btnDelete.Attributes.Add("onclick", "javascript:return " + "confirm('Are you sure you want to delete this record: " + DataBinder.Eval(e.Row.DataItem, "FirstName") + " " + DataBinder.Eval(e.Row.DataItem, "LastName") + " PersonID: " + contactsGridView.DataKeys[e.Row.RowIndex].Values[0] + "')"); int personID = Convert.ToInt16(contactsGridView.DataKeys[e.Row.RowIndex].Values[0]); try { AddressBookRepository context = new AddressBookRepository(); var query = context.GetPersonByID(personID).FirstOrDefault(); string addedBy = query.AddedBy.Trim(); // reference ViewDetails button HyperLink btnDetails = (HyperLink)e.Row.Cells[8].Controls[0]; // show ViewDetails button only if current user is "Admin" or if contact is added by current user (logged in user) btnDetails.Visible = ((User.IsInRole("canEdit")) || User.Identity.Name.ToUpper() == addedBy.ToUpper()); //User.Identity.GetUserId(); if ((User.IsInRole("canEdit")) || (User.Identity.Name.ToUpper() == addedBy.ToUpper())) { btnDelete.Visible = true; } else { btnDelete.Visible = false; } } catch (Exception) { lblError.Text = "An error occurred. Please try again."; } } }