|
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Button Butvote;
protected System.Web.UI.WebControls.RadioButtonList RadioButtonList1;
protected System.Web.UI.WebControls.Button Butred;
private string voteID="2";
private void Page_Load(object sender, System.EventArgs e)
{
if(!this.IsPostBack)
{
//创建连接
SqlConnection sqlcon=DB.care();
sqlcon.Open();
//查询投票标题
SqlCommand cmd=new SqlCommand("select voteTitle from voteMaster where voteID="+this.voteID,sqlcon);
string title=Convert.ToString(cmd.ExecuteScalar());
this.Label1.Text=title;
//查询标题的内容
SqlCommand scd=new SqlCommand("select voteDetailsID ,voteItem from voteDetails where voteID="+this.voteID,sqlcon);
SqlDataReader sdr=scd.ExecuteReader();
this.RadioButtonList1.DataSource=sdr;
this.RadioButtonList1.DataTextField="voteItem";
this.RadioButtonList1.DataValueField="voteDetailsID";
this.RadioButtonList1.DataBind();
sdr.Close();
sqlcon.Close();
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.RadioButtonList1.SelectedIndexChanged += new System.EventHandler(this.RadioButtonList1_SelectedIndexChanged);
this.Butvote.Click += new System.EventHandler(this.Butvote_Click);
this.Butred.Click += new System.EventHandler(this.Butred_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Butvote_Click(object sender, System.EventArgs e)
{
SqlConnection sqlcon=DB.care();
sqlcon.Open();
SqlCommand com=new SqlCommand();
com.Connection=sqlcon;
com.CommandText="Update voteDetails set voteNum=voteNum+1 where voteID="+ voteID +"and voteDetailsID="+RadioButtonList1.SelectedValue.ToString();
int vot=com.ExecuteNonQuery();
if(vot!=0)
{
Response.Write("<script language='javascript'>alert('投票成功');window.location.href='show.aspx?voteid="+this.voteID+" ' ;</script>");
}
else
{
Response.Write("投票失败");
}
sqlcon.Close();
}
private void RadioButtonList1_SelectedIndexChanged(object sender, System.EventArgs e)
{
}
private void Butred_Click(object sender, System.EventArgs e)
{
Response.Redirect("show.aspx?voteid="+this.voteID);
}
}
}
|
|