在线用户    站点地图 技术论坛 文档中心 站点导航 资源下载 语言参考 教程专题 站点统计 业务项目 使用帮助
您的位置
MSDN参考新加了CSS2参考,全中文,制作精良。245k下载。           新加了sxf_sxf推荐的"动态网页专家指导" 下载!          新加了lsg2002lsg推荐的"ASP.NET高级编程(PDF)" 下载!          新加了DHTML代码预览!!,功能强大哦!欢迎试用          新加了网友lincong为大家提供的SQL Server 2000 程序员指南,有点大,但同时也就说明内容全咯!           新加了网友ibra为大家提供的北大的JAVA课件,很适合初学者入门           新加了一个java的连接缓冲池代码示例           新加了人月神话电子版下载!软件工程巨著哦~~           更新了评分标准,引入了“专家分”概念,相信可以更好的激励大家的学习!详情请看“使用帮助”           新加了由网友GGG提供的“IIS超级管理手册”,值得一看哦!(虽然我没看过 :P )           新加了“英雄榜”,欢迎测试!           “无组件上传·从原理剖析到实践”已经全部完成,源码全部开放,有兴趣的朋友请到文档中心一探究竟。  您的位置  首页>文档中心>ASP.net>文档A0000249 在同一窗口中打开页面中的链接 在新窗口中打开页面中的链接 将ASPCHINA设为首页 将ASPCHINA加入书签    
 LOGIN HERE
用户:
密码: 忘记密码
记住密码 用户注册 游客进入
相关文章

问卷调查脚本

跟我学做在线调查(四)

跟我学做在线调查(三)

跟我学做在线调查(二)

跟我学做在线调查(一)

用户控件技术

以图片上传为例谈用户控件的制作
发表文章
我写的投票系统

作者:samnt     提交人:samnt     发布时间:03-05-30     文章类型:原创     浏览量:3334
参考链结:http://    
关键字:投票|用户控件|调查|用户调查|投票系统|调查系统
[本文档没有附件]
字体:    视力保护色: 杏仁黄  秋叶褐  胭脂红  芥末绿  天蓝  雪青  灰  银河白(默认色) [A0000249]

VB.NET写的投票系统

一数据库设计
一共三个表
Investigations表:用于保存投票的标题,及说明
字段:  ID      自动递增  关键字
    Title      String    投票的标题
    Declaration  String    说明

Fields表:用于保存各个选项
字段:  ID   自动递增  关键字
    InvestigateID  Integer    投票ID
    FieldName  String    投票选项
ClickTimes表:用于保存选项点击次数
字段:  ID         自动递增  关键字
    FieldID       integer    选项ID
    ClickTimes    integer    点击次数
关系:  (1)一对多:Investiagetions.ID-Fileds.InvestigateID
    (2)一对多:Fileds.IDClickTimes.FieldID

数据库采用Access数据,对各关系实施参照完整性,级联删除及更新
二组件设计
一个三个组件:一个类,用于读写数据库,两个用户控件,用来投票与查看投票结果

(1)  读写数据库的类Investigation.vb
功能:  1)AddInvestigation    添加新调查
    2) AddInvestigationField  添加调查选项
    3)AddClick        添加点击次数
    4)  DeleteInvestigation    删除调查
    5)GetInvestigations    取得所有调查列表
    6)GetInvestigationTitle  取得一个调查的标题
    7)GetFieldClickTimes    取得一个调查的所有选项及点击次数
   
Imports System.Data.OleDb
  Public Class Investigation
    Private mConnString As String

    Public Sub New()
      Dim ar As String
      ar =Server.MapPath(“”)
      mConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & ar & "\mdb\Investigation.mdb" & ";Persist Security Info=False;"
    End Sub

    Public Function AddInvestigation(ByVal Title As String, ByVal Declaration As String) As Integer
      Dim myConn As New OleDbConnection(mConnString)
      Dim myCommand As New OleDbCommand("Insert into investigations (title,declaration) Values (?,?)", myConn)

      Try
        myConn.Open()
        With myCommand
          With .Parameters
            .Add("@Title", OleDbType.Char).Value = Title
            .Add("@Declaration", OleDbType.VarChar).Value = Declaration
          End With
          .ExecuteNonQuery()

          .CommandText = "SELECT @@IDENTITY"
          Return .ExecuteScalar
        End With
      Catch ex As Exception
        Throw ex
      Finally
        myConn.Close()
      End Try
    End Function

    Public Function AddInvestigationField(ByVal InvestigationID As Integer, ByVal FieldName As String) As Integer
      Dim myConn As New OleDbConnection(mConnString)
      Dim myCommand As New OleDbCommand()

      Try
        myConn.Open()
        With myCommand
          .Connection = myConn
          .CommandText = "Insert into Fields (InvestigateID,FieldName) Values (?,?)"
          With .Parameters
            .Add("@InvestigateID", OleDbType.Integer).Value = InvestigationID
            .Add("@FieldName", OleDbType.VarChar).Value = FieldName
          End With
          .ExecuteNonQuery()

          Dim FieldID As Integer
          .CommandText = "SELECT @@IDENTITY"
          FieldID = CInt(.ExecuteScalar)

          .CommandText = "Insert into ClickTimes (FieldID) Values (?)"
          .Parameters.Clear()
          .Parameters.Add("@FieldID", OleDbType.Integer).Value = FieldID
          .ExecuteNonQuery()
        End With
      Catch ex As Exception
        Throw ex
      Finally
        myConn.Close()
      End Try
    End Function

    Public Function AddClick(ByVal FieldID As Integer) As Integer

      Dim myConn As New OleDbConnection(mConnString)
      Dim myCommand As New OleDbCommand("Update ClickTimes set ClickTimes=[ClickTimes]+1 where FieldID=" & FieldID, myConn)

      Try
        myConn.Open()
        myCommand.ExecuteNonQuery()
      Catch ex As Exception
        Throw ex
      Finally
        myConn.Close()
      End Try
    End Function

    Public Function DeleteInvestigation(ByVal ID As Integer) As Boolean
      Dim myConn As New OleDbConnection(mConnString)
      Dim myCommand As New OleDbCommand("Delete From Investigations where ID=" & ID, myConn)

      Try
        myConn.Open()
        myCommand.ExecuteNonQuery()
      Catch ex As Exception
        Throw ex
      Finally
        myConn.Close()
      End Try
    End Function

    Public Function GetInvestigations() As DataSet
      Dim myConn As New OleDbConnection(mConnString)
      Dim myAdapter As New OleDbDataAdapter()
      Dim myCMD As New OleDbCommand()
      Dim myInvestigations As New DataSet()
     
      Try
        myCMD.CommandText = "select * from Investigations order by id"
        myCMD.Connection = myConn
        myAdapter.SelectCommand = myCMD
        myAdapter.Fill(myInvestigations, "Investigations")
        Return myInvestigations
      Catch ex As Exception
        Throw ex
      End Try
    End Function

    Public Function GetInvestigationTitle(ByVal ID As Integer) As String
      Dim myConn As New OleDbConnection(mConnString)
      Dim myCMD As New OleDbCommand()
      Dim mID As Integer

      Try
        myConn.Open()
        If ID <= 0 Then
          myCMD.CommandText = "Select Title From Investigations Order By ID Desc;"
        Else
          myCMD.CommandText = "select Title from Investigations Where ID=" & ID
        End If
        myCMD.Connection = myConn
        Return myCMD.ExecuteScalar

      Catch ex As Exception
        Throw ex
      Finally
        myConn.Close()
      End Try
    End Function

    Public Function GetFieldClickTimes(ByVal InvestigationID As Integer) As DataSet
      Dim myConn As New OleDbConnection(mConnString)
      Dim myAdapter As New OleDbDataAdapter()
      Dim myCMD As New OleDbCommand()
      Dim dsFieldClickTimes As New DataSet()

      Dim strSQL As String
      Dim inID As Integer

      Try
        myConn.Open()
        myCMD.Connection = myConn
        myAdapter.SelectCommand = myCMD

        If InvestigationID <= 0 Then
          strSQL = "Select Top 1 ID From Investigations Order By ID DESC;"
          myCMD.CommandText = strSQL
          inID = CInt(myCMD.ExecuteScalar)
        Else
          inID = InvestigationID
        End If

        strSQL = "SELECT Fields.ID,Fields.FieldName, ClickTimes.ClickTimes FROM Fields "
        strSQL += "LEFT JOIN ClickTimes ON Fields.ID = ClickTimes.FieldID "
        strSQL += "WHERE (((Fields.InvestigateID)=" & inID & "));"

        myCMD.CommandText = strSQL

        myAdapter.Fill(dsFieldClickTimes, "FieldClickTimes")
        Return dsFieldClickTimes
      Catch ex As Exception
        Throw ex
      Finally
        myConn.Close()
      End Try
    End Function

  End Class

(2)  投票用户控件
功能:定义了一个属性InvestigationID,只要给定InvestigationID就能显示相应的调查,并能将调查结果写入数据库。并定向的调查结果页

ModuleInvestigation.ascx中定义三个控件
<TABLE id="Table1" cellSpacing="1" cellPadding="1" width="300" border="1">
  <TR>
    <TD>
      <asp:Label id="lblTitle" runat="server" Width="279px">Label</asp:Label></TD>
  </TR>
  <TR>
    <TD>
      <asp:RadioButtonList id="FieldRadioBtn" runat="server" Width="180px" Height="146px"></asp:RadioButtonList></TD>
  </TR>
  <TR>
    <TD>
      <asp:Button id="SubmitButton" runat="server" Width="65px" Text="提交"></asp:Button></TD>
  </TR>
</TABLE>
ModuleInvestigation.ascs.vb中代码如下:
Public MustInherit Class ModuleInvestigation
    Inherits System.Web.UI.UserControl
    Protected WithEvents SubmitButton As System.Web.UI.WebControls.Button
    Protected WithEvents lblTitle As System.Web.UI.WebControls.Label
    Protected WithEvents FieldRadioBtn As System.Web.UI.WebControls.RadioButtonList
    Protected mInvestigationID As Integer = -1

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      If Not Me.IsPostBack Then WriteSubject(mInvestigationID)
    End Sub

    Public Property InvestigatioID() As Integer
      Get
        Return mInvestigationID
      End Get
      Set(ByVal Value As Integer)
        mInvestigationID = Value
      End Set
    End Property

    Private Sub WriteSubject(ByVal InvestigationID As Integer)
      Dim myIV As New Investigation()
      Dim dsFields As New DataSet()
      Dim Title As String
      Title = myIV.GetInvestigationTitle(InvestigationID)
      dsFields = myIV.GetFieldClickTimes(InvestigationID)
      lblTitle.Text = Title
      With FieldRadioBtn
        .DataSource = dsFields
        .DataMember = "FieldClickTimes"
        .DataTextField = "FieldName"
        .DataValueField = "ID"
        .DataBind()
      End With
    End Sub

    Private Sub SubmitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SubmitButton.Click
      Dim myIV As New Investigation()
      myIV.AddClick(CInt(FieldRadioBtn.SelectedItem.Value))
      Response.Redirect("dcResult.aspx?ID=" & mInvestigationID)
    End Sub

  End Class
(3)  投票结果用户控件
功能:定义了一个属性InvestigationID,只要给定InvestigationID就能显示相应的调查结果

ModuleInvestigationResult.ascx中添加一个表格控件
<asp:Table id="Table1" runat="server" Width="309px" Height="109px"></asp:Table>

ModuleInvestigationResult.ascx.vb中代码如下:
Public MustInherit Class ModuleInvestigationResult
    Inherits System.Web.UI.UserControl
    Protected WithEvents Table1 As System.Web.UI.WebControls.Table
    Protected mInvestigationID As Integer = -1

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      If Not Me.IsPostBack Then
        ShowResult(mInvestigationID)
      End If
    End Sub

    Public Property InvestigationID() As Integer
      Get
        Return mInvestigationID
      End Get
      Set(ByVal Value As Integer)
        mInvestigationID = Value
        ShowResult(mInvestigationID)
      End Set
    End Property

    Public Sub Refresh()
      ShowResult(mInvestigationID)
    End Sub

    Private Sub ShowResult(ByVal mInvestigationID As Integer)
      Dim myIV As New Investigation()
      Dim dsF As DataSet
      dsF = myIV.GetFieldClickTimes(InvestigationID)
      Table1.Rows.Clear()
      Dim r As DataRow
      For Each r In dsF.Tables(0).Rows
        Dim newRow As New TableRow()
        Dim newCell1 As New TableCell()
        Dim newCell2 As New TableCell()
        Dim newCell3 As New TableCell()

        If r.IsNull("ClickTimes") Then
          newCell1.Text = r.Item("FieldName")
          newCell2.Text = " 0次"
        Else
          newCell1.Text = r.Item("FieldName")
          newCell2.Text = r.Item("ClickTimes") & "次"
          newCell3.Text = "<span style='WIDTH: " & CInt(r.Item("ClickTimes") * 20) & "px; HEIGHT: 20px; BACKGROUND-COLOR: #0033ff'></span><br>"
        End If
        newRow.Cells.Add(newCell1)
        newRow.Cells.Add(newCell2)
        newRow.Cells.Add(newCell3)
        Table1.Rows.Add(newRow)
      Next
    End Sub
  End Class
三界面设计
一共四个页面:Investigation.aspx让用户投票,Result.aspx,查看投票结果,Add.aspx添加新的调查,Check.aspx查看、删除所有调查

(1)用户投票页Investigation.aspx
在这个页中,只要拖放一个ModuleInvestigation.ascx用户控件就行了。没有任何代码。当然你可以放到任何页中

(2)查看投票结果页码Result.aspx
在这个页中,只要拖放一个ModuleInvestigationResult.ascx用户控件就行了.代码文件中要给这个控件设置一个属性InvestigationID.

Protected ModuleInvestigationResult1 As ModuleInvestigationResult
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      ModuleInvestigationResult1.InvestigationID = CInt(Request.QueryString("ID"))
    End Sub

(3)添加新调查Add.aspx的页面
这个页面要复杂一点。添加三个控件用来输入新调查的标题,选项及说明。另有一个控件Listbox用来显示,删除已添加的选项
<TABLE id="Table1" style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 8px" cellSpacing="1" cellPadding="1" width="760" border="0">
        <TR>
          <TD><FONT face="宋体">调查标题:</FONT></TD>
          <TD><asp:textbox id="TitleTextBox" runat="server" Width="365px"></asp:textbox></TD>
          <TD></TD>
        </TR>
        <TR>
          <TD><FONT face="宋体">添加选项</FONT></TD>
          <TD>
            <asp:textbox id="FieldTextBox" runat="server"></asp:textbox><asp:button id="AddButton" runat="server" Text="添加"></asp:button></TD>
          <TD></TD>
        </TR>
        <TR>
          <TD><FONT face="宋体"></FONT></TD>
          <TD>
            <asp:listbox id="FieldsListBox" runat="server" Width="230px" Height="140px"></asp:listbox><asp:button id="RemoveButton" runat="server" Text="移除"></asp:button></TD>
          <TD></TD>
        </TR>
        <TR>
          <TD><FONT face="宋体">调查说明:</FONT></TD>
          <TD><asp:textbox id="DeclarationTextBox" runat="server" Width="368px" Height="100px"></asp:textbox></TD>
          <TD></TD>
        </TR>
        <TR>
          <TD></TD>
          <TD><asp:button id="SubmitButton" runat="server" Width="108px" Text="提交"></asp:button></TD>
          <TD></TD>
        </TR>
      </TABLE>

Add.aspx.vb的代码如下:
Inherits System.Web.UI.Page
    Protected WithEvents TitleTextBox As System.Web.UI.WebControls.TextBox
    Protected WithEvents FieldTextBox As System.Web.UI.WebControls.TextBox
    Protected WithEvents AddButton As System.Web.UI.WebControls.Button
    Protected WithEvents FieldsListBox As System.Web.UI.WebControls.ListBox
    Protected WithEvents RemoveButton As System.Web.UI.WebControls.Button
    Protected WithEvents DeclarationTextBox As System.Web.UI.WebControls.TextBox
    Protected WithEvents SubmitButton As System.Web.UI.WebControls.Button

    Private Sub SubmitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SubmitButton.Click
      Dim myIV As New Investigation()
      Dim IVID As Integer
      Dim i As ListItem
      IVID = myIV.AddInvestigation(TitleTextBox.Text, DeclarationTextBox.Text)

      For Each i In FieldsListBox.Items
        myIV.AddInvestigationField(IVID, i.Text)
      Next
      TitleTextBox.Text = ""
      FieldTextBox.Text = ""
      DeclarationTextBox.Text = ""
      FieldsListBox.Items.Clear()
    End Sub

    Private Sub AddButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddButton.Click
      FieldsListBox.Items.Add(FieldTextBox.Text)
      FieldTextBox.Text = ""
    End Sub

    Private Sub RemoveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RemoveButton.Click
      With FieldsListBox
        .Items.Remove(.SelectedItem)
      End With
    End Sub
 
(4)检查页面Check.aspx文件
功能:查阅所有的调查项目及投票结果,并能删除指定的调查
<TABLE id="Table1" style="Z-INDEX: 101; LEFT: 8px; WIDTH: 504px; POSITION: absolute; TOP: 8px; HEIGHT: 352px" cellSpacing="1" cellPadding="1" width="504" border="1">
        <TR>
          <TD><FONT face="宋体"></FONT></TD>
          <TD><FONT face="宋体">浏览调查结果</FONT></TD>
          <TD></TD>
        </TR>
        <TR>
          <TD><FONT face="宋体">选择调查:</FONT></TD>
          <TD>
            <asp:DropDownList id="TitleDDL" runat="server" Width="157px" AutoPostBack="True"></asp:DropDownList></TD>
          <TD></TD>
        </TR>
        <TR>
          <TD style="HEIGHT: 216px"></TD>
          <TD style="HEIGHT: 216px">
            <uc1:ModuleInvestigationResult id="ModuleInvestigationResult1" runat="server"></uc1:ModuleInvestigationResult></TD>
          <TD style="HEIGHT: 216px"></TD>
        </TR>
        <TR>
          <TD></TD>
          <TD>
            <asp:Button id="DeleteButton" runat="server" Text="删除"></asp:Button></TD>
          <TD><FONT face="宋体"></FONT></TD>
        </TR>
        <TR>
          <TD></TD>
          <TD></TD>
          <TD></TD>
        </TR>
      </TABLE>

Check.aspx.vb代码  :
    Protected WithEvents TitleDDL As System.Web.UI.WebControls.DropDownList
    Protected WithEvents DeleteButton As System.Web.UI.WebControls.Button
    Protected dsInv As DataSet
    Protected ModuleInvestigationResult1 As ModuleInvestigationResult

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      If Not Me.IsPostBack Then
        InitTitleDDL()
      Else
        With ModuleInvestigationResult1
          .InvestigationID = CInt(TitleDDL.SelectedItem.Value)
          .Refresh()
        End With
      End If
    End Sub

    Private Sub DeleteButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteButton.Click
      With TitleDDL
        Dim newInv As New Investigation()
        If .SelectedIndex >= 0 Then
          newInv.DeleteInvestigation(CInt(.SelectedItem.Value))
          InitTitleDDL()
        End If
      End With
    End Sub

    Private Sub InitTitleDDL()
      Dim newInv As New Investigation()
      dsInv = newInv.GetInvestigations
      With TitleDDL
        .DataSource = dsInv
        .DataTextField = "Title"
        .DataValueField = "ID"
        .DataBind()
        ModuleInvestigationResult1.InvestigationID = CInt(.Items(0).Value)
      End With
  End Sub

小结
上面介绍的用户调查系统,还有很多可以考虑的问题:
(1)  界面设计
几乎没有界面设计,特别是图形显示投票的多少,这个“图形”只是一个色条。
(2)  投票控制
一个人可以任意投票,完全没有控制。这一点在实际应用的时候,可以结合系统处理。如果在一个要求用户登陆的站点,可以根据用户ID来控制。如果是一个匿名的站点可以根据IP地址来控制。
(3)  数据显示
应该用百分比好
(4)  调查的选择
当数据库中有多个调查时,默认显示的是最新的调查。做得更灵活一点是在管理页面中能指定任意一个调查。这并不难实现。

匆匆搞定bug难免,多多指教。

关于这篇文章的评论 [注意:这里仅仅是给大家提供了一个发表对文章本身看法的地方,如果有疑问,请到论坛提出] 我要提问!
godly        2003/5/31 11:39:00
Good Things
程序通过,界面慢慢在自己搞,谢谢了
标题
内容
发言
*您尚未以注册用户身份登录,不能发表评论。这里登录
您的位置
  (c)2000-2020 Yup Studio, all rights reserved.  
191.4063