473,383 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

AddHandler not working

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.ConfigurationMa nager.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.ConfigurationMa nager.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.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/
Oct 2 '07 #1
15 2022
Try changing the line

Dim questionlink As New LinkButton()

to

Dim WithEvents questionlink As New LinkButton()

Does that work?
"Nathan Sokalski" wrote:
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.ConfigurationMa nager.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.ConfigurationMa nager.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.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/
Oct 2 '07 #2
No, if I add the WithEvents keyword I recieve the message:

'WithEvents' is not valid on a local variable declaration.

That was a good suggestion (when I read your reply I thought that might be
it), but unfortunately it wasn't. Any other ideas? Thanks.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

"Charlie" <c2****@yahoo.comwrote in message
news:43**********************************@microsof t.com...
Try changing the line

Dim questionlink As New LinkButton()

to

Dim WithEvents questionlink As New LinkButton()

Does that work?
"Nathan Sokalski" wrote:
>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.ConfigurationM anager.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.ConfigurationM anager.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.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

Oct 2 '07 #3
Put the declaration of questionlink outside of the btnSubmit_Click code
block, in the general declarations area...

public withevents questionlink as New LinkButton()
inside your codeblock, remove the dim questionlink line.

you could also scope the linkbutton as public or friend.

does that work?

"Nathan Sokalski" wrote:
No, if I add the WithEvents keyword I recieve the message:

'WithEvents' is not valid on a local variable declaration.

That was a good suggestion (when I read your reply I thought that might be
it), but unfortunately it wasn't. Any other ideas? Thanks.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

"Charlie" <c2****@yahoo.comwrote in message
news:43**********************************@microsof t.com...
Try changing the line

Dim questionlink As New LinkButton()

to

Dim WithEvents questionlink As New LinkButton()

Does that work?
"Nathan Sokalski" wrote:
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.ConfigurationMa nager.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.ConfigurationMa nager.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.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/


Oct 2 '07 #4
Nathan,

Here is a simple sample of dynamicly creating textboxes with events.

Will you be so kind if you send next time problems to these newsgroup to
show a simple sample instead of almost a complete sample. We want to help
you, however not doing a daytime job by investigating parts which are not
relevant to your question.

Cor

Oct 2 '07 #5
On Oct 1, 9:38 pm, "Nathan Sokalski" <njsokal...@hotmail.comwrote:
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.ConfigurationMa nager.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.ConfigurationMa nager.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.
--
Nathan Sokalski
njsokal...@hotmail.comhttp://www.nathansokalski.com/
You apparently aren't to familiar with the programming modal of
ASP.NET? You need to use AddHandler to add the event listener before
the child control's event are handled (like in the Load event of the
Page), otherwise the dynamic event won't exist when it's time to
process it.

By the way, if you search the newsgroups before posting you could find
the answer on your own....

http://groups.google.com/group/micro...rch+this+group

Thanks,

Seth Rowe

Oct 2 '07 #7
Nathan Sokalski wrote:
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:
<snip>

You have to tell it to AutoPostBack. Incidentally, you can use "With" to
save a bit of typing:-
If Not qnumreader.Read() Then
Dim questionlink As New LinkButton()
With questionlink
.CausesValidation = False
.CommandArgument = i
.CssClass = "answerRED"
.EnableViewState = False
.Text = i & "&nbsp;"
.AutoPostBack = True
End With
AddHandler questionlink.Command, AddressOf
Me.QuestionLinkCommand
Me.Form.Controls.AddAt(insertafter, questionlink)
insertafter += 1
End If
HTH

Andrew
Oct 2 '07 #8
This is a LinkButton, it does not need or have an AutoPostBack property. And
take note that in my original post I stated that it does do a PostBack, it
just doesn't trigger the eventhandler I attempted to assign it.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

"Andrew Morton" <ak*@in-press.co.uk.invalidwrote in message
news:u3**************@TK2MSFTNGP03.phx.gbl...
Nathan Sokalski wrote:
>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:

<snip>

You have to tell it to AutoPostBack. Incidentally, you can use "With" to
save a bit of typing:-
> If Not qnumreader.Read() Then
Dim questionlink As New LinkButton()
With questionlink
.CausesValidation = False
.CommandArgument = i
.CssClass = "answerRED"
.EnableViewState = False
.Text = i & "&nbsp;"
.AutoPostBack = True
End With
> AddHandler questionlink.Command, AddressOf
Me.QuestionLinkCommand
Me.Form.Controls.AddAt(insertafter, questionlink)
insertafter += 1
End If

HTH

Andrew

Oct 2 '07 #9
asp.net pages are stateless. you rendered the linkbuton and added to
handler on the click event. but when the user links on th button and the
asp.net is run again, the page does create th linkbutton and add the
handler, so the event is ignored.

your code needs to remember it added the linkbutton and handler during
previous postback and recreate the linkbutton and handler during the
oninit event. you can save your state in session or viewstate.

-- bruce (sqlwork.com)

Nathan Sokalski wrote:
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.ConfigurationMa nager.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 & "&nbsp;"
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.ConfigurationMa nager.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.
Oct 2 '07 #10
The only way I can come up with to run this code in the Load event is to
create a Hidden control and have btnSubmit (the Button I am clicking to
cause the postback) assign it a value using JavaScript prior to doing the
postback. This would work, but I was hoping to not need to have extra
Controls for things like this. Is there a more efficient way, or am I stuck
with using an extra Hidden control? Thanks.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

"rowe_newsgroups" <ro********@yahoo.comwrote in message
news:11**********************@19g2000hsx.googlegro ups.com...
On Oct 1, 9:38 pm, "Nathan Sokalski" <njsokal...@hotmail.comwrote:
>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.ConfigurationM anager.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.ConfigurationM anager.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.
--
Nathan Sokalski
njsokal...@hotmail.comhttp://www.nathansokalski.com/

You apparently aren't to familiar with the programming modal of
ASP.NET? You need to use AddHandler to add the event listener before
the child control's event are handled (like in the Load event of the
Page), otherwise the dynamic event won't exist when it's time to
process it.

By the way, if you search the newsgroups before posting you could find
the answer on your own....

http://groups.google.com/group/micro...rch+this+group

Thanks,

Seth Rowe

Oct 2 '07 #11
On Oct 2, 11:55 am, "Nathan Sokalski" <njsokal...@hotmail.comwrote:
The only way I can come up with to run this code in the Load event is to
create a Hidden control and have btnSubmit (the Button I am clicking to
cause the postback) assign it a value using JavaScript prior to doing the
postback. This would work, but I was hoping to not need to have extra
Controls for things like this. Is there a more efficient way, or am I stuck
with using an extra Hidden control? Thanks.
--
Nathan Sokalski
njsokal...@hotmail.comhttp://www.nathansokalski.com/

"rowe_newsgroups" <rowe_em...@yahoo.comwrote in message

news:11**********************@19g2000hsx.googlegro ups.com...
On Oct 1, 9:38 pm, "Nathan Sokalski" <njsokal...@hotmail.comwrote:
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.ConfigurationMa nager.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.ConfigurationMa nager.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.
--
Nathan Sokalski
njsokal...@hotmail.comhttp://www.nathansokalski.com/
You apparently aren't to familiar with the programming modal of
ASP.NET? You need to use AddHandler to add the event listener before
the child control's event are handled (like in the Load event of the
Page), otherwise the dynamic event won't exist when it's time to
process it.
By the way, if you search the newsgroups before posting you could find
the answer on your own....
http://groups.google.com/group/micro...framework.aspn...
Thanks,
Seth Rowe
It seems to me after briefly reading your code segment you are only
creating one LinkButton. A simple approach would then be to add the
LinkButton at design time with it's Visible property set to false and
only show the LinkButton when it's necessary, and then hide it again
in it's event handler. Also, be sure to set EnableViewState to true to
preserve the Visible state on postbacks.

Something like:

///////////////
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click
Me.LinkButton1.Visible = True
End Sub

Protected Sub LinkButton1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles LinkButton1.Click
Try
'// My other code I want to run
Finally
Me.LinkButton1.Visible = False
End Try
End Sub
//////////////

P.S. It's easier on the responders if you just provide a short, easy
to read sample that demonstrates your problem. It's a pain to decipher
out the unrelated junk to just get to the problem.

Thanks,

Seth Rowe

Oct 2 '07 #12
It seems to me after briefly reading your code segment you are only
creating one LinkButton.
Nevermind, after rereading your post I now notice the For loop I
previously missed so disregard my post.
P.S. It's easier on the responders if you just provide a short, easy
to read sample that demonstrates your problem. It's a pain to decipher
out the unrelated junk to just get to the problem.
As you can now see, useless clutter leads to pointless responses.

Thanks,

Seth Rowe

Oct 2 '07 #13
No, I am not just creating one LinkButton. If you missed it, the code that
creates the LinkButton is inside the loop that starts with:

For i As Integer = 1 To 75

There may, in some cases, only be one LinkButton, but depending on the
specific case there could be anywhere from 1-75 LinkButtons. If there were
never going to be more than about 5, I would probably do what you said and
simply change the properties, but that is unfortunately not the case here.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

"rowe_newsgroups" <ro********@yahoo.comwrote in message
news:11*********************@n39g2000hsh.googlegro ups.com...
On Oct 2, 11:55 am, "Nathan Sokalski" <njsokal...@hotmail.comwrote:
>The only way I can come up with to run this code in the Load event is to
create a Hidden control and have btnSubmit (the Button I am clicking to
cause the postback) assign it a value using JavaScript prior to doing the
postback. This would work, but I was hoping to not need to have extra
Controls for things like this. Is there a more efficient way, or am I
stuck
with using an extra Hidden control? Thanks.
--
Nathan Sokalski
njsokal...@hotmail.comhttp://www.nathansokalski.com/

"rowe_newsgroups" <rowe_em...@yahoo.comwrote in message

news:11**********************@19g2000hsx.googlegr oups.com...
On Oct 1, 9:38 pm, "Nathan Sokalski" <njsokal...@hotmail.comwrote:
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.ConfigurationM anager.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.ConfigurationM anager.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.
--
Nathan Sokalski
njsokal...@hotmail.comhttp://www.nathansokalski.com/
You apparently aren't to familiar with the programming modal of
ASP.NET? You need to use AddHandler to add the event listener before
the child control's event are handled (like in the Load event of the
Page), otherwise the dynamic event won't exist when it's time to
process it.
By the way, if you search the newsgroups before posting you could find
the answer on your own....
>http://groups.google.com/group/micro...framework.aspn...
Thanks,
Seth Rowe

It seems to me after briefly reading your code segment you are only
creating one LinkButton. A simple approach would then be to add the
LinkButton at design time with it's Visible property set to false and
only show the LinkButton when it's necessary, and then hide it again
in it's event handler. Also, be sure to set EnableViewState to true to
preserve the Visible state on postbacks.

Something like:

///////////////
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click
Me.LinkButton1.Visible = True
End Sub

Protected Sub LinkButton1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles LinkButton1.Click
Try
'// My other code I want to run
Finally
Me.LinkButton1.Visible = False
End Try
End Sub
//////////////

P.S. It's easier on the responders if you just provide a short, easy
to read sample that demonstrates your problem. It's a pain to decipher
out the unrelated junk to just get to the problem.

Thanks,

Seth Rowe

Oct 2 '07 #14
Nathan Sokalski wrote:
This is a LinkButton, it does not need or have an AutoPostBack
property.
My mistake.
And take note that in my original post I stated that it
does do a PostBack, it just doesn't trigger the eventhandler I
attempted to assign it.
What errors are highlighted if you use Option Strict On? I ask because in
the handler you have
Dim cmd As New SqlCommand("SELECT subgroupid FROM questions
WHERE questionnumber=" & e.CommandArgument, myconnection)
which would give an error about not being able to use & with the
e.CommandArgument /object/, so there may be other errors you haven't
spotted.

Andrew
Oct 3 '07 #15
Nathan,

I gave you a link for a windowsform, because you were talking about a form
in my idea, but beside that there is as well a sample about a page.

This is a kind of calendar finding a date on a webpage.

http://www.vb-tips.com/ASPNetDynamicButton.aspx

Cor
Oct 3 '07 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: RA | last post by:
I have simpe code as follows. Private sub Page_load..... FillTable() End Sub Private Sub FillTable() '
2
by: Just Me | last post by:
When a document is to be printed I call a method that contains an AddHandler statement. I just realized that if a second copy is to be printed the method is called and the AddHandler is executed...
10
by: Rick Palmer | last post by:
I have an app I'm working on that will allow a user to run one of 5 reports. The report names are in a combobox on my form. I have a sub defined for each report that has the exact same name as is...
6
by: Carlo3030 | last post by:
Been reading Dino Esposito's Article "A DetailsView Control for ASP.NET 1.x" found in the msdn Library. His example was created using VB.NET. I am trying to convert the example (DetailsView...
3
by: Vemund Halvorsen | last post by:
Hi, Im having some trouble getting Addhandler to work. Using withevents everything is working fine. But with AddHandler I get no callback. The code below works fin if I declare _tcp as a...
5
by: Slim | last post by:
i have a simple page, with one button button1. when click it creates a new button button 2 and adds a event handler to it. but when button 2 is clicked nothing happens, why? Partial Class...
15
by: Nathan Sokalski | last post by:
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.