N
Nathan Sokalski
I have a section of my code that dynamically creates LinkButtons to allow
the user to go to the page containing a question they have not answered. The
code that creates the LinkButton is called, as well as the AddHandler line
(I ran a Debug and saw that it executes this code, and the links are
displayed on the page afterwards). However, the eventhandler is not called
when the LinkButton is clicked. Here is the code that dynamically generates
the LinkButtons as well as the eventhandler I want to be called:
'The code that generates the LinkButtons:
Private Sub btnSubmit_Click(ByVal sender As Object, ByVal e As
System.Web.UI.ImageClickEventArgs) Handles btnSubmit.Click
Me.submitanswers()
If Me.completetest() Then
'Code that I use to update my database, not involved in this
problem because Me.completetest() returns false
Else
Me.lblAnswered.Text = String.Format("{0} of 75 Questions
Answered (Not Answered: ", CStr(Me.questionsanswered()))
Dim myconnection As New
SqlConnection(System.Configuration.ConfigurationManager.AppSettings("connectionstring"))
Dim cmd As New SqlCommand("", myconnection)
Dim qnumreader As SqlDataReader
Dim insertafter As Integer =
Me.Form.Controls.IndexOf(Me.lblAnswered) + 1
For i As Integer = 1 To 75
cmd.CommandText = String.Format("SELECT
questions.questionnumber FROM useranswers INNER JOIN questions ON
useranswers.questionid=questions.questionid WHERE
questions.questionnumber={0} AND useranswers.testid={1} AND
useranswers.userid={2}", i, CStr(Session("testid")),
CStr(Session("userid")))
myconnection.Open()
qnumreader = cmd.ExecuteReader()
If Not qnumreader.Read() Then
Dim questionlink As New LinkButton()
questionlink.CausesValidation = False
questionlink.CommandArgument = i
questionlink.CssClass = "answerRED"
questionlink.EnableViewState = False
questionlink.Text = i & " "
AddHandler questionlink.Command, AddressOf
Me.QuestionLinkCommand
Me.Form.Controls.AddAt(insertafter, questionlink)
insertafter += 1
End If
myconnection.Close()
Next
Me.lblCloseParen.Visible = True
End If
End Sub
'The eventhandler I want to use:
Private Sub QuestionLinkCommand(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.CommandEventArgs)
Dim myconnection As New
SqlConnection(System.Configuration.ConfigurationManager.AppSettings("connectionstring"))
Dim cmd As New SqlCommand("SELECT subgroupid FROM questions WHERE
questionnumber=" & e.CommandArgument, myconnection)
myconnection.Open()
Session("subgroupid") = CInt(cmd.ExecuteScalar())
myconnection.Close()
End Sub
When I run my Application, the code in btnSubmit_Click works as I expect (or
at least it looks like it did), but when I click the dynamically created
LinkButtons (variable name questionlink), they postback but do not trigger
the QuestionLinkCommand method (the QuestionLinkCommand never gets executed
when I do a debug session). I cannot figure out why they are not triggering
this eventhandler, because I use AddHandler statements when creating the
LinkButtons, and the eventhandler has the correct signature. Is there
something I am doing wrong? Thanks.
the user to go to the page containing a question they have not answered. The
code that creates the LinkButton is called, as well as the AddHandler line
(I ran a Debug and saw that it executes this code, and the links are
displayed on the page afterwards). However, the eventhandler is not called
when the LinkButton is clicked. Here is the code that dynamically generates
the LinkButtons as well as the eventhandler I want to be called:
'The code that generates the LinkButtons:
Private Sub btnSubmit_Click(ByVal sender As Object, ByVal e As
System.Web.UI.ImageClickEventArgs) Handles btnSubmit.Click
Me.submitanswers()
If Me.completetest() Then
'Code that I use to update my database, not involved in this
problem because Me.completetest() returns false
Else
Me.lblAnswered.Text = String.Format("{0} of 75 Questions
Answered (Not Answered: ", CStr(Me.questionsanswered()))
Dim myconnection As New
SqlConnection(System.Configuration.ConfigurationManager.AppSettings("connectionstring"))
Dim cmd As New SqlCommand("", myconnection)
Dim qnumreader As SqlDataReader
Dim insertafter As Integer =
Me.Form.Controls.IndexOf(Me.lblAnswered) + 1
For i As Integer = 1 To 75
cmd.CommandText = String.Format("SELECT
questions.questionnumber FROM useranswers INNER JOIN questions ON
useranswers.questionid=questions.questionid WHERE
questions.questionnumber={0} AND useranswers.testid={1} AND
useranswers.userid={2}", i, CStr(Session("testid")),
CStr(Session("userid")))
myconnection.Open()
qnumreader = cmd.ExecuteReader()
If Not qnumreader.Read() Then
Dim questionlink As New LinkButton()
questionlink.CausesValidation = False
questionlink.CommandArgument = i
questionlink.CssClass = "answerRED"
questionlink.EnableViewState = False
questionlink.Text = i & " "
AddHandler questionlink.Command, AddressOf
Me.QuestionLinkCommand
Me.Form.Controls.AddAt(insertafter, questionlink)
insertafter += 1
End If
myconnection.Close()
Next
Me.lblCloseParen.Visible = True
End If
End Sub
'The eventhandler I want to use:
Private Sub QuestionLinkCommand(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.CommandEventArgs)
Dim myconnection As New
SqlConnection(System.Configuration.ConfigurationManager.AppSettings("connectionstring"))
Dim cmd As New SqlCommand("SELECT subgroupid FROM questions WHERE
questionnumber=" & e.CommandArgument, myconnection)
myconnection.Open()
Session("subgroupid") = CInt(cmd.ExecuteScalar())
myconnection.Close()
End Sub
When I run my Application, the code in btnSubmit_Click works as I expect (or
at least it looks like it did), but when I click the dynamically created
LinkButtons (variable name questionlink), they postback but do not trigger
the QuestionLinkCommand method (the QuestionLinkCommand never gets executed
when I do a debug session). I cannot figure out why they are not triggering
this eventhandler, because I use AddHandler statements when creating the
LinkButtons, and the eventhandler has the correct signature. Is there
something I am doing wrong? Thanks.