一、cs頁面代碼
using System;
using System.Data;
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;
using System.Data.SqlClient;
using System.Configuration;
public partial class diaocha : System.Web.UI.Page
{
private string dcid;
private string leixing;
private string ConnStr = null;
protected void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
// 在此處放置用戶代碼以初始化頁面
ConnStr = ConfigurationSettings.AppSettings["connstr"];
SqlConnection con = new SqlConnection(ConnStr);
con.Open();
SqlCommand cmd = new SqlCommand("select top 1 id,title,types from V_title order by id desc", con);
SqlDataReader rs = cmd.ExecuteReader();
if (rs.Read())
{
Labtitle.Text = rs["Title"].ToString();
dcid = rs["id"].ToString();
leixing = rs["types"].ToString();
Label1.Text=rs["types"].ToString();
Label2.Text = rs["id"].ToString();
}
rs.Close();
SqlCommand cmd1 = new SqlCommand("select id,cont from V_vote where lid='" + dcid + "' order by id desc", con);
SqlDataReader rs1 = cmd1.ExecuteReader();
if (leixing == "1") //1爲單選2爲多選
{
dcradio.DataSource = rs1;
dcradio.Visible = true;
dcradio.DataTextField = "cont";
dcradio.DataValueField = "id";
dcradio.DataBind();
}
else
{
CheckBoxListdc.DataSource = rs1;
CheckBoxListdc.Visible = true;
CheckBoxListdc.DataTextField = "cont";
CheckBoxListdc.DataValueField = "id";
CheckBoxListdc.DataBind();
}
rs1.Close();
}
}
protected void ImageButton1_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
//---------------------- 採用Cookie用於記錄投票者的投票情況
HttpCookie makecookie = new HttpCookie("SocutVote");//製造cookie
HttpCookie readcookie = Request.Cookies["SocutVote"];//讀出cookie
if (readcookie != null)//從未投過票
{
Page.RegisterStartupScript("Startup", "<script>alert('您已經投票');</script>");
}
else
{
if (this.Label1.Text == "1")
{
if (dcradio.SelectedIndex != -1) //如果有選定項目
{
//Response.Write(dcradio.SelectedValue.ToString());
ConnStr = ConfigurationSettings.AppSettings["connstr"];
SqlConnection con = new SqlConnection(ConnStr);
con.Open();
SqlCommand cmdupdate = new SqlCommand();
cmdupdate.Connection = con;
cmdupdate.CommandText = "update V_vote set vcount=vcount+1 where id='" + this.dcradio.SelectedValue.ToString() + "'";
cmdupdate.ExecuteNonQuery();
//添加投票人數
SqlCommand cmdrenshu = new SqlCommand();
cmdrenshu.Connection = con;
cmdrenshu.CommandText = "update V_title set renshu=renshu+1 where id='" + this.Label2.Text + "'";
cmdrenshu.ExecuteNonQuery();
Response.AppendCookie(makecookie); //寫入Cookie
Page.RegisterStartupScript("Startup", "<script>alert('感謝你投票,您的投票已經提交,我們會改進自己的工作');</script>");
}
else//如果沒有選中項目
{
Page.RegisterStartupScript("Startup", "<script>alert('出錯:請選擇調查項');</script>");
}
}
else
{
if (CheckBoxListdc.SelectedIndex != -1)
{
for (int i = 0; i < this.CheckBoxListdc.Items.Count; i++)
{
if (this.CheckBoxListdc.Items[i].Selected)
{
ConnStr = ConfigurationSettings.AppSettings["connstr"];
SqlConnection con = new SqlConnection(ConnStr);
con.Open();
SqlCommand cmdupdate1 = new SqlCommand();
cmdupdate1.Connection = con;
cmdupdate1.CommandText = "update V_vote set vcount=vcount+1 where id='" + this.CheckBoxListdc.Items[i].Value.ToString() + "'";
cmdupdate1.ExecuteNonQuery();
Response.AppendCookie(makecookie); //寫入Cookie
SqlCommand cmdrenshu1 = new SqlCommand();
cmdrenshu1.Connection = con;
cmdrenshu1.CommandText = "update V_title set count=count+1 where id='" + this.Label2.Text + "'";
cmdrenshu1.ExecuteNonQuery();
}
}
Page.RegisterStartupScript("Startup", "<script>alert('感謝你投票,您的投票已經提交,我們會改進自己的工作');</script>");
}
else
{
Page.RegisterStartupScript("Startup", "<script>alert('出錯:請選擇調查項');</script>");
}
}
}
}
}
二、aspx頁面代碼
<table width="195" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="center">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="52"><img src="images/dcbd1.jpg" width="52" height="37" /></td>
<td background="images/dcbd2.jpg" class="white_txt">滿意度調查</td>
<td align="right" background="images/dcbd2.jpg" class="white_txt"><img src="images/dcbd2_2.jpg" width="6" height="37" /></td>
</tr>
</table>
</td>
</tr>
<tr>
<td align="center" background="images/dcbd3.jpg">
<table width="89%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td height="36" class="lv_txt"> 歡迎你進行評價。<asp:Label ID="Label2" runat="server" Visible="False"></asp:Label>
<asp:Label ID="Label1" runat="server" Visible="False"></asp:Label></td>
</tr>
<tr>
<td height="30" style="text-align: center"><strong><asp:Label ID="Labtitle" runat="server"></asp:Label></strong></td>
</tr>
<tr>
<td align="center" rowspan="2" style="height: 18px">
<table width="89%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td height="36" align=left><asp:RadioButtonList ID="dcradio" runat="server" Height="26px" Width="144px" Visible="False">
</asp:RadioButtonList>
<asp:CheckBoxList ID="CheckBoxListdc" runat="server" Height="31px" Width="144px">
</asp:CheckBoxList></td>
</tr>
</table>
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="../images/toupiao.gif"
OnClick="ImageButton1_Click" />
<a href="dcresult.aspx" target=_blank><img src="images/chakan.gif" border="0"></a></td>
</tr>
<tr>
</tr>
</table>
</td>
</tr>
<tr>
<td valign="top" background="images/dcbd3.jpg" bgcolor="#FFFFFF"><img src="images/dcbd4.jpg" width="195" height="11" /></td>
</tr>
</table>
效果如下圖: