简单在线调查,单选和多选(c#)

来源:互联网 发布:西安天互数据招聘 编辑:程序博客网 时间:2024/05/29 13:56

一、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>
                &nbsp;<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="../images/toupiao.gif"
                    OnClick="ImageButton1_Click" />
                &nbsp;
                <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>

 
原创粉丝点击