做一个调查让用户回答问题,然后把问题的答案(事先设定好的几个答案让用户选择)从数据库中查出来,并更具每个答案所占总回答数的比例来画图(要求画直方图和饼状图),以前没有用asp.net画过图,请问直接用asp.net可以实现嘛还是要调用其他的控件,最好能给个思路和例子,先谢谢各位了。
解决方案 »
- dropdownlist下拉事件不触发
- 大家平常用HTTP Analyzer吗?都一般怎么利用好这个工具.
- 关于梅花雪树形控件2.0的问题
- 如何将变量转换成对象本身!
- 50分数据问题 文章添加页面自己写的文字可以insert进数据库,从word拷过来的不行,详细见里面
- 关于ASP.NET比ASP具体多哪些功能!
- 在线等!求助:C# asp 中怎样访问多重frame中的一个frame,然后设置其src。麻烦大家了,仅有的几十分全部给了
- 求四数加减乘除得24的算法,C#实现
- 太谢谢拉!
- 怎样弹出各种提示窗口?
- 为什么我的Visual Studio .Net 2003 简体中文企业版装不了?
- 求一句SQL语句??
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Web.Security;
//***********************系统基类**********************
public class PollBasePage:Page {
protected SqlConnection conn;
protected SqlCommand cmd;
public PollBasePage(){
conn=new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
}protected void InitialCommand(string cmdstr){
cmd=new SqlCommand(cmdstr,conn);
cmd.CommandType=CommandType.StoredProcedure;}protected bool HaveSameValue(string[] str){
for(int i=0;i<str.Length-1;i++){
for(int j=i+1;j<str.Length;j++){
if(str[j]==str[i])
return true;}
}
return false;}protected bool IsNotNumber(string[] str){
for(int i=0;i<str.Length;i++){
for(int j=0;j<str[i].Length;j++){
if(!Char.IsDigit(str[i][j]))
return true;}
}
return false;}protected string EncodeIt(string enstr){
return HttpContext.Current.Server.HtmlEncode(enstr);}protected bool HaveSamePoll(string poll,int pollid){
InitialCommand("poll_SelectAllPollButNotThis");
cmd.Parameters.Add("@subject",poll);
cmd.Parameters.Add("@pollid",pollid);
conn.Open();
Object s=cmd.ExecuteScalar();
conn.Close();
if(s!=null)
return true;
return false;}protected ArrayList GetListItems(int startindex,int endindex,int stepspan){
ArrayList arr=new ArrayList();
for(int i=startindex;i<=endindex;i+=stepspan){
arr.Add(i);}
return arr;}protected int GetSelectedIndex(DropDownList ddl,string itemvalue){
for(int i=0;i<ddl.Items.Count;i++){
if(ddl.Items[i].Value==itemvalue)
return i;}
return 0;}protected string[] ConvertSbToArray(StringBuilder sb){
return sb.ToString().Trim(' ',',').Split(',');}protected int GetStringLength(string str){
byte[] bytes=Encoding.Default.GetBytes(str);
return bytes.Length;}protected string GetLeftString(string str,int cnum){
if(str.Length<=cnum)
return str;
if(GetStringLength(str)<cnum*2)
return str;
char[] cstr=str.ToCharArray();
StringBuilder sb=new StringBuilder();
for(int i=0,j=0;i<(cnum*2);){
if(Encoding.Default.GetBytes(cstr,j,1).Length==1)
i++;
else
i+=2;
sb.Append(str[j]);
j++;}
sb.Append("……");
return sb.ToString();}
}
public class PollChart:PollBasePage{
void Page_Load(Object src, EventArgs e){
int pollid=Int32.Parse(Request.QueryString["pollid"]);
InitialCommand("poll_SelectPollByID");
cmd.Parameters.Add("@pollid",pollid);
conn.Open();
SqlDataReader reader=cmd.ExecuteReader();
reader.Read();
string subject=reader.GetString(0).Trim();
string ChartType=reader.GetString(1).Trim().ToLower();
reader.Close();
InitialCommand("poll_CountOptionsByPollid");
cmd.Parameters.Add("@pollid",pollid);
int TotalOptions=(int)cmd.ExecuteScalar();
int[] BallotArray=new int[TotalOptions];
string[] OptionArray=new string[TotalOptions];
string[] ColorArray=new string[TotalOptions];
int i=0;
int TotalBallots=0;
InitialCommand("poll_SelectOptionsByPollid");
cmd.Parameters.Add("@pollid",pollid);
reader=cmd.ExecuteReader();
while(reader.Read()){
BallotArray[i]=reader.GetInt32(3);
OptionArray[i]=reader.GetString(1).Trim();
ColorArray[i]=reader.GetString(2).Trim();
TotalBallots+=reader.GetInt32(3);
i++;}
reader.Close();
conn.Close();
Bitmap bm=new Bitmap(300,100);
Graphics g=Graphics.FromImage(bm);
g.Clear(Color.Yellow);
StringFormat sf=new StringFormat();
sf.Alignment=StringAlignment.Center;
sf.LineAlignment=StringAlignment.Center;
if(TotalBallots<=0){
g.DrawString("请您先投票!",new Font("黑体",12),new SolidBrush(Color.Black),new Rectangle(0,0,300,100),sf);
}
else{
int OptionLength=0;
for(i=0;i<OptionArray.Length;i++){
if(GetStringLength(OptionArray[i])>OptionLength)
OptionLength=GetStringLength(OptionArray[i]);}
int BallotsLength=(TotalBallots.ToString().Length)*8+60;
int LegendLength=0;
if(OptionLength>20)
LegendLength=200+BallotsLength;
else
LegendLength=OptionLength*10+50+BallotsLength;
int BarWidth=Int32.Parse(ConfigurationSettings.AppSettings["BarWidth"]);
int BarSpace=Int32.Parse(ConfigurationSettings.AppSettings["BarSpace"]);
int BarLeftSpace=(TotalBallots.ToString().Length)*6+10;
int PieChartWidth=Int32.Parse(ConfigurationSettings.AppSettings["PieWidth"]);
int PieChartHeight=Int32.Parse(ConfigurationSettings.AppSettings["PieHeight"]);
int BMWidth=(ChartType=="bar")?(TotalOptions*BarSpace+BarLeftSpace+20+LegendLength):(PieChartWidth+80+LegendLength);
int LegendHeight=TotalOptions*22+90;
int BMHeight=(ChartType=="bar")?LegendHeight:Math.Max(PieChartHeight+110,LegendHeight);
if(BMHeight<250)
BMHeight=250;
int BarChartHeight=BMHeight-80;
string PollSubject=((GetStringLength(subject)*12)>BMWidth)?GetLeftString(subject,(int)(BMWidth/25)):subject;
bm=new Bitmap(BMWidth,BMHeight);
g=Graphics.FromImage(bm);
g.Clear(Color.Snow);Rectangle SubjectRec=new Rectangle(0,4,BMWidth,28);
Rectangle BallotsRec=new Rectangle(0,30,BMWidth,20);
g.DrawString(PollSubject,new Font("楷体_gb2312",14),Brushes.Black,SubjectRec,sf);
g.DrawString("(选票总数:"+TotalBallots.ToString()+")",new Font("楷体_gb2312",10),Brushes.Black,BallotsRec,sf);
g.DrawRectangle(Pens.Black,0,0,BMWidth-1,BMHeight-1);PointF LegendPoint=new PointF(BMWidth-LegendLength,90);
PointF DescPoint=new PointF(LegendPoint.X+30,90);
g.DrawString("图例",new Font("宋体",12,FontStyle.Bold),new SolidBrush(Color.Red),new Rectangle((int)(LegendPoint.X),60,LegendLength,25),sf);if(ChartType=="bar"){float LineSpace=BarChartHeight;
float ScalePercent=1.0f;
int MaxBallotsValue=(TotalBallots>90)?(TotalBallots+(10-TotalBallots%10)):(((TotalBallots<=10)||(TotalBallots%(TotalBallots/10+1)==0)||(TotalBallots%10==0))?TotalBallots:(TotalBallots+((TotalBallots/10+1)-(TotalBallots%(TotalBallots/10+1)))));
int BallotsStep=(TotalBallots<=10)?1:((TotalBallots<=90)?((TotalBallots%10==0)?(TotalBallots/10):(TotalBallots/10+1)):(MaxBallotsValue/10));
ScalePercent=(float)TotalBallots/(float)MaxBallotsValue;
for(i=BallotsStep;i<=MaxBallotsValue;i+=BallotsStep){
float LinePosition=(float)(BMHeight-LineSpace);
g.DrawString((MaxBallotsValue+BallotsStep-i).ToString(),new Font("宋体",10),new SolidBrush(Color.Red),2.0f,(float)(LinePosition-6));
g.DrawLine(Pens.LightGray,8.0f,LinePosition,(float)(BMWidth-LegendLength-20.0f),LinePosition);
LineSpace-=(float)((float)BarChartHeight/(float)(MaxBallotsValue/BallotsStep));}g.DrawLine(Pens.LightGray,(float)(BMWidth-LegendLength-20),(float)(BMHeight-BarChartHeight),(float)(BMWidth-LegendLength-20),(float)BMHeight);for(i=0;i<BallotArray.Length;i++){
float BarHeight=(float)(BallotArray[i]*BarChartHeight/TotalBallots*ScalePercent);g.FillRectangle(new SolidBrush(GetColor(ColorArray[i])),BarLeftSpace,(float)(BMHeight-BarHeight),(float)BarWidth,BarHeight);
g.DrawRectangle(Pens.Black,BarLeftSpace,(float)(BMHeight-BarHeight),(float)BarWidth,BarHeight);
g.DrawString(BallotArray[i].ToString(),new Font("宋体",9),new SolidBrush(Color.Black),new RectangleF((float)(BarLeftSpace-18.0),(float)(BMHeight-BarHeight-16),50.0f,20.0f),sf);
BarLeftSpace+=BarSpace;}
}else{
Single StartAng=0;
Single CurrentAng=0;
for(i=0;i<BallotArray.Length;i++){
float percent=(TotalBallots==0)?0.0F:(float)((float)BallotArray[i]/(float)TotalBallots);
CurrentAng=percent*360;
g.FillPie(new SolidBrush(GetColor(ColorArray[i])),1,80,PieChartWidth,PieChartHeight,StartAng,CurrentAng);
g.DrawPie(Pens.Black,1,80,PieChartWidth,PieChartHeight,StartAng,CurrentAng);
g.DrawEllipse(Pens.Black,1,80,PieChartWidth,PieChartHeight);
StartAng+=CurrentAng;
}
g.DrawString("Webdiyer([email protected])制作",new Font("宋体",9),new SolidBrush(Color.Blue),new Rectangle(0,BMHeight-22,BMWidth,22),sf);
}
for(i=0;i<OptionArray.Length;i++){
float percent=(TotalBallots==0)?0.0F:(float)((float)BallotArray[i]/(float)TotalBallots);
g.FillRectangle(new SolidBrush(GetColor(ColorArray[i])),LegendPoint.X,LegendPoint.Y,20,10);
g.DrawRectangle(Pens.Black,LegendPoint.X,LegendPoint.Y,20,10);
g.DrawString(GetLeftString(OptionArray[i],10),new Font("宋体",10),Brushes.Black,DescPoint);
g.DrawString(BallotArray[i].ToString()+"("+percent.ToString("#0.#%")+")",new Font("宋体",10),Brushes.Red,BMWidth-BallotsLength,DescPoint.Y);
LegendPoint.Y+=20;
DescPoint.Y+=20;}
}
bm.Save(Response.OutputStream,ImageFormat.Jpeg);}
Color GetColor(string colorstr){
return Color.FromArgb(Int32.Parse(colorstr));}
}
private string optionindex="1";public string OptionName{
get{this.EnsureChildControls();
return HttpContext.Current.Server.HtmlEncode(((TextBox)Controls[1]).Text.Trim());}
set{this.EnsureChildControls();
((TextBox)Controls[1]).Text=value;}
}public string InitBallots{
get{this.EnsureChildControls();
return ((TextBox)Controls[3]).Text.Trim();}
set{this.EnsureChildControls();
((TextBox)Controls[3]).Text=value;}
}public string OptionColor{
get{this.EnsureChildControls();
return Color.FromName(((HtmlSelect)Controls[5]).Value).ToArgb().ToString().Trim();}
set{for(int i=0;i<((HtmlSelect)Controls[5]).Items.Count;i++){
Color c=Color.FromName(((HtmlSelect)Controls[5]).Items[i].Value);
if(c.ToArgb().ToString()==value)
((HtmlSelect)Controls[5]).SelectedIndex=i;}
}
}public int ColorIndex{
set{this.EnsureChildControls();
((HtmlSelect)Controls[5]).SelectedIndex=value;
}
}
public String OptionIndex{
set{optionindex=value;}}protected override void CreateChildControls(){
//---------0-------
this.Controls.Add(new LiteralControl("选项"+optionindex+":"));
//-----------1------
TextBox opbox=new TextBox();
opbox.Text="";
opbox.MaxLength=75;
opbox.Columns=14;
opbox.ID="optionname_"+optionindex;
opbox.EnableViewState=false;
this.Controls.Add(opbox);
//----------2---------
this.Controls.Add(new LiteralControl(" 票数:"));
//-----------3---------
TextBox balbox=new TextBox();
balbox.Text="0";
balbox.MaxLength=8;
balbox.Width=30;
balbox.ID="ballots_"+optionindex;
balbox.EnableViewState=false;
this.Controls.Add(balbox);
//----------4----------
this.Controls.Add(new LiteralControl(" 表示颜色:"));
//----------5---------------
HtmlSelect colors=new HtmlSelect();
colors.Items.Add(new ListItem("—红色—","red"));
colors.Items.Add(new ListItem("—蓝色—","blue"));
colors.Items.Add(new ListItem("—黄色—","yellow"));
colors.Items.Add(new ListItem("—黑色—","black"));
colors.Items.Add(new ListItem("—灰色—","gray"));
colors.Items.Add(new ListItem("—粉色—","pink"));
colors.Items.Add(new ListItem("—绿色—","green"));
colors.Items.Add(new ListItem("—金色—","gold"));
colors.Items.Add(new ListItem("—银色—","silver"));
colors.Items.Add(new ListItem("—棕色—","brown"));
colors.Items.Add(new ListItem("—紫色—","purple"));
colors.Items.Add(new ListItem("—青色—","cyan"));
colors.Items.Add(new ListItem("—橙色—","orange"));
colors.ID="colors_"+optionindex;
colors.EnableViewState=false;
for(int j=0;j<colors.Items.Count;j++){
colors.Items[j].Attributes.Add("style","background-color:"+colors.Items[j].Value+";color:"+colors.Items[j].Value);}
this.Controls.Add(colors);
//----------------6------------------
this.Controls.Add(new LiteralControl("<br>"));
}
}//******************AdminLeft.aspx****************************public class AdminLeft:PollBasePage{
protected LinkButton leftbtn;
protected void SignMeOut(Object src,EventArgs e){
FormsAuthentication.SignOut();
Response.Write("<script lenguage='Javascript'>window.open('default.aspx','_top');</script>");
}
}
//***********************添加新调查页(addpoll.aspx)******************public class AddPoll:PollBasePage{
protected TextBox pollsubject;
protected DropDownList totalops;
protected PlaceHolder holder1;
protected RadioButtonList selmode;
protected RadioButtonList charttype;
protected Button subbtn;
protected Label msg;public AddPoll(){
this.Init+=new EventHandler(this.Page_Init);}
void Page_Init(Object src,EventArgs e){
InitializeComponent();}
void InitializeComponent(){
this.Load+=new EventHandler(this.Page_Load);
subbtn.Click+=new EventHandler(this.SubClick);}void Page_Load(Object src,EventArgs e){
if(!Page.IsPostBack){
totalops.DataSource=GetListItems(2,12,1);
totalops.DataBind();
totalops.SelectedIndex=2;}
ShowAddPollForm();}void PopulatePollForm(int t){
holder1.Controls.Clear();
for(int i=0;i<t;i++){
PollCtl s=new PollCtl();
s.OptionIndex=(i+1).ToString("00");
s.ColorIndex=i;
holder1.Controls.Add(s);
}
}
protected void SubClick(Object src,EventArgs e){
StringBuilder opnames=new StringBuilder();
StringBuilder opballots=new StringBuilder();
StringBuilder opcolors=new StringBuilder();
for(int i=0;i<holder1.Controls.Count;i++){
if(holder1.Controls[i] is PollCtl){
PollCtl pctl=(PollCtl)holder1.Controls[i];
if(pctl.OptionName.Trim()!=""){
opnames.Append(pctl.OptionName);
opnames.Append(",");
opballots.Append(pctl.InitBallots);
opballots.Append(",");
opcolors.Append(pctl.OptionColor);
opcolors.Append(",");
}
}
}
if(pollsubject.Text.Trim()==""){
msg.Text="调查主题不能为空!";
return;}
string[] onames=ConvertSbToArray(opnames);
string[] oballots=ConvertSbToArray(opballots);
string[] ocolors=ConvertSbToArray(opcolors);
if(onames.Length<2){
msg.Text="调查选项不能少于两项!";
return;}
else if(HaveSameValue(onames)){
msg.Text="两个选项的文字内容不能相同!";
return;}
else if(HaveSameValue(ocolors)){
msg.Text="任意两个选项的表示颜色不能相同!";
return;}
else if(IsNotNumber(oballots)){
msg.Text="起始票数必须是数字!";
return;}
string psubject=EncodeIt(pollsubject.Text.Trim());
if(HaveSamePoll(psubject,-1)){
msg.Text="已存在相同主题的调查!";
return;}
InitialCommand("poll_InsertNewPoll");
cmd.Parameters.Add("@subject",psubject);
cmd.Parameters.Add("@charttype",charttype.SelectedItem.Value);
cmd.Parameters.Add("@selectmode",selmode.SelectedItem.Value);
cmd.Parameters.Add("pollid",SqlDbType.Int,4,"id").Direction=ParameterDirection.ReturnValue;
conn.Open();
try{cmd.ExecuteNonQuery();
int pid=(int)cmd.Parameters["pollid"].Value;
for(int j=0;j<onames.Length;j++){
InitialCommand("poll_InsertPollOptions");
cmd.Parameters.Add("@pollid",pid);
cmd.Parameters.Add("@options",onames[j]);
cmd.Parameters.Add("@color",ocolors[j]);
cmd.Parameters.Add("@ballots",oballots[j]);
cmd.ExecuteNonQuery();}
msg.Text="添加新调查成功!";
}
catch(Exception ex){
msg.Text=ex.Message;}
finally{
conn.Close();}
}void ShowAddPollForm(){
PopulatePollForm(Int32.Parse(totalops.SelectedItem.Value));
}
}
protected Panel pollspanel;
protected DataList polls;
protected System.Web.UI.WebControls.Image pollchart;
public ShowAllPolls(){
this.Init+=new EventHandler(this.Page_Init);}
void Page_Init(Object src,EventArgs e){
InitializeComponent();}
void InitializeComponent(){
this.Load+=new EventHandler(this.Page_Load);}
void Page_Load(Object src, EventArgs e){
SqlDataAdapter adapter=new SqlDataAdapter("poll_SelectAllPolls",conn);
adapter.SelectCommand.CommandType=CommandType.StoredProcedure;
DataSet ds=new DataSet();
adapter.Fill(ds);
polls.DataSource=ds.Tables[0].DefaultView;
polls.DataBind();
}protected void BindOptions(Object src,DataListItemEventArgs e){
int pollid=Int32.Parse(polls.DataKeys[e.Item.ItemIndex].ToString());
string selmode="";
string charttype="";
InitialCommand("poll_SelectPollByID");
cmd.Parameters.Add("@pollid",pollid);
conn.Open();
SqlDataReader reader=cmd.ExecuteReader();
while(reader.Read()){
selmode=reader.GetString(2).Trim();
charttype=reader.GetString(1).Trim();}
reader.Close();
InitialCommand("poll_SelectOptionsByPollid");
cmd.Parameters.Add("@pollid",pollid);
reader=cmd.ExecuteReader();
CheckBoxList list1=new CheckBoxList();
list1.DataSource=reader;
list1.DataTextField="options";
list1.DataValueField="id";
RadioButtonList list2=new RadioButtonList();
list2.DataSource=reader;
list2.DataTextField="options";
list2.DataValueField="id";
PlaceHolder pholder=(PlaceHolder)e.Item.FindControl("pollholder");
if(selmode=="single"){
list2.DataBind();
pholder.Controls.Add(list2);}
else{
list1.DataBind();
pholder.Controls.Add(list1);}
reader.Close();
conn.Close();
}protected void SubPoll(Object src,DataListCommandEventArgs e){
int pollid=(int)polls.DataKeys[(int)e.Item.ItemIndex];
InitialCommand("poll_SelectPollByID");
cmd.Parameters.Add("@pollid",pollid);
conn.Open();
SqlDataReader reader=cmd.ExecuteReader();
reader.Read();
string selmode=reader.GetString(2).Trim();
reader.Close();
if(selmode=="single"){
RadioButtonList rlist=(RadioButtonList)e.Item.FindControl("pollholder").Controls[0];
if(rlist.SelectedIndex!=-1){
int opid=Int32.Parse(rlist.SelectedItem.Value);
InitialCommand("poll_UpdateOptionBallots");
cmd.Parameters.Add("@oid",opid);
cmd.ExecuteNonQuery();}
}
else{
CheckBoxList clist=(CheckBoxList)e.Item.FindControl("pollholder").Controls[0];
string opids="";
for(int i=0;i<clist.Items.Count;i++){
if(clist.Items[i].Selected)
opids+=clist.Items[i].Value+",";}
if(opids!=String.Empty){
InitialCommand("poll_UpdateOptionsBallots");
cmd.Parameters.Add("@oids",opids);
cmd.ExecuteNonQuery();}
}
conn.Close();
Response.Redirect("pollchart.aspx?pollid="+pollid);
}
}
//*******************显示调查结果图表(pollchart.aspx)*****************
public class ShowPollChart:PollBasePage{
protected System.Web.UI.WebControls.Image pollchart;
void Page_Load(Object src,EventArgs e){
int pollid=Int32.Parse(Request.QueryString["pollid"]);
pollchart.ImageUrl="chart.aspx?pollid="+pollid;
}
}//********************登录页(login.aspx)********************public class LoginModule:PollBasePage{
protected TextBox UserName;
protected TextBox UserPass;
protected Label Msg;protected void LoginBtn_Click(Object src,EventArgs e){
if(Page.IsValid){
string user=UserName.Text.Trim();
string pw=UserPass.Text.Trim();
if(FormsAuthentication.Authenticate(user,pw))
FormsAuthentication.RedirectFromLoginPage(user,false);
else
Msg.Text="身份验证失败,请正确输入您的帐号和密码!";
}
}}
protected DataGrid polls;
protected Label msg;
void Page_Load(Object src, EventArgs e){
if(!Page.IsPostBack)
BindData();
if(polls.EditItemIndex!=-1){
DropDownList opnumber=(DropDownList)polls.Items[polls.EditItemIndex].Cells[0].FindControl("opnumber");
PopulateControls(Int32.Parse(opnumber.SelectedItem.Value));}
SetPagerStyle();
}private void PopulateControls(int opnum){
DropDownList opnumber=(DropDownList)polls.Items[polls.EditItemIndex].Cells[0].FindControl("opnumber");
opnumber.DataSource=GetListItems(2,12,1);
opnumber.DataBind();
opnumber.SelectedIndex=GetSelectedIndex((DropDownList)opnumber,opnum.ToString());
PlaceHolder opholder=(PlaceHolder)polls.Items[polls.EditItemIndex].Cells[0].FindControl("holder1");
opholder.Controls.Clear();
for(int i=0;i<opnum;i++){
PollCtl p=new PollCtl();
p.OptionIndex=(i+1).ToString("00");
opholder.Controls.Add(p);}
}private void BindData(){
SqlDataAdapter adapter=new SqlDataAdapter("poll_SelectAllPolls",conn);
adapter.SelectCommand.CommandType=CommandType.StoredProcedure;
DataSet ds=new DataSet();
adapter.Fill(ds);
polls.DataSource=ds.Tables[0].DefaultView;
polls.DataBind();
}protected void EditPoll(Object src,DataGridCommandEventArgs e){
polls.EditItemIndex=e.Item.ItemIndex;
BindData();
int pid=(int)polls.DataKeys[e.Item.ItemIndex];
InitialCommand("poll_CountOptionsByPollid");
cmd.Parameters.Add("@pollid",pid);
conn.Open();
int records=(int)cmd.ExecuteScalar();
PlaceHolder opholder=(PlaceHolder)polls.Items[e.Item.ItemIndex].Cells[0].FindControl("holder1");
opholder.Controls.Clear();
PopulateControls(records);
InitialCommand("poll_SelectOptionsByPollid");
cmd.Parameters.Add("@pollid",pid);
SqlDataReader reader=cmd.ExecuteReader();
int i=0;
while(reader.Read()){
PollCtl p=(PollCtl)opholder.Controls[i];
p.OptionName=reader.GetString(1).Trim();
p.InitBallots=reader.GetInt32(3).ToString().Trim();
p.OptionColor=reader.GetString(2).Trim();
i++;}
reader.Close();
conn.Close();
}private void SetPagerStyle(){
if(polls.PageCount<=1)
polls.PagerStyle.Visible=false;
else
polls.PagerStyle.Visible=true;}protected void ChangePage(Object src,DataGridPageChangedEventArgs e){
polls.EditItemIndex=-1;
polls.CurrentPageIndex=e.NewPageIndex;
BindData();}protected void UpdatePoll(Object src,DataGridCommandEventArgs e){
int pid=(int)polls.DataKeys[e.Item.ItemIndex];
PlaceHolder opholder=(PlaceHolder)e.Item.FindControl("holder1");
StringBuilder options=new StringBuilder();
StringBuilder ballots=new StringBuilder();
StringBuilder colors=new StringBuilder();
int opnum=0;
for(int i=0;i<opholder.Controls.Count;i++){
PollCtl p=(PollCtl)opholder.Controls[i];
if(p.OptionName.Trim()!=""){
opnum++;
options.Append(p.OptionName.Trim());
options.Append(",");
ballots.Append(p.InitBallots);
ballots.Append(",");
colors.Append(p.OptionColor);
colors.Append(",");}
}
StringBuilder errmsg=new StringBuilder();
if(opnum<2)
errmsg.Append("<li>调查选项不能少于两项!<br>");
string[] optionsarr=ConvertSbToArray(options);
string[] ballotsarr=ConvertSbToArray(ballots);
string[] colorsarr=ConvertSbToArray(colors);
if(HaveSameValue(optionsarr))
errmsg.Append("<li>两个选项的文字内容不能相同!<br>");
if(IsNotNumber(ballotsarr))
errmsg.Append("<li>选项票数必须是数字!<br>");
if(HaveSameValue(colorsarr))
errmsg.Append("<li>任意两个选项不能用相同的颜色表示!<br>");
TextBox sbox=(TextBox)e.Item.FindControl("pollsubject");
string psubject=EncodeIt(sbox.Text.Trim());
if(HaveSamePoll(psubject,pid))
errmsg.Append("<li>已存在相同主题的调查!<br>");
if(errmsg.Length>0){
msg.Text="<font color=black>更新调查失败,错误信息如下:</font><br>"+errmsg.ToString();
return;}
RadioButtonList slist=(RadioButtonList)e.Item.FindControl("selmode");
RadioButtonList chlist=(RadioButtonList)e.Item.FindControl("charttype");
string smode=slist.SelectedItem.Value;
string chtype=chlist.SelectedItem.Value;
InitialCommand("poll_UpdatePoll");
cmd.Parameters.Add("@id",pid);
cmd.Parameters.Add("@subject",psubject);
cmd.Parameters.Add("@charttype",chtype);
cmd.Parameters.Add("@selectmode",smode);
conn.Open();
try{cmd.ExecuteNonQuery();
for(int i=0;i<optionsarr.Length;i++){
InitialCommand("poll_InsertPollOptions");
cmd.Parameters.Add("@pollid",pid);
cmd.Parameters.Add("@options",optionsarr[i]);
cmd.Parameters.Add("@color",colorsarr[i]);
cmd.Parameters.Add("@ballots",ballotsarr[i]);
cmd.ExecuteNonQuery();}
msg.Text="更新调查成功!";
}
catch(Exception ex){
msg.Text=ex.Message;}
finally{
conn.Close();}
polls.EditItemIndex=-1;
BindData();
}protected void CancelEdit(Object src,DataGridCommandEventArgs e){
polls.EditItemIndex=-1;
BindData();}
protected void DeletePoll(Object src,DataGridCommandEventArgs e){
int pid=(int)polls.DataKeys[e.Item.ItemIndex];
InitialCommand("poll_DeletePoll");
cmd.Parameters.Add("@pollid",pid);
conn.Open();
try{cmd.ExecuteNonQuery();
msg.Text="已有一条调查被成功删除!";
if(polls.Items.Count<=1&&polls.CurrentPageIndex>0)
polls.CurrentPageIndex=polls.CurrentPageIndex-1;}
catch(Exception ex){
msg.Text=ex.Message;}
finally{conn.Close();}
polls.EditItemIndex=-1;
BindData();
}protected int GetSelModeIndex(string selmode){
if(selmode.ToLower().Trim()=="single")
return 0;
return 1;}protected int GetChartTypeIndex(string charttype){
if(charttype.ToLower().Trim()=="bar")
return 0;
return 1;}
}}
图表组件wsChart1.0(OCX版)