Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2011-03-08
2
Medium Priority
?
225 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 2000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

722 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