473,405 Members | 2,187 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

asp.net dynamically created button not firing onClick event

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.ToS tring, 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

-------------------------------------------------------------------------------------
Wat am I missing?

Jul 20 '06 #1
7 3545
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>

Jul 20 '06 #2
since it's dynamically generated you'll probably need to add the
onclick attribute to the button..

btnSearch.Attributes.Add("OnClick","btnSearch_Clic k")?

just off the top of my head, haven't tested.
rs****@nuvox.com wrote:
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.ToS tring, 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

-------------------------------------------------------------------------------------
Wat am I missing?
Jul 20 '06 #3
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');"

Jul 20 '06 #4
....in either case the Click event does not fire.

Jul 20 '06 #5
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...
rs****@nuvox.com wrote:
...in either case the Click event does not fire.
Jul 20 '06 #6
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.

<rs****@nuvox.comwrote in message
news:11*********************@s13g2000cwa.googlegro ups.com...
>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.ToS tring, 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

-------------------------------------------------------------------------------------
Wat am I missing?

Jul 20 '06 #7
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.ToS tring, 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
.................................................. ..............

Jul 20 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: RobG | last post by:
I am trying to dynamically add an onclick to an element, however I just can't get the syntax right. consider the following function: function doClick (evt,x) { // do things with evt and x } ...
14
by: tshad | last post by:
I posted this on the asp.net group, also. I wasn't sure whether this was an asp.net problem or a javascript problem. I have a page that was originally created from a program I found on the net...
5
by: MS Newsgroups | last post by:
Hi, I have a scenario where I am dynamically adding a control from code when a controls event is fired. The problem I have is that when the newly created control is clicked, the click event does...
1
by: Earl Teigrob | last post by:
PROBLEM: When a user control is loaded into a PlaceHolder control more than once, the events do not fire on the first click of a control on the dynamically loaded user control. In other words, the...
2
by: R Duke | last post by:
I have tried everything I can think of to change the visible property of a design time created control from a dynamically created control's command event handler. Here is the scenario. I have...
1
by: AndrewMBaldwin | last post by:
Ok, this is going to be a long post, so I apologize in advance, but if it was an easy question I would have probably found an answer somewhere out here by now... The short story of this is that...
2
by: showens | last post by:
I have a table that has rows appended dynamically, based on the item selected from a dropdownlist. The rows have a number of cells, one with a literal control using the text of the dropdownlist,...
3
by: Jay | last post by:
I am on the 2.0 framework and have run the c:\windows\microsoft.net \framework\v1.1.4322\aspnet_regiis.exe -c and had no success. About half of the buttons on my webforms are firing and the other...
1
by: johnjsforum | last post by:
Buddies, I have a web page to create HTML buttons dynamically as in the “formDivColorPicker” function ////////////////////////////////////////////////////////////////////////////////////...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.