Asp.net c voting system (Anti-click farming)

Source: Internet
Author: User

Asp tutorial. net c voting system (Anti-click farming)
This article uses the asp.net tutorial mssql to create an anti-click voting system, which uses cookies.
*/

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.html controls;
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.exe cutescalar ());
Cmd. commandtext = "select * from votedetails where vid = '" + title + "'";
This. rbl. datasource = cmd.exe cutereader ();
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 to prevent cheating
Httpcookie makecookie = new httpcookie ("vote"); // create a cookie
Httpcookie readcookie = request. cookies ["vote"]; // read cookie
// Response. write ("If (readcookie = null) // never vote
{
Makecookie. values. add ("voteitem", title );
}
Else
{
String strallitem = readcookie. values ["voteitem"]. tostring (); // read the items that have been voted
If (strallitem. indexof (title) =-1) // no votes have been cast
{
Makecookie. values. add ("voteitem", readcookie. values ["voteitem"] + title );
}
Else // if you have voted
{
Response. write ("<script language = webpage special effect> alert ('you have successfully voted for this topic and cannot 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.exe cutenonquery ();
Showmessage. box ("Vote successful! ");
}
Catch (sqlexception ex)
{
Response. write (ex. errors. tostring ());
}
Finally
{
Conn. close ();
Cmd. dispose ();
}
}
Else
{
Showmessage. box ("select a voting item ");
}
}
/// <Summary>
/// View the result
/// </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)
//{
//// Obtain the selected voting title
// M_str_voteid = request ["voteid"];
//// Bind the voting title and voting options
// Labbind ();
// Rblbind ();
//}
//}
/// Bind the 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 ();
//}
//// Bind the radiobuttonlist Control
// Private void labbind ()
//{
// Dataset ds = db. reds ("select * from tb_vote where voteid =" + m_str_voteid );
// Rblvoteitem. datasource = ds;
/// Bind the votecontent field to text and voteitemid to value.
// Rblvoteitem. datatextfield = "votecontent ";
// Rblvoteitem. datavaluefield = "voteitemid ";
// Rblvoteitem. databind ();
//}
//// Vote button
// Protected void btnvote_click (object sender, eventargs e)
//{
//// Vote to prevent cheating
// Httpcookie makecookie = new httpcookie ("vote" + m_str_voteid); // create a cookie
// Httpcookie readcookie = request. cookies ["vote" + m_str_voteid]; // read cookie
// If (readcookie = null)
//{
/// If you have never voted, put the voting title into the cookie and set the expiration time
// Makecookie. values. add ("voteitem", "<" + m_str_voteid + "> ");
// Makecookie. expires = datetime. maxvalue;
//}
// Else
//{
//// Read the voted items
// String p_str_allitem = readcookie. values ["voteitem"]. tostring ();
//// If you have not voted for the topic, add the topic to the cookie.
// 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 have voted for this topic and cannot vote again! '); </Script> ");
//// Here return is very important. Jump Out Of The btnvote_click event directly.
// Return;
//}
//}
//// Execute the voting operation, with the number of votes + 1
// String p_str_voteitemid = this. rblvoteitem. selectedvalue;
// String p_str_cmdtxt = "update tb_voteitem set votetotal = votetotal + 1 where voteitemid =" + p_str_voteitemid + "and voteid =" + m_str_voteid;
// Bool p_bl_reval = db. exsql (p_str_cmdtxt );
// If (p_bl_reval)
//{
//// Write cookie
// Response. appendcookie (makecookie );
//// The voting result is displayed in the new window.
// Response. write ("<script language = javascript> alert ('vote successful. 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 the result button
// Protected void btnresult_click (object sender, eventargs e)
//{
// Response. write ("<script language = javascript> alert ('vote successful. Thank you for your support! '); Windows. open ('voteresult. aspx? Voteid = "+ m_str_voteid +" & title = "+ server. urlencode (labvotetitle. text) +" ', 'new'); </script> ");
//}
# Endregion
}

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.