?? webform1.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 System.Data.OleDb;
using System.Drawing.Imaging;
namespace UserPoll
{
/// <summary>
/// WebForm1 的摘要說明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.RadioButtonList rlCandidates;
protected System.Web.UI.WebControls.Button btnVote;
protected System.Web.UI.WebControls.Image Image1;
private OleDbConnection conn;
private DataSet ds = new DataSet ();
private string connstr = "";
private void Page_Load(object sender, System.EventArgs e)
{
// 在此處放置用戶代碼以初始化頁面
//初始化數據庫連接string
connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
+ Server.MapPath(@".\db\polldb.mdb")
+ ";Mode=Share Deny None;Persist Security Info=False";
if (!IsPostBack)
{
BindData();
}
}
private void BindData()
{
//new a dbconnection,數據庫為db目錄下的polldb.mdb
conn = new OleDbConnection(connstr);
//創建sql語句
string sql = "SELECT * FROM [datatbl]";
//創建dataadapter
OleDbDataAdapter da = new OleDbDataAdapter(sql,conn);
//填充dataset
da.Fill(ds,"Data");
//給RadioButtonList賦數據
InitRadioButtonList();
//繪制餅狀圖
DrawResult();
}
private void InitRadioButtonList()
{
//將DataSet數據綁定到RadioButtonList
rlCandidates.DataSource = ds.Tables["Data"] ;
rlCandidates.DataTextField = ds.Tables["Data"].Columns["name"].ToString ();
rlCandidates.DataValueField = ds.Tables["Data"].Columns["count"].ToString ();
rlCandidates.DataBind ();
}
private void DrawResult()
{
//創建一個Bitmap
Bitmap bm = new Bitmap(200,200);
//得到Graphics
Graphics g = Graphics.FromImage(bm);
g.Clear (Color.White);
//統計總投票數
int totalCount=0;
foreach (DataRow aRow in ds.Tables["Data"].Rows )
{
totalCount += int.Parse (aRow["count"].ToString ());
}
int startangle = 0; //扇形起始角度
int sweepangle; //扇形跨越角度
foreach (DataRow aRow in ds.Tables["Data"].Rows)
{
string sname = aRow["name"].ToString();
string scount = aRow["count"].ToString() ;
string sindex = aRow["index"].ToString() ;
//選取不同顏色
Color c = getColor(int.Parse(sindex)) ;
sweepangle = (int)Math.Round (double.Parse(scount)/totalCount*360);
Brush myBrush = new SolidBrush(c);
//調用FillPie繪制扇形
g.FillPie(myBrush,0,0,200,200,startangle,sweepangle);
startangle += sweepangle;
//輸出文字解釋
Response.Write (sname + ":" + scount + " 顏色:" + c.ToString() + "<br> ");
}
//保存為臨時文件
bm.Save(Server.MapPath (@".\result.jpeg"), ImageFormat.Jpeg );
//將Image控件的圖像鏈接到此文件
Image1.ImageUrl = Server.MapPath (@".\result.jpeg");
}
//根據序號,返回不同顏色
private Color getColor(int index)
{
Color c;
int i = index % 5;
switch(i)
{
case 0:
c = Color.Red;
break;
case 1:
c = Color.Blue ;
break;
case 2:
c = Color.Yellow ;
break;
case 3:
c = Color.Green;
break;
case 4:
c = Color.Gray;
break;
default:
c = Color.Black ;
break;
}
return (c);
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:該調用是 ASP.NET Web 窗體設計器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 設計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內容。
/// </summary>
private void InitializeComponent()
{
this.btnVote.Click += new System.EventHandler(this.btnVote_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void btnVote_Click(object sender, System.EventArgs e)
{
if (rlCandidates.SelectedItem != null)
{
//new a dbconnection,數據庫為db目錄下的polldb.mdb
conn = new OleDbConnection(connstr);
conn.Open ();
//Update記錄,count=count+1
string sql = "UPDATE [datatbl] SET [count]=" + Convert.ToString ((int.Parse (rlCandidates.SelectedItem.Value) + 1))
+ " WHERE [name]='" + rlCandidates.SelectedItem.Text + "'" ;
OleDbCommand cmd = new OleDbCommand(sql,conn);
cmd.ExecuteNonQuery ();
conn.Close ();
//更新數據顯示
BindData();
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -