📄 questions.aspx.cs
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Wrox.WebModules.Accounts.Business;
namespace Wrox.WebModules.Polls.Web
{
/// <summary>
/// Summary description for Questions.
/// </summary>
public class Questions : Wrox.ThePhile.Web.PhilePage
{
protected System.Web.UI.WebControls.LinkButton AddNew;
protected System.Web.UI.WebControls.LinkButton CancelAddNew;
protected System.Web.UI.WebControls.DataGrid QuestionsGrid;
protected System.Web.UI.WebControls.TextBox NewQuestionText;
protected System.Web.UI.WebControls.CheckBox NewIsCurrent;
protected System.Web.UI.WebControls.Button Create;
protected System.Web.UI.WebControls.TableRow AddNewControlsRow;
protected System.Web.UI.WebControls.TableRow CreateNewRow;
protected System.Web.UI.WebControls.Label AddNewError;
protected Wrox.WebModules.Polls.Web.Controls.User.Poll CurrentPoll;
protected System.Web.UI.WebControls.Table Table1;
protected void Page_Load(object sender, EventArgs e)
{
// check if the current user is allowed to administer the polls
if (!Context.User.Identity.IsAuthenticated ||
!((PhilePrincipal)Context.User).HasPermission((int)PollsPermissions.AdministerPolls))
{
// if not, redirect to the Login page
Response.Redirect("/ThePhile/Modules/Users/Login.aspx?ShowError=true", true);
}
if (!Page.IsPostBack)
{
// bind the page's controls
BindGrid();
}
}
protected void BindGrid()
{
// get all the Questions
DataView myDV = Business.Question.GetQuestions().Tables[0].DefaultView;
// sort the data according to the SortExpression value
if (QuestionsGrid.Attributes["SortExpression"] != null)
myDV.Sort = QuestionsGrid.Attributes["SortExpression"];
QuestionsGrid.DataSource = myDV;
QuestionsGrid.DataBind();
}
protected void QuestionsGrid_Sort(Object sender, DataGridSortCommandEventArgs e)
{
AddNewError.Visible = false;
ShowAddNewControls(false);
QuestionsGrid.EditItemIndex = -1;
// set the SortExpression attribute that will be used to actually sort
// the data in the BindGrid method
QuestionsGrid.Attributes["SortExpression"] = e.SortExpression.ToString();
BindGrid();
}
protected void QuestionsGrid_Edit(object sender, DataGridCommandEventArgs e)
{
AddNewError.Visible = false;
ShowAddNewControls(false);
// start editing
QuestionsGrid.EditItemIndex = (int)e.Item.ItemIndex;
BindGrid();
}
protected void QuestionsGrid_CancelEdit(object sender, DataGridCommandEventArgs e)
{
QuestionsGrid.EditItemIndex = -1;
BindGrid();
}
protected void QuestionsGrid_Update(object sender, DataGridCommandEventArgs e)
{
if (Page.IsValid)
{
// get the new values from the textboxes
string questionText = ((TextBox)e.Item.FindControl("EditQuestionText")).Text;
bool isCurrent = ((CheckBox)e.Item.FindControl("EditIsCurrent")).Checked;
bool archived = ((CheckBox)e.Item.FindControl("EditArchived")).Checked;
int questionID = (int)QuestionsGrid.DataKeys[e.Item.ItemIndex];
// update the values
Business.Question question = new Business.Question(questionID);
question.Text = questionText;
question.IsCurrent = isCurrent;
question.Archived = archived;
question.Update();
QuestionsGrid.EditItemIndex = -1;
BindGrid();
// rebind the CurrentPoll control, because the current poll might has changed
CurrentPoll.DataBind();
}
}
protected void QuestionsGrid_Delete(object sender, DataGridCommandEventArgs e)
{
AddNewError.Visible = false;
ShowAddNewControls(false);
QuestionsGrid.EditItemIndex = -1;
// get the ID of this record and delete it
Business.Question question = new Business.Question((int)QuestionsGrid.DataKeys[e.Item.ItemIndex]);
question.Delete();
BindGrid();
// rebind the CurrentPoll control, because the deleted poll could be the current one
CurrentPoll.DataBind();
}
protected void AddNew_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
// add the new record
if (new Business.Question().Create(NewQuestionText.Text, NewIsCurrent.Checked, false) < 0)
{
// if Add returned -1, the question was already present
AddNewError.Visible = true;
}
ShowAddNewControls(false);
BindGrid();
// rebind the CurrentPoll control, because the current poll might has changed
CurrentPoll.DataBind();
}
}
protected void CancelAddNew_Click(object sender, EventArgs e)
{
ShowAddNewControls(false);
}
protected void Create_Click(object sender, EventArgs e)
{
// show the textboxes and buttons for adding a new record
AddNewError.Visible = false;
ShowAddNewControls(true);
QuestionsGrid.EditItemIndex = -1;
BindGrid();
}
protected void ShowAddNewControls(bool ShowControls)
{
// show/hide the controls for adding a new record
NewQuestionText.Text="";
NewIsCurrent.Checked=false;
AddNewControlsRow.Visible = ShowControls;
CreateNewRow.Visible = !ShowControls;
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
base.OnInit(e);
InitializeComponent();
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -