📄 options.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 Options.
/// </summary>
public class Options : Wrox.ThePhile.Web.PhilePage
{
protected System.Web.UI.WebControls.DropDownList QuestionsDropDown;
protected System.Web.UI.WebControls.DataGrid OptionsGrid;
protected System.Web.UI.WebControls.LinkButton AddNew;
protected System.Web.UI.WebControls.LinkButton CancelAddNew;
protected System.Web.UI.WebControls.HyperLink AddCancel;
protected System.Web.UI.WebControls.TextBox NewOptionText;
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.TableCell AddNewResultCell;
protected System.Web.UI.WebControls.Table Table1;
protected System.Web.UI.WebControls.Table TableOptions;
protected System.Web.UI.WebControls.Label AddNewError;
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);
}
// get the QuestionID from the QueryString
string questionID = Request.Params["QuestionID"];
OptionsGrid.Attributes["QuestionID"] = questionID;
if (!Page.IsPostBack)
{
// load all the avaible questions in the DropDown control
DataView myDV = Business.Question.GetQuestions().Tables[0].DefaultView;
QuestionsDropDown.DataSource = myDV;
QuestionsDropDown.DataBind();
if (questionID!=null)
{
// select the DropDown element according to the questionID
QuestionsDropDown.SelectedIndex = QuestionsDropDown.Items.IndexOf(
QuestionsDropDown.Items.FindByValue(questionID));
}
// (re)bind the page's controls
BindGrid();
}
}
protected void BindGrid()
{
int questionID;
// get the QuestionID value from the Grid's attributes
if ( OptionsGrid.Attributes["QuestionID"] != null )
questionID = int.Parse(OptionsGrid.Attributes["QuestionID"]);
else
questionID = int.Parse(QuestionsDropDown.SelectedItem.Value);
// get all the options of this question
DataView myDV = new Business.Question(questionID).GetOptions().Tables[0].DefaultView;
// sort the records according to the SortExpression value in the Grid's attributes
if ( OptionsGrid.Attributes["SortExpression"]!="" )
myDV.Sort = OptionsGrid.Attributes["SortExpression"];
OptionsGrid.DataSource = myDV;
OptionsGrid.DataBind();
}
protected void QuestionsDropDown_IndexChanged(object sender, EventArgs e)
{
// reload the page to show the options for the selected question
// This will also hide the controls to add a option if they were visible
Response.Redirect("Options.aspx?QuestionID=" + QuestionsDropDown.SelectedItem.Value);
}
protected void OptionsGrid_Sort(Object sender, DataGridSortCommandEventArgs e)
{
AddNewError.Visible = false;
ShowAddNewControls(false);
OptionsGrid.EditItemIndex = -1;
// set the SortExpression attribute that will be used to actually sort
// the data in the BindGrid method
OptionsGrid.Attributes["SortExpression"] = e.SortExpression.ToString();
BindGrid();
}
protected void OptionsGrid_Edit(object sender, DataGridCommandEventArgs e)
{
AddNewError.Visible = false;
ShowAddNewControls(false);
// start the editing for this item
OptionsGrid.EditItemIndex = (int)e.Item.ItemIndex;
BindGrid();
}
protected void OptionsGrid_CancelEdit(object sender, DataGridCommandEventArgs e)
{
OptionsGrid.EditItemIndex = -1;
BindGrid();
}
protected void OptionsGrid_Update(object sender, DataGridCommandEventArgs e)
{
if (Page.IsValid)
{
int optionID = (int)OptionsGrid.DataKeys[e.Item.ItemIndex];
string optionText = ((TextBox)e.Item.FindControl("EditOptionText")).Text;
// update the option
Business.Option option = new Business.Option(optionID);
option.Text = optionText;
option.Update();
OptionsGrid.EditItemIndex = -1;
BindGrid();
}
}
protected void OptionsGrid_Delete(object sender, DataGridCommandEventArgs e)
{
AddNewError.Visible = false;
ShowAddNewControls(false);
OptionsGrid.EditItemIndex = -1;
// delete this option
Business.Option option = new Business.Option((int)OptionsGrid.DataKeys[e.Item.ItemIndex]);
option.Delete();
BindGrid();
}
protected void AddNew_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
int questionID = int.Parse(QuestionsDropDown.SelectedItem.Value);
string optionText = NewOptionText.Text;
// add the new option
if (new Business.Question(questionID).AddOption(optionText).ID < 0)
{
// if the call to the Add method returned -1, it means that this option
// was already present, so show the label that tells this
AddNewError.Visible = true;
}
// hide the controls for adding an option
ShowAddNewControls(false);
BindGrid();
}
}
protected void CancelAddNew_Click(object sender, EventArgs e)
{
ShowAddNewControls(false);
}
protected void Create_Click(object sender, EventArgs e)
{
// show the controls for adding a new option
AddNewError.Visible = false;
ShowAddNewControls(true);
OptionsGrid.EditItemIndex = -1;
BindGrid();
}
protected void ShowAddNewControls(bool ShowControls)
{
// show or hide the controls for adding a new option
NewOptionText.Text = "";
CreateNewRow.Visible = !ShowControls;
AddNewResultCell.Visible = !ShowControls;
AddNewControlsRow.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 + -