Solved

ASP.NET weird issue, AddHandler does not seem to work...

Posted on 2011-03-08
2
221 Views
Last Modified: 2012-05-11
Hi,

I am creating at runtime a linkbutton and need to add a "delete.click" event handler. However the debugger passes through the correct code, but the handler does not seem to be added. Cerainly when I clicked on the "delete" link button I do not trick the event.

 
lbtnDelete = New LinkButton()
        Dim strCommandArg As String = (((tblTable.Rows.Count) - 1) + 1).ToString
        lbtnDelete.ID = "_lbtnDelete" & strGuid
        lbtnDelete.Text = "NewDelete"
        lbtnDelete.CommandName = "Delete"
        lbtnDelete.CommandArgument = strCommandArg
        AddHandler lbtnDelete.Click, AddressOf _lbtnDeleteRow_Click
        rowNew.Cells(5).Controls.Add(lbtnDelete)

Open in new window


Where I create table rows from XML, this code works fine. It is just when I add a new row I have a problem.

Thoughts??? and thanks in advance for any help,

Sam
0
Comment
Question by:SamJolly
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 33

Accepted Solution

by:
Todd Gerbert earned 500 total points
ID: 35079707
Dynamically created controls need to be added at each post-back, in the Page_PreInit event, for example (http://msdn.microsoft.com/en-us/library/ms178472.aspx#lifecycle_events).

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
	Private Sub Page_PreInit(ByVal sender As Object, ByVal e As EventArgs)
		Dim lbtnDelete As New LinkButton
		lbtnDelete.Text = "Button Created at Runtime"
		
		AddHandler lbtnDelete.Click, AddressOf lbtnDelete_Click
		
		form1.Controls.Add(lbtnDelete)
	End Sub
	
	Private Sub lbtnDelete_Click(ByVal sender As Object, ByVal e As EventArgs)
		ClientScript.RegisterStartupScript(Me.GetType(), "lbtnDelete_ClientScript", "alert('lbtnDelete Clicked');", True)
	End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>

Open in new window


Also, you may wish to handle the Command event (http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.linkbutton.command.aspx).

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
	Private Sub Page_PreInit(ByVal sender As Object, ByVal e As EventArgs)
		Dim lbtnDelete As New LinkButton
		lbtnDelete.Text = "Button Created at Runtime"
		lbtnDelete.CommandName = "ShowMessage"
		lbtnDelete.CommandArgument = "Hello World"
		
		AddHandler lbtnDelete.Command, AddressOf lbtnDelete_Command
		form1.Controls.Add(lbtnDelete)
	End Sub
	
	Private Sub lbtnDelete_Command(ByVal sender As Object, ByVal e As CommandEventArgs)
		If e.CommandName = "ShowMessage" Then
			ClientScript.RegisterStartupScript(Me.GetType(), _
			 "ShowMessageScript", _
			 "alert('" & e.CommandArgument & "');", _
			 True)
		End If
	End Sub
	
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    </div>
    </form>
</body>
</html>

Open in new window

0
 

Author Closing Comment

by:SamJolly
ID: 35188449
thks
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

635 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question