asp.net dynamically created button not firing onClick event

R

rsaffy

I am having trouble with my dynamically created button's event
handling. I read that the buttons need to be recreated on every trip
to the server, but how exactly do you do that when the datagrid the
button is added to is created at run time?

here is code from my aspx page...
-------------------------------------------------------------------------------------
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Search Page</title>
<script runat="server">

Sub GreetingBtn_Click(ByVal sender As Object, _
ByVal e As EventArgs)

' When the button is clicked,
' change the button text, and disable it.

Dim clickedButton As Button = sender
clickedButton.Text = "...button clicked..."
clickedButton.Enabled = False

' Display the greeting label text.
'GreetingLabel.Visible = True
End Sub
</script>
</head>

here is code where I generate my datagrid...
-------------------------------------------------------------------------------------
Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnSearch.Click
Dim sSql As String
Dim nReturn As Integer
Dim sReturnMessage As String

Dim b As New SAT_BIZ_FolderSearch
Dim d As New SAT_DATA.DataObject

Dim dt As New Data.DataTable

' need to search...
sSql = ""
sReturnMessage = ""
nReturn = b.BuildCustSearchSql(sSql,
radioSearchType.SelectedItem.Text, txtSearch.Text)
If nReturn <> 0 Then
lblMessageArea.Text = nReturn & ": " & sReturnMessage
Else
pPlaceHolder.Controls.Clear()

lblMessageArea.Text = sSql

d.SetConnectionString(g_sConnString)

dt = d.GetDataTable(sSql, nReturn, sReturnMessage)
If nReturn <> 0 Then
lblMessageArea.Text = lblMessageArea.Text & nReturn &
": " & sReturnMessage
End If

datagridSearchResults.DataSource = dt
datagridSearchResults.DataBind()

End If
b = Nothing
d = Nothing
dt = Nothing
End Sub

here is code where I generate the row contents for my datagrid...
-------------------------------------------------------------------------------------
Private Sub datagridSearchResults_ItemDataBound(ByVal sender As
Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs)
Handles datagridSearchResults.ItemDataBound
Dim i As Integer
Dim btnFilePath As Button

Select Case e.Item.ItemType()
Case ListItemType.Header
' FORMAT THE DATAGRID HEADER HERE..
For i = 0 To e.Item.Cells.Count - 1


arrGridFieldName.SetValue(e.Item.Cells(i).Text.ToString, i)

Select Case e.Item.Cells(i).Text.ToString
Case "COMPANY_NAME"
arrGridColumnHeader.SetValue("Company
Name", i)
Case "CUST_ACCT_NBR"
arrGridColumnHeader.SetValue("Customer
Account", i)
Case "PROSPECT_ID"
arrGridColumnHeader.SetValue("Prospect ID",
i)
Case "PROPOSAL_ID"
arrGridColumnHeader.SetValue("Proposal ID",
i)
Case "BTN"
arrGridColumnHeader.SetValue("BTN", i)
Case "CITY_NAME"
arrGridColumnHeader.SetValue("City", i)
Case "STATE_CODE"
arrGridColumnHeader.SetValue("State", i)
Case "CUST_PATH"
arrGridColumnHeader.SetValue("Customer
Folder", i)
Case "SALES_PATH"
arrGridColumnHeader.SetValue("Sales
Folder", i)
Case Else

arrGridColumnHeader.SetValue(e.Item.Cells(i).Text, i)
End Select

' replace the oracle field name with what we just
put in the array...
e.Item.Cells(i).Text =
arrGridColumnHeader(i).ToString
e.Item.BackColor = Drawing.Color.HotPink

Next

Case ListItemType.Item, ListItemType.AlternatingItem

' FORMAT THE DATAGRID DETAILS HERE..
For i = 0 To e.Item.Cells.Count - 1

If arrGridFieldName(i).ToString() = "CUST_PATH"
Then
If e.Item.Cells(i).Text() = "0" Then
e.Item.Cells(i).Text() = ""
Else
btnFilePath = New Button
btnFilePath.Text = "OPEN"
btnFilePath.ID = "GreetingBtn"
btnFilePath.CommandName = "OPEN_CUST_PATH"
btnFilePath.CommandArgument =
e.Item.Cells(i).Text()
btnFilePath.OnClientClick = "return
confirm('Ready to Submit');"
e.Item.Cells(i).Controls.Add(btnFilePath)
End If
End If

Next

End Select

End Sub
 
R

rsaffy

here is what I was starting with...

How to: Respond to Button Web Server Control Events in Client Script

To add client script, which handles the OnClientClick event, to a
button Web server control
In the ASP.NET button Web server control to which you want to add
client script (a Button, LinkButton, or ImageButton control), set the
OnClientClick property to the client script that you want to run.

Note
If you want to be able to cancel the submission, set the OnClientClick
property to the string "Return" and the function name. The client
script can then cancel the submission by returning false.


The following code example shows how to add a client-script click event
to a Button control.

Visual Basic Copy Code
<%@ Page Language="VB" %>
<script runat="server">
Sub Button1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs)
Label1.Text = "Server click handler called."
End Sub
</script>

<body>
<form id="form1" runat="server">
<asp:Button ID="Button1" Runat="server"
OnClick="Button1_Click"
OnClientClick="return confirm('Ready to submit.')"
Text="Test Client Click" />
<br />
<asp:Label ID="Label1" Runat="server" text="" />
</form>
</body>
</html>
 
T

tfsmag

since it's dynamically generated you'll probably need to add the
onclick attribute to the button..

btnSearch.Attributes.Add("OnClick","btnSearch_Click")?

just off the top of my head, haven't tested.
 
R

rsaffy

oops, thanks tfsmag. I was testing and replaced that line, it should
be

btnFilePath.OnClientClick = "GreetingBtn_Click"

instead of...

btnFilePath.OnClientClick = "return confirm('Ready to Submit');"
 
M

Marina Levit [MVP]

You are adding a client side click handler. That's why the property is
called 'OnClientClick'.

You are looking for a server side event handler. Use AddHandler to add the
handler to your method.
 
T

tfsmag

the onclientclick will not fire the event, you need to add the click
event to the onclick attribute. onclient click is mainly for returning
alerts and confirmations to the client if i'm not mistaken...
 
R

rsaffy

ok, I tried that. The Click event still doesn't fire... its got to be
about the bit I read regarding dynamically created controls need to be
recreated before any event handling??? ...as a side note, my buttons
"disappear" once I click one of them.

btnFilePath.EnableViewState = True
AddHandler btnFilePath.Click, AddressOf GreetingBtn_Click

I will work on rebuilding my buttons on every trip to the server and
see if that helps.

my aspx.vb code now looks like this.......................
................................................................
Private Sub datagridSearchResults_ItemDataBound(ByVal sender As
Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs)
Handles datagridSearchResults.ItemDataBound
Dim i As Integer
Dim btnFilePath As Button

Select Case e.Item.ItemType()
Case ListItemType.Header
' FORMAT THE DATAGRID HEADER HERE..
For i = 0 To e.Item.Cells.Count - 1


arrGridFieldName.SetValue(e.Item.Cells(i).Text.ToString, i)

Select Case e.Item.Cells(i).Text.ToString
Case "COMPANY_NAME"
arrGridColumnHeader.SetValue("Company
Name", i)
Case "CUST_ACCT_NBR"
arrGridColumnHeader.SetValue("Customer
Account", i)
Case "PROSPECT_ID"
arrGridColumnHeader.SetValue("Prospect ID",
i)
Case "PROPOSAL_ID"
arrGridColumnHeader.SetValue("Proposal ID",
i)
Case "BTN"
arrGridColumnHeader.SetValue("BTN", i)
Case "CITY_NAME"
arrGridColumnHeader.SetValue("City", i)
Case "STATE_CODE"
arrGridColumnHeader.SetValue("State", i)
Case "CUST_PATH"
arrGridColumnHeader.SetValue("Customer
Folder", i)
Case "SALES_PATH"
arrGridColumnHeader.SetValue("Sales
Folder", i)
Case Else

arrGridColumnHeader.SetValue(e.Item.Cells(i).Text, i)
End Select

' replace the oracle field name with what we just
put in the array...
e.Item.Cells(i).Text =
arrGridColumnHeader(i).ToString
e.Item.BackColor = Drawing.Color.HotPink

Next

Case ListItemType.Item, ListItemType.AlternatingItem

' FORMAT THE DATAGRID DETAILS HERE..
For i = 0 To e.Item.Cells.Count - 1

If arrGridFieldName(i).ToString() = "CUST_PATH"
Then
If e.Item.Cells(i).Text() = "0" Then
e.Item.Cells(i).Text() = ""
Else
btnFilePath = New Button
btnFilePath.Text = "OPEN"
btnFilePath.ID = "GreetingBtn"
btnFilePath.CommandName = "OPEN_CUST_PATH"
btnFilePath.CommandArgument =
e.Item.Cells(i).Text()
btnFilePath.EnableViewState = True
AddHandler btnFilePath.Click, AddressOf
GreetingBtn_Click
' I have tried all of these below, with no
luck.............
' btnFilePath.OnClientClick = "return
confirm('Ready to Submit');"
' btnFilePath.OnClientClick =
"GreetingBtn_Click"
' btnFilePath.Attributes.Add("onClick",
"javascript:alert('woo')")
e.Item.Cells(i).Controls.Add(btnFilePath)
End If
End If

Next

End Select

End Sub


Sub GreetingBtn_Click(ByVal sender As Object, _
ByVal e As EventArgs)

' When the button is clicked,
' change the button text, and disable it.

Dim clickedButton As Button = sender
clickedButton.Text = "...button clicked..."
clickedButton.Enabled = False

End Sub
................................................................
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top