Solved

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

Posted on 2011-03-08
2
211 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
Comment Utility
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
Comment Utility
thks
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now