radiobuttonlist控件去改变数据库里面的值
小弟刚刚接触asp.net想做一个类似投票功能的web窗体想利用radiobuttonlist控件的值改变数据库里面的int数值,使其+1,想要段代码,请高手赐教。...
小弟刚刚接触asp.net 想做一个类似投票功能的web窗体 想利用radiobuttonlist控件的值改变数据库里面的int数值,使其+1,想要段代码,请高手赐教。
展开
展开全部
前台:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Example_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table cellspacing="2px">
<tr>
<td rowspan="4" style="width: 68px">
<asp:RadioButtonList ID="RadioButtonList1" runat="server">
<asp:ListItem Value="vote1">投票1</asp:ListItem>
<asp:ListItem Value="vote2">投票2</asp:ListItem>
<asp:ListItem Value="vote3">投票3</asp:ListItem>
<asp:ListItem Value="vote4">投票4</asp:ListItem>
</asp:RadioButtonList></td>
<td style="width: 474px; background-color: silver;">
<asp:Label ID="Label1" runat="server" BackColor="#C0FFC0" Height="22px"></asp:Label></td>
</tr>
<tr>
<td style="width: 474px; background-color: silver;">
<asp:Label ID="Label2" runat="server" BackColor="#C0FFC0" Height="22px"></asp:Label></td>
</tr>
<tr>
<td style="width: 474px; background-color: silver;">
<asp:Label ID="Label3" runat="server" BackColor="#C0FFC0" Height="22px"></asp:Label></td>
</tr>
<tr>
<td style="width: 474px; background-color: silver;">
<asp:Label ID="Label4" runat="server" BackColor="#C0FFC0" Height="22px"></asp:Label></td>
</tr>
</table>
</div>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="投票" />
</form>
</body>
</html>
后台cs
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
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;
public partial class Example_Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
getVote();
}
}
private void getVote()
{
double sumVote = 0.00;
SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=test;Persist Security Info=True;User ID=sa;Password=sa");
try
{
if (conn.State != ConnectionState.Open)
{
conn.Open();
}
string sql = "select sum(votenumber),voteid from tb_vote group by voteid";
string sqlCount = "select sum(votenumber) from tb_vote";
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
SqlDataAdapter daCount = new SqlDataAdapter(sqlCount, conn);
DataSet ds = new DataSet();
DataSet dsCount = new DataSet();
daCount.Fill(dsCount);
da.Fill(ds);
if(dsCount != null && dsCount.Tables.Count > 0)
{
sumVote = double.Parse(dsCount.Tables[0].Rows[0][0].ToString());
}
if (ds != null && ds.Tables.Count > 0)
{
if (sumVote != 0)
{
Label1.Text = Convert.ToString(System.Math.Round((double.Parse(ds.Tables[0].Rows[0][0].ToString()) / sumVote) * 100, 2)) + "%";
Label2.Text = Convert.ToString(System.Math.Round((double.Parse(ds.Tables[0].Rows[1][0].ToString()) / sumVote) * 100, 2)) + "%";
Label3.Text = Convert.ToString(System.Math.Round((double.Parse(ds.Tables[0].Rows[2][0].ToString()) / sumVote) * 100, 2)) + "%";
Label4.Text = Convert.ToString(System.Math.Round((double.Parse(ds.Tables[0].Rows[3][0].ToString()) / sumVote) * 100, 2)) + "%";
int wid1 = int.Parse(Convert.ToString(System.Math.Round(474 * System.Math.Round((double.Parse(ds.Tables[0].Rows[0][0].ToString()) / sumVote), 2),0)));
int wid2 = int.Parse(Convert.ToString(System.Math.Round(474 * System.Math.Round((double.Parse(ds.Tables[0].Rows[1][0].ToString()) / sumVote), 2),0)));
int wid3 = int.Parse(Convert.ToString(System.Math.Round(474 * System.Math.Round((double.Parse(ds.Tables[0].Rows[2][0].ToString()) / sumVote), 2),0)));
int wid4 = int.Parse(Convert.ToString(System.Math.Round(474 * System.Math.Round((double.Parse(ds.Tables[0].Rows[3][0].ToString()) / sumVote), 2), 0)));
Label1.Width = Unit.Pixel(wid1);
Label2.Width = Unit.Pixel(wid2);
Label3.Width = Unit.Pixel(wid3);
Label4.Width = Unit.Pixel(wid4);
}
}
}
catch
{
}
finally
{
if (conn.State != ConnectionState.Closed)
{
conn.Close();
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string vote_id = "";
SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=test;Persist Security Info=True;User ID=sa;Password=sa");
try
{
if (conn.State != ConnectionState.Open)
{
conn.Open();
}
for (int i = 0; i < RadioButtonList1.Items.Count; i++)
{
if (RadioButtonList1.Items[i].Selected)
{
vote_id = RadioButtonList1.Items[i].Value.ToString();
}
}
string sql = "update tb_vote set votenumber= votenumber+1 where voteid='" + vote_id + "'";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.ExecuteNonQuery();
getVote();
}
catch
{
}
finally
{
if (conn.State != ConnectionState.Closed)
{
conn.Close();
}
}
}
}
数据表脚本:
USE [test]
GO
/****** 对象: Table [dbo].[tb_vote] 脚本日期: 05/13/2010 11:26:48 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[tb_vote](
[voteid] [nvarchar](20) COLLATE Chinese_PRC_CI_AS NOT NULL,
[votenumber] [int] NULL,
CONSTRAINT [PK_tb_vote_1] PRIMARY KEY CLUSTERED
(
[voteid] ASC
) ON [PRIMARY]
) ON [PRIMARY]
insert into tb_vote values ('vote1',0)
go
insert into tb_vote values ('vote2',0)
go
insert into tb_vote values ('vote3',0)
go
insert into tb_vote values ('vote4',0)
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Example_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table cellspacing="2px">
<tr>
<td rowspan="4" style="width: 68px">
<asp:RadioButtonList ID="RadioButtonList1" runat="server">
<asp:ListItem Value="vote1">投票1</asp:ListItem>
<asp:ListItem Value="vote2">投票2</asp:ListItem>
<asp:ListItem Value="vote3">投票3</asp:ListItem>
<asp:ListItem Value="vote4">投票4</asp:ListItem>
</asp:RadioButtonList></td>
<td style="width: 474px; background-color: silver;">
<asp:Label ID="Label1" runat="server" BackColor="#C0FFC0" Height="22px"></asp:Label></td>
</tr>
<tr>
<td style="width: 474px; background-color: silver;">
<asp:Label ID="Label2" runat="server" BackColor="#C0FFC0" Height="22px"></asp:Label></td>
</tr>
<tr>
<td style="width: 474px; background-color: silver;">
<asp:Label ID="Label3" runat="server" BackColor="#C0FFC0" Height="22px"></asp:Label></td>
</tr>
<tr>
<td style="width: 474px; background-color: silver;">
<asp:Label ID="Label4" runat="server" BackColor="#C0FFC0" Height="22px"></asp:Label></td>
</tr>
</table>
</div>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="投票" />
</form>
</body>
</html>
后台cs
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
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;
public partial class Example_Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
getVote();
}
}
private void getVote()
{
double sumVote = 0.00;
SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=test;Persist Security Info=True;User ID=sa;Password=sa");
try
{
if (conn.State != ConnectionState.Open)
{
conn.Open();
}
string sql = "select sum(votenumber),voteid from tb_vote group by voteid";
string sqlCount = "select sum(votenumber) from tb_vote";
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
SqlDataAdapter daCount = new SqlDataAdapter(sqlCount, conn);
DataSet ds = new DataSet();
DataSet dsCount = new DataSet();
daCount.Fill(dsCount);
da.Fill(ds);
if(dsCount != null && dsCount.Tables.Count > 0)
{
sumVote = double.Parse(dsCount.Tables[0].Rows[0][0].ToString());
}
if (ds != null && ds.Tables.Count > 0)
{
if (sumVote != 0)
{
Label1.Text = Convert.ToString(System.Math.Round((double.Parse(ds.Tables[0].Rows[0][0].ToString()) / sumVote) * 100, 2)) + "%";
Label2.Text = Convert.ToString(System.Math.Round((double.Parse(ds.Tables[0].Rows[1][0].ToString()) / sumVote) * 100, 2)) + "%";
Label3.Text = Convert.ToString(System.Math.Round((double.Parse(ds.Tables[0].Rows[2][0].ToString()) / sumVote) * 100, 2)) + "%";
Label4.Text = Convert.ToString(System.Math.Round((double.Parse(ds.Tables[0].Rows[3][0].ToString()) / sumVote) * 100, 2)) + "%";
int wid1 = int.Parse(Convert.ToString(System.Math.Round(474 * System.Math.Round((double.Parse(ds.Tables[0].Rows[0][0].ToString()) / sumVote), 2),0)));
int wid2 = int.Parse(Convert.ToString(System.Math.Round(474 * System.Math.Round((double.Parse(ds.Tables[0].Rows[1][0].ToString()) / sumVote), 2),0)));
int wid3 = int.Parse(Convert.ToString(System.Math.Round(474 * System.Math.Round((double.Parse(ds.Tables[0].Rows[2][0].ToString()) / sumVote), 2),0)));
int wid4 = int.Parse(Convert.ToString(System.Math.Round(474 * System.Math.Round((double.Parse(ds.Tables[0].Rows[3][0].ToString()) / sumVote), 2), 0)));
Label1.Width = Unit.Pixel(wid1);
Label2.Width = Unit.Pixel(wid2);
Label3.Width = Unit.Pixel(wid3);
Label4.Width = Unit.Pixel(wid4);
}
}
}
catch
{
}
finally
{
if (conn.State != ConnectionState.Closed)
{
conn.Close();
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string vote_id = "";
SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=test;Persist Security Info=True;User ID=sa;Password=sa");
try
{
if (conn.State != ConnectionState.Open)
{
conn.Open();
}
for (int i = 0; i < RadioButtonList1.Items.Count; i++)
{
if (RadioButtonList1.Items[i].Selected)
{
vote_id = RadioButtonList1.Items[i].Value.ToString();
}
}
string sql = "update tb_vote set votenumber= votenumber+1 where voteid='" + vote_id + "'";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.ExecuteNonQuery();
getVote();
}
catch
{
}
finally
{
if (conn.State != ConnectionState.Closed)
{
conn.Close();
}
}
}
}
数据表脚本:
USE [test]
GO
/****** 对象: Table [dbo].[tb_vote] 脚本日期: 05/13/2010 11:26:48 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[tb_vote](
[voteid] [nvarchar](20) COLLATE Chinese_PRC_CI_AS NOT NULL,
[votenumber] [int] NULL,
CONSTRAINT [PK_tb_vote_1] PRIMARY KEY CLUSTERED
(
[voteid] ASC
) ON [PRIMARY]
) ON [PRIMARY]
insert into tb_vote values ('vote1',0)
go
insert into tb_vote values ('vote2',0)
go
insert into tb_vote values ('vote3',0)
go
insert into tb_vote values ('vote4',0)
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询