?? showvote.aspx.cs
字號:
using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace book08
{
//該源碼下載自www.51aspx.com(51aspx.com)
/// <summary>
/// ShowVote 的摘要說明。
/// </summary>
public partial class ShowVote : System.Web.UI.Page
{
#region Properties
/// <summary>
/// 投票id
/// </summary>
public int VoteID
{
get
{
if (Request.QueryString["id"] == null)
{
return GetActiveVoteID();
}
else
{
return Convert.ToInt32(Request.QueryString["id"]);
}
}
}
/// <summary>
/// 是否多選
/// </summary>
public bool IsMulti
{
get
{
string strConn = ConfigurationSettings.AppSettings["ConnectionString"];
SqlConnection conn = new SqlConnection(strConn);
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "SELECT type FROM Vote WHERE id = " + VoteID.ToString();
int iRet = 0;
try
{
conn.Open();
iRet = Convert.ToInt32(cmd.ExecuteScalar());
}
catch (SqlException ex)
{
Response.Write(ex.Message);
}
finally
{
conn.Close();
}
return (iRet == 1);
}
}
#endregion
/// <summary>
/// 獲取當(dāng)前投票id
/// </summary>
/// <returns>id</returns>
private int GetActiveVoteID()
{
string strConn = ConfigurationSettings.AppSettings["ConnectionString"];
SqlConnection conn = new SqlConnection(strConn);
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "GetActiveVoteID";
int iRet = -1;
try
{
conn.Open();
iRet = (int)cmd.ExecuteScalar();
}
catch (SqlException ex)
{
Response.Write(ex.Message);
}
finally
{
conn.Close();
}
return iRet;
}
protected void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
BindOtherVotes();
if (VoteID < 0)
{
panelMain.Visible = false;
panelNoVote.Visible = true;
panelInfo.Visible = false;
return;
}
else
{
panelMain.Visible = true;
panelNoVote.Visible = false;
panelInfo.Visible = true;
}
if (IsMulti)
{
panelMulti.Visible = true;
panelSingle.Visible = false;
BindMulti();
}
else
{
panelMulti.Visible = false;
panelSingle.Visible = true;
BindSingle();
}
GetVoteInfo();
}
}
/// <summary>
/// 數(shù)據(jù)綁定多選列表
/// </summary>
private void BindMulti()
{
string strConn = ConfigurationSettings.AppSettings["ConnectionString"];
SqlConnection conn = new SqlConnection(strConn);
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "GetOptions";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@vote_id", VoteID);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
try
{
conn.Open();
da.Fill(ds);
cblOptions.DataSource = ds.Tables[0].DefaultView;
cblOptions.DataTextField = "content";
cblOptions.DataValueField = "id";
cblOptions.DataBind();
}
catch (SqlException ex)
{
Response.Write(ex.Message);
}
finally
{
conn.Close();
}
}
/// <summary>
/// 數(shù)據(jù)綁定單選列表
/// </summary>
private void BindSingle()
{
string strConn = ConfigurationSettings.AppSettings["ConnectionString"];
SqlConnection conn = new SqlConnection(strConn);
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "GetOptions";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@vote_id", VoteID);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
try
{
conn.Open();
da.Fill(ds);
rblOptions.DataSource = ds.Tables[0].DefaultView;
rblOptions.DataTextField = "content";
rblOptions.DataValueField = "id";
rblOptions.DataBind();
}
catch (SqlException ex)
{
Response.Write(ex.Message);
}
finally
{
conn.Close();
}
}
/// <summary>
/// 加載投票信息
/// </summary>
private void GetVoteInfo()
{
string strConn = ConfigurationSettings.AppSettings["ConnectionString"];
SqlConnection conn = new SqlConnection(strConn);
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "GetVote";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@id", VoteID);
try
{
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
lblQuestion.Text = dr.GetString(0);
lblStartdate.Text = dr.GetDateTime(1).ToShortDateString();
lblEnddate.Text = dr.GetDateTime(2).ToShortDateString();
lblTotalCount.Text = dr.GetInt32(5).ToString();
string strScript = string.Format(
"window.open('ShowResult.aspx?id={0}',null,'menubar=no,toolbar=no,width=500,height=300,fullscreen=2');", VoteID);
lbView.Attributes.Add("onclick", strScript);
}
}
catch (SqlException ex)
{
Response.Write(ex.Message);
}
finally
{
conn.Close();
}
}
/// <summary>
/// 數(shù)據(jù)綁定其他投票
/// </summary>
private void BindOtherVotes()
{
string strConn = ConfigurationSettings.AppSettings["ConnectionString"];
SqlConnection conn = new SqlConnection(strConn);
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "GetOtherVotes";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@id", VoteID);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
try
{
conn.Open();
da.Fill(ds);
dgOtherVotes.DataSource = ds.Tables[0].DefaultView;
dgOtherVotes.DataBind();
}
catch (SqlException ex)
{
Response.Write(ex.Message);
}
finally
{
conn.Close();
}
}
#region Web 窗體設(shè)計器生成的代碼
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 該調(diào)用是 ASP.NET Web 窗體設(shè)計器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 設(shè)計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內(nèi)容。
/// </summary>
private void InitializeComponent()
{
}
#endregion
protected void lbVote_Click(object sender, System.EventArgs e)
{
if (!CheckInput())
Response.Write("<script>alert('請至少選擇一個選項!');</script>");
else
{
if (Request.Cookies[VoteID.ToString()] != null)
{
Response.Write("<script>alert('您已經(jīng)投過票了,謝謝!');</script>");
}
else
{
//存cookie
HttpCookie MyCookie = new HttpCookie(VoteID.ToString());
MyCookie.Value = "1";
Response.Cookies.Add(MyCookie);
//投票
DoVote();
Response.Write("<script>alert('投票成功!');</script>");
}
GetVoteInfo();
}
}
/// <summary>
/// 投票
/// </summary>
private void DoVote()
{
if (IsMulti) //多選
{
foreach (ListItem item in cblOptions.Items)
{
if (item.Selected)
{
string strConn = ConfigurationSettings.AppSettings["ConnectionString"];
SqlConnection conn = new SqlConnection(strConn);
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "VoteOption";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@id", Convert.ToInt32(item.Value));
try
{
conn.Open();
cmd.ExecuteNonQuery();
}
catch (SqlException ex)
{
Response.Write(ex.Message);
}
finally
{
conn.Close();
}
}
}
}
else
{
string strConn = ConfigurationSettings.AppSettings["ConnectionString"];
SqlConnection conn = new SqlConnection(strConn);
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "VoteOption";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@id", Convert.ToInt32(rblOptions.SelectedValue));
try
{
conn.Open();
cmd.ExecuteNonQuery();
}
catch (SqlException ex)
{
Response.Write(ex.Message);
}
finally
{
conn.Close();
}
}
}
/// <summary>
/// 檢查選擇是否正確
/// </summary>
/// <returns></returns>
private bool CheckInput()
{
if (IsMulti)
{
return (cblOptions.SelectedIndex != -1);
}
else
{
return (rblOptions.SelectedIndex != -1);
}
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -