default.aspx.cs

来自「编写一个选举网页」· CS 代码 · 共 48 行

CS
48
字号
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page 
{
    private string voteID = "2";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            SqlConnection con = DB.createConnect();
            con.Open();
            SqlCommand cmd = new SqlCommand("select voteTitle from voteTitle where voteID=" + this.voteID, con);
            string title = Convert.ToString(cmd.ExecuteScalar());
            this.lblTitle.Text = title;
            SqlCommand cmdItem = new SqlCommand("select * from voteDetail where voteID=" + this.voteID, con);
            SqlDataReader sdr = cmdItem.ExecuteReader();
            this.rblContent.DataSource = sdr;
            this.rblContent.DataTextField = "voteContent";
            this.rblContent.DataValueField = "voteDetailID";
            this.rblContent.DataBind();
            sdr.Close();
            con.Close();
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        SqlConnection con = DB.createConnect();
        con.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = "update voteDetail set voteNum=voteNum+1 where voteID=" + this.voteID + " and voteDetailID=" + this.rblContent.SelectedValue.ToString();
        cmd.ExecuteNonQuery();
        con.Close();
    }
    protected void btnResult_Click(object sender, EventArgs e)
    {
        Response.Redirect("showResult.aspx?voteID="+this.voteID);
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?