asp.net c voting system (anti-brush function)

Source: Internet
Author: User
Tags urlencode

ASP tutorial. NET C voting system (anti-brush function)
This article is the use of the ASP.net tutorial mssql made a brush voting system, he was using cookies to achieve.
*/

using System;
Using System.Configuration;
Using System.Data;
Using System.Linq;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.HtmlControls;
Using System.Web.UI.WebControls;
Using System.web.ui.webcontrols.webparts;
Using System.Xml.Linq;
Using System.Data.SqlClient;

public partial class _default:system.web.ui.page
{
Protected string title = "1";

protected void Page_Load (object sender, EventArgs e)
{
if (!ispostback)
{
Votefun ();
}
}

protected void Votefun ()
{

SqlConnection conn = Dbconn.conn ();
SqlCommand cmd = new SqlCommand ("Select Vitems from votetest where vid = '" + title + "", conn);
Try
{
Conn.Open ();
This.showtitle.text = Convert.ToString (Cmd.executescalar ());
Cmd.commandtext = "SELECT * from votedetails where vid = '" + title + "'";
This.rbl.datasource = Cmd.executereader ();
This.rbl.datatextfield = "Contents";
This.rbl.datavaluefield = "id";
This.rbl.databind ();
}
catch (SqlException ex)
{
Response.Write (Ex.errors.tostring ());
}
Finally
{
Conn.close ();
Cmd.dispose ();
}
}
Vote


protected void Butvoteyes_click (object sender, EventArgs e)
{
if (This.rbl.selectedindex!=-1)
{
Vote against cheating
HttpCookie Makecookie = new HttpCookie ("vote");//Manufacturing Cookies
HttpCookie Readcookie = request.cookies["vote"];//read cookies
Response.Write ("if (Readcookie = null)//Never cast a ticket
{
Makecookie.values.add ("Voteitem", title);
}
Else
{
String strallitem = readcookie.values["Voteitem"].tostring ()//Read the voted item
if (Strallitem.indexof (title) = = 1)//No vote
{
Makecookie.values.add ("Voteitem", readcookie.values["Voteitem"] + title);
}
else//If you've already voted.
{
Response.Write ("<script language= page Special effects >alert (' You've successfully voted, you can't vote again!") ');</script> ");
Return

}
}
Response.appendcookie (Makecookie);

String selectid = This.rbl.selectedvalue.tostring ();
SqlConnection conn = Dbconn.conn ();
SqlCommand cmd = new SqlCommand ("update votedetails set num = num +1 where id = '" + Selectid + "", conn);
Try
{
Conn.Open ();
Cmd.executenonquery ();
Showmessage.box ("Vote for success!") ");
}
catch (SqlException ex)
{
Response.Write (Ex.errors.tostring ());
}
Finally
{
Conn.close ();
Cmd.dispose ();
}
}
Else
{
Showmessage.box ("Please select the voting item");
}
}
<summary>
View Results
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
protected void Butvoteno_click (object sender, EventArgs e)
{
Response.Redirect ("showvotedetails.aspx?vid=" +title+ "");
}


#region Test Code
public static string M_str_voteid;
protected void Page_Load (object sender, EventArgs e)
//{
if (!ispostback)
//    {
Get the voting title chosen by the user
M_str_voteid = request["Voteid"];
Binding voting titles and voting options
Labbind ();
Rblbind ();
//    }
//}
Binding Label Control
private void Rblbind ()
//{
DataSet ds = Db.reds ("Select Votetitle from Tb_vote where voteid=" + M_str_voteid);
Labvotetitle.text = Ds.tables[0].rows[0][0].tostring ();
//}
Binding RadioButtonList Controls
private void Labbind ()
//{
DataSet ds = Db.reds ("select * from Tb_vote where voteid=" + M_str_voteid);
Rblvoteitem.datasource = ds;
The Votecontent field is bound to Text,voteitemid bound to value
Rblvoteitem.datatextfield = "Votecontent";
Rblvoteitem.datavaluefield = "Voteitemid";
Rblvoteitem.databind ();
//}
Voting button
protected void Btnvote_click (object sender, EventArgs e)
//{
Vote against cheating
HttpCookie Makecookie = new HttpCookie ("vote" + M_str_voteid);//Create Cookie
HttpCookie Readcookie = request.cookies["vote" + m_str_voteid];//Read cookies
if (Readcookie = null)
//    {
If you have never voted, put the voting title in the cookie and set the expiration time
Makecookie.values.add ("Voteitem", "<" + M_str_voteid + ">");
Makecookie.expires = DateTime.MaxValue;
//    }
Else
//    {
Read items that have been voted
String p_str_allitem = readcookie.values["Voteitem"].tostring ();
Add the topic to the cookie if you have not voted on the subject
if (P_str_allitem.indexof ("<" + M_str_voteid + ">") = = 1)
//        {
Makecookie.values.add ("Voteitem", readcookie.values["Voteitem"] + "<" + M_str_voteid + ">");
//        }
Else
//        {
Response.Write ("<script Language=javascript>alert" you've voted and can't vote again!) ');</script> ");
It's critical to return here, just jump out of the Btnvote_click event
Return
//        }
//    }
+1 votes for voting operation
string p_str_voteitemid = This.rblvoteitem.selectedvalue;
String p_str_cmdtxt = "Update Tb_voteitem set votetotal=votetotal+1 where voteitemid=" + P_str_voteitemid + "and VO Teid= "+ M_str_voteid;
BOOL P_bl_reval = Db.exsql (p_str_cmdtxt);
if (p_bl_reval)
//    {
Write Cookie
Response.appendcookie (Makecookie);
Pop a poll in a new window
Response.Write ("<script Language=javascript>alert") (' Vote for success, thank you for your support! '); Windows.open (' voteresult.aspx?voteid= "+ M_str_voteid +" &title= "+ server.urlencode (labvotetitle.text) +" ', ' New ');</script> ");
//    }
Else
//    {
Response.Write ("<script Language=javascript>alert") (' Vote failed! ');</script> ");
//    }
//}
View Results button
protected void Btnresult_click (object sender, EventArgs e)
//{
Response.Write ("<script Language=javascript>alert") (' Vote for success, thank you for your support! '); Windows.open (' voteresult.aspx?voteid= "+ M_str_voteid +" &title= "+ server.urlencode (labvotetitle.text) +" ', ' New ');</script> ");
//}
#endregion
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.