Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Screencast - Getting to Know the Pipeline

885 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