|
******************************
Author: J.Ramkrishna Murty |
Date created 15-January-2003 |
Webmail [email protected] |
Organization 3Mice Interactive Media Limited, Nairobi Kenya. |
Check the author’s other articles here |
www.3mice.com |
ASP.NET
[ OPINION POLLS ] by J.Ramkrishna Murty
A
Very Simple but usefull ASP.NET Opinion poll example.
Installation: Download
the "ASP.NET[Opinion Polls Ver1.1].zip" file and create a folder called
polls in your "c:/Inetpub/wwwroot" and to test the polls open your
web browser and type "http://localhost/polls/Default.aspx"
—————————-Default.aspx——————————————————————
<%@ Page Language="vb" Explicit="true" Debug="true"
%>
<%@ Import Namespace="System.Text" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OleDb" %>
<html>
<head>
<meta name="GENERATOR" Content="ASP Express 2.1">
<title>J.Ramkrshna Murty – [ASP.NET Opinion Polls – Ver1.0]</title>
<script language="VB" runat="server">
Sub Page_Change(sender As Object, e As DataGridPageChangedEventArgs)
MyDataGrid.CurrentPageIndex = e.NewPageIndex
BindData()
End Sub
Sub Page_Load(Source As Object, E As EventArgs)
If Not Page.IsPostBack Then
BindData()
End If
If Page.IsPostBack Then
End If
End Sub
Sub BindData()
‘1. Create a connection
Dim objConn
objConn=New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source="
& Server.MapPath("polls.mdb") & ";")
objConn.Open
‘2. Create a command object for the query
Const strSQL as String = "select * from polls where currentquestion=0"
Dim objCmd as New OleDbCommand(strSQL, objConn)
‘3. Create/Populate the DataReader
Dim objDR as OleDbDataReader
objDR = objCmd.ExecuteReader()
MyDataGrid.DataSource = objDR
MyDataGrid.DataBind()
End Sub
</script>
</head>
<body>
<div id=divResults runat=server />
<asp:Datagrid runat="server"
Id="MyDataGrid"
GridLines="Both"
cellpadding="0"
cellspacing="0"
BorderWidth="0"
Headerstyle-BackColor="white"
Headerstyle-Font-Name="Arial"
Headerstyle-Font-Bold="True"
Headerstyle-Font-Size="14"
BackColor="white"
Font-Name="Arial"
Font-Size="11"
AlternatingItemStyle-BackColor="white"
AlternatingItemStyle-Font-Name="Arial"
AlternatingItemStyle-Font-Size="11"
BorderColor="white"
AllowPaging = "false"
AllowCustomPaging ="false"
PageSize = "8"
PagerStyle-Mode = "NumericPages"
PagerStyle-HorizontalAlign="Center"
PagerStyle-PageButtonCount = "10"
OnPageIndexChanged = "Page_Change"
AutogenerateColumns="False"
Width="50%">
<Columns>
<asp:TemplateColumn HeaderText="">
<ItemTemplate>
<form action="results.aspx" id="sample1" method="get">
<table bgcolor="#FFFFFF" cellspacing="1" border="2"
cellpadding="4">
<tr>
<td width="384" bordercolor="#FF9900" bgcolor="#FF9900">
<div align="center"><strong><font color="#FFFFFF"
size="2" face="Verdana, Arial, Helvetica, sans-serif">ASP.NET
– Opinion Polls – Ver1.0</font></strong></div></td>
</tr>
<tr><td>
<table width="100%" border="0" cellspacing="0"
cellpadding="5">
<tr>
<td colspan="2"> <font size="2" face="Verdana,
Arial, Helvetica, sans-serif">
<%# DataBinder.Eval(Container.DataItem, "question") %>
</font></td>
</tr>
<tr>
<td width="11%">
<input type="hidden" name="pollid" id="pollid"
value="<%# DataBinder.Eval(Container.DataItem, "currentquestion")
%>">
<input type="radio" name="choice" value="Choice1,<%#
DataBinder.Eval(Container.DataItem, "votes1")%>"></td>
<td width="89%"><font size="2" face="Verdana,
Arial, Helvetica, sans-serif">
<%# DataBinder.Eval(Container.DataItem, "choice1") %>
</font></td>
</tr>
<tr>
<td><input type="radio" name="choice" value="Choice2,<%#
DataBinder.Eval(Container.DataItem, "votes2")%>"></td>
<td><font size="2" face="Verdana, Arial, Helvetica,
sans-serif">
<%# DataBinder.Eval(Container.DataItem, "choice2") %>
</font></td>
</tr>
<tr>
<td><input type="radio" name="choice" value="Choice3,<%#
DataBinder.Eval(Container.DataItem, "votes3")%>"></td>
<td><font size="2" face="Verdana, Arial, Helvetica,
sans-serif">
<%# DataBinder.Eval(Container.DataItem, "choice3") %>
</font></td>
</tr>
<tr>
<td><input type="radio" name="choice" value="Choice4,<%#
DataBinder.Eval(Container.DataItem, "votes4")%>"></td>
<td><font size="2" face="Verdana, Arial, Helvetica,
sans-serif">
<%# DataBinder.Eval(Container.DataItem, "choice4") %>
</font><input type="submit" id=submit value="Vote"></td>
</tr>
</table>
</td></tr>
</table>
</form>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
</body>
</html>
————————–results.aspx———————————————————————
<%@ Page Language="vb" Explicit="true" Debug="true"%>
<%@ Import Namespace="System.Text" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OleDb" %>
<html>
<head>
<meta name="GENERATOR" Content="ASP Express 2.1">
<title>J.Ramkrshna Murty – [ASP.NET Opinion Polls – Ver1.0]</title>
<script language="VB" runat="server">
Sub Page_Change(sender As Object, e As DataGridPageChangedEventArgs)
BindData()
End Sub
Sub Page_Load(Source As Object, E As EventArgs)
If Not Page.IsPostBack Then
BindData()
End If
End Sub
Sub BindData()
‘1. Create a connection
Dim objConn
objConn=New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source="
& Server.MapPath("polls.mdb") & ";")
‘You must open the db connection before populating the DataReader
objConn.Open()
Dim strSQL as String
strSQL="select * from polls where currentquestion="& Request.querystring("pollid")
Dim objCmd as New OleDbCommand(strSQL, objConn)
‘3. Create/Populate the DataReader
Dim objDR as OleDbDataReader
objDR = objCmd.ExecuteReader()
MyDataGrid.DataSource = objDR
MyDataGrid.DataBind()
‘4.Updating Polls
‘5. Create a New connection
Dim objConn1
objConn1=New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source="
& Server.MapPath("polls.mdb") & ";")
Dim votevalue
Dim ChoiceValue
Dim VotedFor
ChoiceValue=(left(request.QueryString("choice"),7))
if choiceValue="Choice1" then
VotedFor="Votes1"
else if choiceValue="Choice2" then
VotedFor="Votes2"
else if choiceValue="Choice3" then
VotedFor="Votes3"
else if choiceValue="Choice4" then
VotedFor="Votes4"
end if
votevalue=(Cint(right(request.QueryString("choice"),(len(request.QueryString("choice"))-8)))+1)
‘2. Create the command object, passing in the SQL string
Dim strSQL1 as String = "update polls set "& VotedFor &"="
& votevalue+1 & " where currentquestion="& Request.querystring("pollid")
& ""
Dim myCommand as New OleDbCommand(strSQL1, objConn1)
objConn1.Open()
myCommand.ExecuteReader(CommandBehavior.CloseConnection)
End Sub
</script>
</head>
<body>
<div id=divResults runat=server />
<asp:Datagrid runat="server"
Id="MyDataGrid"
GridLines="Both"
cellpadding="0"
cellspacing="0"
BorderWidth="0"
Headerstyle-BackColor="white"
Headerstyle-Font-Name="Arial"
Headerstyle-Font-Bold="True"
Headerstyle-Font-Size="14"
BackColor="white"
Font-Name="Arial"
Font-Size="11"
AlternatingItemStyle-BackColor="white"
AlternatingItemStyle-Font-Name="Arial"
AlternatingItemStyle-Font-Size="11"
BorderColor="white"
AllowPaging = "false"
AllowCustomPaging ="false"
PageSize = "8"
PagerStyle-Mode = "NumericPages"
PagerStyle-HorizontalAlign="Center"
PagerStyle-PageButtonCount = "10"
OnPageIndexChanged = "Page_Change"
AutogenerateColumns="False"
Width="50%">
<Columns>
<asp:TemplateColumn HeaderText="">
<ItemTemplate>
<table bgcolor="#FFFFFF" cellspacing="1" border="2"
cellpadding="4">
<tr>
<td width="384" bordercolor="#FF9900" bgcolor="#FF9900">
<div align="center"><strong><font color="#FFFFFF"
size="2" face="Verdana, Arial, Helvetica, sans-serif">ASP.NET
– Opinion Polls – Ver1.0</font></strong></div></td>
</tr>
<tr><td>
<table width="100%" border="0" cellspacing="0"
cellpadding="5">
<tr>
<td width="72%"><font size="2" face="Verdana,
Arial, Helvetica, sans-serif">
<%# DataBinder.Eval(Container.DataItem, "choice1")%>
</font></td>
<td width="28%"><font size="2" face="Verdana,
Arial, Helvetica, sans-serif">
<%#Cint(DataBinder.Eval(Container.DataItem, "votes1")/(DataBinder.Eval(Container.DataItem,
"votes1")+DataBinder.Eval(Container.DataItem, "votes2")+DataBinder.Eval(Container.DataItem,
"votes3")+DataBinder.Eval(Container.DataItem, "votes4"))*100)%>
% </font></td>
</tr>
<tr>
<td><font size="2" face="Verdana, Arial, Helvetica,
sans-serif">
<%# DataBinder.Eval(Container.DataItem, "choice2")%>
</font></td>
<td><font size="2" face="Verdana, Arial, Helvetica,
sans-serif">
<%#Cint(DataBinder.Eval(Container.DataItem, "votes2")/(DataBinder.Eval(Container.DataItem,
"votes1")+DataBinder.Eval(Container.DataItem, "votes2")+DataBinder.Eval(Container.DataItem,
"votes3")+DataBinder.Eval(Container.DataItem, "votes4"))*100)%>
% </font></td>
</tr>
<tr>
<td><font size="2" face="Verdana, Arial, Helvetica,
sans-serif">
<%# DataBinder.Eval(Container.DataItem, "choice3")%>
</font></td>
<td><font size="2" face="Verdana, Arial, Helvetica,
sans-serif">
<%# Cint(DataBinder.Eval(Container.DataItem, "votes3")/(DataBinder.Eval(Container.DataItem,
"votes1")+DataBinder.Eval(Container.DataItem, "votes2")+DataBinder.Eval(Container.DataItem,
"votes3")+DataBinder.Eval(Container.DataItem, "votes4"))*100)%>
% </font></td>
</tr>
<tr>
<td><font size="2" face="Verdana, Arial, Helvetica,
sans-serif">
<%# DataBinder.Eval(Container.DataItem, "choice4")%>
</font></td>
<td><font size="2" face="Verdana, Arial, Helvetica,
sans-serif">
<%# Cint(DataBinder.Eval(Container.DataItem, "votes4")/(DataBinder.Eval(Container.DataItem,
"votes1")+DataBinder.Eval(Container.DataItem, "votes2")+DataBinder.Eval(Container.DataItem,
"votes3")+DataBinder.Eval(Container.DataItem, "votes4"))*100)%>
% </font></td>
</tr>
</table>
</td></tr>
</table>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
</body>
</html>
—————————————————-polls.mdb————————————————————————–
id ——————————–
Number
pollsessionid
——————— Number
question
——————– Text
polldate
——————- Text
choice1
——————- Text
choice2
——————- Text
choice3
——————– Text
choice4
——————- Text
votes1———————
(Number)
votes2
——————— (Number)
votes3
——————— (Number)
votes4
——————— (Number)
currentquestion
——————— (Yes/No)
Click Here
to Download the Code
******************************
|
|