codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  problem to create button in asp.net  rosezha at 02:24 on Thursday, February 16, 2006
 

hi everyone,

Now i doing online quiz using asp.net and xml. So i want to save the result in database. The problem is i cannot create a button in resultscreen page. I got this error " button must in form runat server tag"
Below i paste the coding :

Imports System.Data
Imports System.Data.SqlClient
Imports System.Xml
Imports System.Web

Public Class mate1
Inherits System.Web.UI.Page
Dim strXmlFilePath As String = Server.MapPath("/forum/quiz.xml")
Dim xDoc As XmlDocument = New XmlDocument
Dim intTotalQuestion As Integer
Dim intQuestionNo As Integer = 1
Dim intScore As Integer = 0
Dim arrAnswerHistory As New ArrayList

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

End Sub
Protected WithEvents lbname As System.Web.UI.WebControls.Label
Protected WithEvents lblIDpljr As System.Web.UI.WebControls.Label
Protected WithEvents lblQuestion As System.Web.UI.WebControls.Label
Protected WithEvents rblAnswer As System.Web.UI.WebControls.RadioButtonList
Protected WithEvents Requiredfieldvalidator1 As System.Web.UI.WebControls.RequiredFieldValidator
Protected WithEvents btnSubmit As System.Web.UI.WebControls.Button
Protected WithEvents lblMarkah As System.Web.UI.WebControls.Label
Protected WithEvents lblTotalQuestion As System.Web.UI.WebControls.Label
Protected WithEvents lblTimeSpent As System.Web.UI.WebControls.Label
Protected WithEvents lkdone As System.Web.UI.WebControls.LinkButton
Protected WithEvents lblResult As System.Web.UI.WebControls.Label
Protected WithEvents QuizScreen As System.Web.UI.HtmlControls.HtmlGenericControl
Protected WithEvents Form1 As System.Web.UI.HtmlControls.HtmlForm
Protected WithEvents ResultScreen As System.Web.UI.HtmlControls.HtmlGenericControl

'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

lbname.Text = Session("BsName")
lblIDpljr.Text = Session("BsID")
'bttTamat.Visible = True

'Load xml data
xDoc.Load(strXmlFilePath)

'Start a new quiz?
If Not Page.IsPostBack Then

'Yes! Count total question
intTotalQuestion = xDoc.SelectNodes("/quiz/mchoice").Count

'Record start time
ViewState("StartTime") = DateTime.Now

ShowQuestion(intQuestionNo)
End If
End Sub

Public Sub btnSubmit_Click(ByVal src As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click

'Retrieve essential variables from state bag
intTotalQuestion = ViewState("TotalQuestion")
intQuestionNo = ViewState("QuestionNo")
intScore = ViewState("Score")
arrAnswerHistory = ViewState("AnswerHistory")
'cmdNext = ViewState("Next")

'Correct answer?
If rblAnswer.SelectedItem.Value = ViewState("CorrectAnswer") Then
intScore += 1
arrAnswerHistory.Add(0)
Else
arrAnswerHistory.Add(rblAnswer.SelectedItem.Value)
End If

'End of quiz?
If intQuestionNo = intTotalQuestion Then

'Yes! Show the result...
QuizScreen.Visible = False
ResultScreen.Visible = True



'Render result screen
ShowResult()


Else

'Not yet! Show another question...
QuizScreen.Visible = True
ResultScreen.Visible = False
intQuestionNo += 1

'Render next question
ShowQuestion(intQuestionNo)
End If
End Sub


Sub ShowQuestion(ByVal intQuestionNo As Integer)
'lbname.Text = Session("BsName")
Dim xNodeList As XmlNodeList
Dim xNodeAttr As Object
Dim strXPath As String
Dim i As Integer
Dim tsTimeSpent As TimeSpan

strXPath = "/quiz/mchoice[" & intQuestionNo.ToString() & "]"

'Extract question
lblQuestion.Text = intQuestionNo.ToString() & ". " & xDoc.SelectSingleNode(strXPath & "/question").InnerXml

'Extract answers
xNodeList = xDoc.SelectNodes(strXPath & "/answer")

'Clear previous listitems
rblAnswer.Items.Clear()

For i = 0 To xNodeList.Count - 1

'Add item to radiobuttonlist
rblAnswer.Items.Add(New ListItem(xNodeList.Item(i).InnerText, i + 1))

'Extract correct answer
xNodeAttr = xNodeList.Item(i).Attributes.ItemOf("correct")
If Not xNodeAttr Is Nothing Then
If xNodeAttr.Value = "yes" Then
ViewState("CorrectAnswer") = i + 1
End If
End If
Next

'Output Total Question
lblTotalQuestion.Text = intTotalQuestion

'Output Time Spent
tsTimeSpent = DateTime.Now.Subtract(ViewState("StartTime"))
lblTimeSpent.Text = tsTimeSpent.Minutes.ToString() & ":" & tsTimeSpent.Seconds.ToString()
lblMarkah.Text = intScore.ToString() & " / " & intTotalQuestion.ToString()
'Store essential data to viewstate
ViewState("TotalQuestion") = intTotalQuestion
ViewState("Score") = intScore
ViewState("QuestionNo") = intQuestionNo
ViewState("AnswerHistory") = arrAnswerHistory



End Sub

Sub ShowResult()
Dim strResult As String
Dim i As Integer
Dim strXPath As String
Dim tsTimeSpent As TimeSpan

tsTimeSpent = DateTime.Now.Subtract(ViewState("StartTime"))

strResult = "<center>"
strResult += "<h3>Keputusan Kuiz </h3>"

' lblIDpljr.Text = Session("BsID")

strResult += "<p>Markah: " & intScore.ToString() & " / " & intTotalQuestion.ToString()
strResult += "<p>Masa Digunakan: " & tsTimeSpent.Minutes.ToString() & ":" & tsTimeSpent.Seconds.ToString()
strResult += "</center>"

strResult += "<h3>Perincian Kuiz:</h3>"

For i = 1 To intTotalQuestion
strXPath = "/quiz/mchoice[" & i.ToString() & "]"
strResult += "<b>" & i.ToString() & ". " & xDoc.SelectNodes(strXPath & "/question").Item(0).InnerXml & "</b><br>"
If arrAnswerHistory.Item(i - 1) = 0 Then
strResult += "<font color=""green""><b>Tahniah! Jawapan anda tepat!</b></font><br><br>"
Else
strResult += "<b>Jawapan Anda:</b> " & xDoc.SelectNodes(strXPath & "/answer[" & arrAnswerHistory.Item(i - 1).ToString() & "]").Item(0).InnerXml & "<br>"
strResult += "<font color=""red""><b>Maaf! Jawapan anda tidak tepat!</b></font><br><br>"
End If
Next

lblResult.Text = strResult

End Sub

Sub doInsert(ByVal Source As Object, ByVal E As EventArgs)
Dim conn As String = "server=INTERNETHOST; uid=sa; pwd=macadd25; Initial catalog=kuiz;"
Dim sqlcon As New SqlConnection(conn)

sqlcon.Open()
Dim MySQL As String = "Insert into markahpljr(Pelajar,Markah,Masa) " & " Values (@id, @result, @time)"
Dim CmdNext As New SqlCommand(MySQL, sqlcon)
With CmdNext.Parameters
.Add(New SqlParameter("@id", lblIDpljr.Text))
.Add(New SqlParameter("@result", lblMarkah.Text))
.Add(New SqlParameter("@time", lblTimeSpent.Text))
End With
CmdNext.ExecuteNonQuery()
sqlcon.Close()

Server.Transfer("/forum/login.html")

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Response.Redirect("/forum/pilihsubjek.html")
End Sub
End Class


Please help me! Thank you









CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums
//








Recent Forum Threads
•  saving??
•  Reading an ASCII file
•  Date script issues
•  perl script help needed
•  onChange issue
•  perl remote execution
•  Chat application
•  How to send multiple perameters in SOAP request.
•  Java code for Insert picture on the table in spreadsheet


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2007