 |
| .NET Framework 1.x For discussing versions 1.0 and 1.1 of the Microsoft .NET Framework. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the .NET Framework 1.x section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

February 7th, 2007, 03:03 AM
|
|
Registered User
|
|
Join Date: Feb 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Problems with Dynamically created controls
Hi,
I tryed what u suggested,but when the Page reloads ,no controls appears.
I m describing my problem in detail,
I m creating a web app. in ASP.Net 1.0,Named Online Test,in which all the questions and their associated options are coming from the database.In a web form ,a drawed a static PlaceHolder control and according to the no. of questions label controls and radiobuttonlist controls are created dynamically.the questions are randomly selected from the database,for ex .if there are 10 questions in the database,10 label controls and 10 radiobuttonlist controls will be drawn dynamically on a placeholder.I m doing all this coding in a Page load event.
Now my problem is that,
when the first time Page Load event fires,randomly selected questions appeared on the form,the user has to select the appropriate answers by selecting one options for each of the ques.after doing this when the user clicks on the submit button which is also created dynamically,the Page load event fires again and next sequence of questions appeared on the form.What I want that Questions must be coming only for the first time.until the user runs the application again.
what I want that after selecting the answers of the questions when the user clickes on the submit button,valuation of his exam will be done by checking his answer with the correct answer which is also stoed in the database.
My code is..........
imports system.data.sqlclient
' all the variables are declared globally here
dim cn as adodb.connection
dim rabbttnlist as radiobuttonlist
dim submit as button
dim lblquno as label ' for displaying qu. no
dim lblqu ' for displaying question
dim val() as long
private sub Page_load()
Dim MyValue As Long
Dim o1, o2, o3, o4, s, qu As String
If Not IsPostBack Then
cn.ConnectionString = "Data Source=localhost;Initial Catalog=QuestionDB;User ID=sa;Password=;
If cn.State = ConnectionState.Open Then
cn.Close()
Else
cn.Open()
End If
'-- Get a count of records
str = "SELECT Count(*) FROM QuestionTab"
cmd = New SqlCommand
cmd.CommandText = str
cmd.CommandType = CommandType.Text
cmd.Connection = cn
recordcount = cmd.ExecuteScalar()
cn.Close()
i = 1
Dim val(recordcount) As Long
repeat:
While i <= recordcount
MyValue = CInt(Int((recordcount * Rnd()) + 1))
For j = 1 To val.Length - 1
If val(j) = MyValue Then ' for checking no generated is not duplicated
GoTo repeat
End If
Next
sqlstring = "select * from QuestionTab where SNo= " & MyValue & ""
da = New SqlDataAdapter(sqlstring, cn)
ds = New DataSet
da.Fill(ds)
qu = ds.Tables(0).Rows(0).Item("Qu")
's = ds.Tables(0).Rows(0).Item("SNo")
o1 = ds.Tables(0).Rows(0).Item("Op1")
o2 = ds.Tables(0).Rows(0).Item("Op2")
o3 = ds.Tables(0).Rows(0).Item("Op3")
o4 = ds.Tables(0).Rows(0).Item("Op4")
Radbttnlist = New RadioButtonList
Radbttnlist.AutoPostBack = False
Radbttnlist.ID = "Radbttnlist" & i
Radbttnlist.Items.Add(New ListItem(o1))
Radbttnlist.Items.Add(New ListItem(o2))
Radbttnlist.Items.Add(New ListItem(o3))
Radbttnlist.Items.Add(New ListItem(o4))
Radbttnlist.RepeatDirection = RepeatDirection.Horizontal
lblquno = New Label
lblquno.ID = "labelQuNo " & i
lblquno.Text = "Qu. " & i
lblquno.Width = Unit.Parse("50px")
lblqu = New Label
lblqu.Text = qu
lblqu.ID = "lblqu" & i
Ph.Controls.Add(lblquno)
Ph.Controls.Add(lblqu)
Ph.Controls.Add(New LiteralControl("<br/>"))
Ph.Controls.Add(Radbttnlist)
Ph.Controls.Add(New LiteralControl(""))
val(i) = MyValue
i = i + 1
End While
bttnsubmit = New Button
bttnsubmit.ID = "bttnsubmit" & i
bttnsubmit.Text = "Submit"
Ph.Controls.Add(bttnsubmit)
cn.Close()
bttnsubmit = New Button
AddHandler bttnsubmit.Click, New EventHandler(AddressOf Me.submit)
Else
End If
Pls reply me as soon as possible
thanks
Preeti
|
|

March 7th, 2007, 01:30 PM
|
|
Authorized User
|
|
Join Date: Mar 2007
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Ok, First, I ran into this problem way back. There are a couple things to look at here.
If not ispostback is saying, if your not posting back, build these. But you are indeed posting back on submit, so its not rebuilding them.
This then means that the button wont fire, because its not there, and more importantly, neither is the data.
So, to do it dynamically you will need to build it each time, but then you run in to he problem with the 'random' questions. Since your using a random, it would theoretically load different questions each time,, which wont do either so....
Now, if the 'values' or 'items' change at all, it will NOT hold viewstate. So the questions must be the same.
So, here we go. In this test code you will notice that the viewstate holds the list to the selected item
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
buildNewForm()
End Sub
Private Sub doClick(ByVal sender As Object, ByVal e As System.EventArgs)
Response.Write("clicked")
End Sub
Private Sub buildNewForm()
Dim myList As New DropDownList
For x As Integer = 0 To 5
Dim myItem As New ListItem
myItem.Text = "Item " & x
myList.Items.Add(myItem)
Next
PlaceHolder1.Controls.Add(myList)
Dim mybutton As New Button
mybutton.CommandName = "Submit"
AddHandler mybutton.Click, AddressOf doClick
PlaceHolder1.Controls.Add(mybutton)
End Sub
In this one it doesnt because the 'values' are changing for the listbox
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
buildNewForm()
End Sub
Private Sub doClick(ByVal sender As Object, ByVal e As System.EventArgs)
Response.Write("clicked")
End Sub
Private Sub buildNewForm()
Dim myList As New DropDownList
For x As Integer = 0 To 5
Dim myItem As New ListItem
myItem.Text = "Item " & x & Now()
myList.Items.Add(myItem)
Next
PlaceHolder1.Controls.Add(myList)
Dim mybutton As New Button
mybutton.CommandName = "Submit"
AddHandler mybutton.Click, AddressOf doClick
PlaceHolder1.Controls.Add(mybutton)
End Sub
Now in essence heres how you make this work.
1: You have two different binding functions, (for simplicity) one binds from database pull, other binds from a session/cookie/temp table that was created from the first pull.
The flow is like this
sub page load
buildNewQuestionsAndButton()
end sub
sub buildNewQuestionsAndButton()
myDatatable = generateRandomQuestions()
options here are to store to session/cookie/temptable etc. may want to send to name/value pairs, and store to session if the list is small, probably easier.
storeToSession(myQuestionData)
buildForm()
buildbutton()
addhandler button.click, addressof doSubmitFunc
end sub
sub doSubmitFunc()
buildformFromStoredQuestionData()
validateForm()
BuildNewQuestionsAndButton()
end sub
Now this code does just that but simplified:
Partial Class test
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
buildNewForm()
Else
buildOldForm()
End If
End Sub
Private Sub doClick(ByVal sender As Object, ByVal e As System.EventArgs)
'Do any validating you want, i only stored the questions so if you were right, it would go to next question. This obviously is not what you want, but it will give you an idea of how to validate and move on
Dim mybox As TextBox
Dim rand1 As Integer
Dim rand2 As Integer
Dim correctAnswer As Integer
Try
rand1 = CInt(Session("rand1"))
rand2 = CInt(Session("rand2"))
correctAnswer = rand1 + rand2
Catch ex As Exception
End Try
mybox = PlaceHolder1.FindControl("answ")
Dim myAnswer As Integer
Try
myAnswer = CInt(mybox.Text)
Catch ex As Exception
myAnswer = -1
End Try
If myAnswer = correctAnswer Then
buildNewForm()
End If
End Sub
Private Sub buildNewForm() 'build a new set of questions
PlaceHolder1.Controls.Clear()
Response.Write("//New Questions" & Now.ToShortTimeString)
Dim rand1 As Integer = doRandom()
Dim rand2 As Integer = doRandom()
Dim myQuestion As String = "What is " & rand1 & " + " & rand2
Session("myQuestion") = myQuestion
Session("rand1") = rand1
Session("rand2") = rand2
Dim myLabel As New Label
myLabel.Text = myQuestion
PlaceHolder1.Controls.Add(myLabel)
Dim myTextbox As New TextBox
myTextbox.ID = "answ"
PlaceHolder1.Controls.Add(myTextbox)
Dim mybutton As New Button
mybutton.CommandName = "Submit"
AddHandler mybutton.Click, AddressOf doClick
PlaceHolder1.Controls.Add(mybutton)
End Sub
Private Sub buildOldForm() 'redo the last set of questions
Response.Write("//Repost" & Now.ToShortTimeString)
Dim myLabel As New Label
myLabel.Text = Session("myQuestion")
PlaceHolder1.Controls.Add(myLabel)
Dim myTextbox As New TextBox
myTextbox.ID = "answ"
PlaceHolder1.Controls.Add(myTextbox)
Dim mybutton As New Button
mybutton.CommandName = "Submit"
AddHandler mybutton.Click, AddressOf doClick
PlaceHolder1.Controls.Add(mybutton)
End Sub
Private Function doRandom() As Integer 'get a number so you know its either a new or old question
Dim MyNewRandomNum
Randomize()
MyNewRandomNum = (Rnd() * 100) + 1
Return MyNewRandomNum
End Function
End Class
This will work as you intend, it can be greatly enhanced, but I did it like this for simplicity sake.
I really hope this helps.
|
|
 |