PDA

View Full Version : Event not firing


hamaswa
03-27-2009, 04:08 AM
Hi I have created linkbuttons dynamically using the below code


Sub createlinkbutton(ByVal commandtext As String, ByVal Cmdarg As String, ByVal pane As Panel, ByVal count As Int32)

Dim i = 0

Dim panel1 As Panel = New Panel

panel1.CssClass = "boxx"

pane.Controls.Add(panel1)

Dim lbButton As LinkButton = New LinkButton

lbButton.Text = commandtext

lbButton.CommandName = commandtext

lbButton.CommandArgument = Cmdarg

AddHandler lbButton.Command, AddressOf Me.LinkButton_Command

panel1.Controls.Add(lbButton)

End Sub



Protected Sub LinkButton_Command(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.CommandEventArgs)

If e.CommandArgument.ToString = "Region" Then

Session("Region") = e.CommandName

End If

If e.CommandArgument.ToString = "LEA" Then

Session("LEA") = e.CommandName

End If

If e.CommandArgument.ToString = "Subject" Then

Session("Subject") = e.CommandName

End If

If e.CommandArgument.ToString = "Position" Then

Session("Position") = e.CommandName

End If

If e.CommandArgument.ToString = "SchoolPhase" Then

Session("SchoolPhase") = e.CommandName

End If

Response.Write("<script language=""javascript"">alert(""" & Session("Region") & """)</script>")

populate()

End Sub


Linkbuttons are successfully generated. When i click one of the linkbutton. The command event doesn't fired. While I have Add command event handler to the linkbuttons.

What is wrong in my code

Thanks

mattyw87
07-28-2009, 12:53 AM
Things work a little differently regarding firing events when you've created the controls dynamically.

I'm not a VB guy (only C# with ASP.net) so can't really post any meaningful code to you but can point to the website which helped me solve this exact problem. When you create a control dynamically you need to implement the IPostBackEventHandler class and provide a post back reference to wire up the event at runtime. Like I say - C# code will probably be no use to you (let me know if it is and I'll post some) Have a look here in the meantime - this has everything you need:

http://www.code-magazine.com/Article.aspx?quickid=0511051&bcsi_scan_135EC23C19E53651=VDmGrJ51htwk3xtaXMfUAiI AAADKseUI&bcsi_scan_filename=Article.aspx (see the "Events" section)