Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > AddHandler not working

Reply
Thread Tools

AddHandler not working

 
 
Nathan Sokalski
Guest
Posts: n/a
 
      10-02-2007
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
http://www.velocityreviews.com/forums/(E-Mail Removed)
http://www.nathansokalski.com/


 
Reply With Quote
 
 
 
 
Charlie
Guest
Posts: n/a
 
      10-02-2007
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
> (E-Mail Removed)
> http://www.nathansokalski.com/
>
>
>

 
Reply With Quote
 
 
 
 
Nathan Sokalski
Guest
Posts: n/a
 
      10-02-2007
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
(E-Mail Removed)
http://www.nathansokalski.com/

"Charlie" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>> (E-Mail Removed)
>> http://www.nathansokalski.com/
>>
>>
>>



 
Reply With Quote
 
Charlie
Guest
Posts: n/a
 
      10-02-2007
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
> (E-Mail Removed)
> http://www.nathansokalski.com/
>
> "Charlie" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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
> >> (E-Mail Removed)
> >> http://www.nathansokalski.com/
> >>
> >>
> >>

>
>
>

 
Reply With Quote
 
Cor Ligthert[MVP]
Guest
Posts: n/a
 
      10-02-2007
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

 
Reply With Quote
 
Cor Ligthert[MVP]
Guest
Posts: n/a
 
      10-02-2007
Sorry,

http://www.vb-tips.com/DynamicControl.aspx
 
Reply With Quote
 
rowe_newsgroups
Guest
Posts: n/a
 
      10-02-2007
On Oct 1, 9:38 pm, "Nathan Sokalski" <(E-Mail Removed)> 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
> (E-Mail Removed)://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

 
Reply With Quote
 
Andrew Morton
Guest
Posts: n/a
 
      10-02-2007
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


 
Reply With Quote
 
bruce barker
Guest
Posts: n/a
 
      10-02-2007
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.

 
Reply With Quote
 
Nathan Sokalski
Guest
Posts: n/a
 
      10-02-2007
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
(E-Mail Removed)
http://www.nathansokalski.com/

"Andrew Morton" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
AddHandler not working Nathan Sokalski ASP .Net 15 10-03-2007 11:00 AM
AddHandler for CheckBox is not working RA ASP .Net 2 11-15-2004 10:24 PM
ImageButton addhandler not firing ! Tee ASP .Net 1 02-18-2004 04:00 PM
Re: Help w/AddHandler (Not Firing Off) Natty Gur ASP .Net 1 08-11-2003 03:06 PM
Help w/AddHandler (Not Firing Off) Jeffrey A. Voigt ASP .Net 3 08-11-2003 04:53 AM



Advertisments