Solved

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

Posted on 2011-03-08
2
213 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
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

809 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