473,406 Members | 2,705 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,406 software developers and data experts.

Dynamic control and onClick event Server-side

RA
I have created a button dynamically; which has been added to a TableCell of
a TableRow of a Table control.

Is there a way to add onclick event which calls a procedure on the
Server-side itself.
Any suggestions?

I tried with
btnAdd.Attributes.Add("onClick","AddItem();")
Public Sub AddItem()
'
dim Test as String
Test = "Testing OnClcik"
End Sub

I get "Object Expected" error.

I also tried
btnAdd.Attributes.Add("onClick","AddItem;")
Error message is " 'AddItem' is undefined"
Nov 18 '05 #1
5 19634
As you create the button dynamically, the asp:button has an OnCommand
eventhandler, set that to be some event name, like Button_OnClick. Create a
function in your code behind, Button_OnClick, and this should help out.

For example...
Page...
<asp:Button id="Button1"
Text="Sort Ascending"
CommandName="Sort"
CommandArgument="Ascending"
OnCommand="CommandBtn_Click"
runat="server"/>

Page behind...
void CommandBtn_Click(Object sender, CommandEventArgs e) {
...
}

--
Alex
Nov 18 '05 #2
RA
This seems code on client-side. I want to write a procedure(AddItem) in
webform1.aspx.vb and want onclick event to call that procedure(AddItem).

I am not much familiar with Javascript.
For this procedure I have to go through table's each row find the ID cell
and then add/update information in dataset and subsequently in SQL Server
table.
"Alex" <al**@nospam.net> wrote in message
news:uT**************@TK2MSFTNGP10.phx.gbl...
As you create the button dynamically, the asp:button has an OnCommand
eventhandler, set that to be some event name, like Button_OnClick. Create
a
function in your code behind, Button_OnClick, and this should help out.

For example...
Page...
<asp:Button id="Button1"
Text="Sort Ascending"
CommandName="Sort"
CommandArgument="Ascending"
OnCommand="CommandBtn_Click"
runat="server"/>

Page behind...
void CommandBtn_Click(Object sender, CommandEventArgs e) {
...
}

--
Alex

Nov 18 '05 #3
Sorry, I am not sure what you mean. The code I gave you is what you need for
your client side page's control to post back to the code behind. You'll
notice the asp:Button is runat=server, so it will post back to your
page.ascx.vb, where you can write your OnCommand eventhandler,
CommandBtn_Click. When you specify the runat=server, your controls will
trigger the code behind.

This was not javascript at all. The code behind had C# signatures, but you
can change them to VB. If you need more clarification, go here...
ms-help://MS.VSCC.2003/MS.MSDNQTR.2003JUL.1033/cpref/html/frlrfSystemWebUIWe
bControlsButtonClassOnCommandTopic.htm

--
Alex Mueller
Nov 18 '05 #4
RA
This is what I am doing.

Private Sub PopulateTable()
Dim tRow as New TableRow
Dim tCell as new TableCell

Dim btnAdd As New Button
With btnAdd
.ID = "AddLink"
.Enabled = False
.Font.Bold = True
.Font.Underline = True
.ForeColor = System.Drawing.Color.Blue
.BackColor = System.Drawing.Color.Transparent
.BorderStyle = BorderStyle.None
.Attributes("onClick") = "AddRCode();"
End With

tCell.Controls.Add(btnAdd)
tRow.Cells.Add(tCell)
TableTest.Rows.Add(tRow)
' Also have code to add more data.
End Sub

Public Sub AddItem()
'
dim Test as String
Test = "Testing OnClcik"
End Sub

"Alex" <al**@nospam.net> wrote in message
news:en**************@TK2MSFTNGP11.phx.gbl...
Sorry, I am not sure what you mean. The code I gave you is what you need
for
your client side page's control to post back to the code behind. You'll
notice the asp:Button is runat=server, so it will post back to your
page.ascx.vb, where you can write your OnCommand eventhandler,
CommandBtn_Click. When you specify the runat=server, your controls will
trigger the code behind.

This was not javascript at all. The code behind had C# signatures, but you
can change them to VB. If you need more clarification, go here...
ms-help://MS.VSCC.2003/MS.MSDNQTR.2003JUL.1033/cpref/html/frlrfSystemWebUIWe
bControlsButtonClassOnCommandTopic.htm

--
Alex Mueller

Nov 18 '05 #5
Hello RA,

I just saw your post and I know that this may be a little bit late. The way
you add an event handler to the dynamically created control is as follows:

1. First create a procedure with the exact signature as an event handler
2. After adding the dynamic control, bind this procedure to the dynamic
control

Note: You can have multiple dynamic controls bind to the same event handler.

As an example:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim btnAdd as New Button
Dim bSomeCondition as Boolean = False

If bSomeCondition = True Then
'add the new button to the form
With btnAdd
.ID = "btnAdd"
.Text = "Add"
'any other attributes
End With
WebForm1.Controls.Add btnAdd

'bind this control to an event handler
AddHandler btnAdd.Click, AddressOf btnAddHandler
End If
'code for the handler
Public Sub btnAddHandler(ByVal sender As System.Object, ByVal e As
System.EventArgs)

' your code goes here

End Sub

Hope this helps.
End Sub

"RA" wrote:
This is what I am doing.

Private Sub PopulateTable()
Dim tRow as New TableRow
Dim tCell as new TableCell

Dim btnAdd As New Button
With btnAdd
.ID = "AddLink"
.Enabled = False
.Font.Bold = True
.Font.Underline = True
.ForeColor = System.Drawing.Color.Blue
.BackColor = System.Drawing.Color.Transparent
.BorderStyle = BorderStyle.None
.Attributes("onClick") = "AddRCode();"
End With

tCell.Controls.Add(btnAdd)
tRow.Cells.Add(tCell)
TableTest.Rows.Add(tRow)
' Also have code to add more data.
End Sub

Public Sub AddItem()
'
dim Test as String
Test = "Testing OnClcik"
End Sub

"Alex" <al**@nospam.net> wrote in message
news:en**************@TK2MSFTNGP11.phx.gbl...
Sorry, I am not sure what you mean. The code I gave you is what you need
for
your client side page's control to post back to the code behind. You'll
notice the asp:Button is runat=server, so it will post back to your
page.ascx.vb, where you can write your OnCommand eventhandler,
CommandBtn_Click. When you specify the runat=server, your controls will
trigger the code behind.

This was not javascript at all. The code behind had C# signatures, but you
can change them to VB. If you need more clarification, go here...
ms-help://MS.VSCC.2003/MS.MSDNQTR.2003JUL.1033/cpref/html/frlrfSystemWebUIWe
bControlsButtonClassOnCommandTopic.htm

--
Alex Mueller


Nov 19 '05 #6

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

Similar topics

8
by: Ashish Shridharan | last post by:
Hi All I have been trying to add a control to the header cell of a datagrid on my ASP.NET page. These controls are defined in the HTML as ASP.NET web controls. They are being added into the...
6
by: Tom Rowton | last post by:
This one has me a bit confused and I'm not finding what I need in the MSDN or by searching these forums, so here goes... I have a rather large, complex code-in-page WebForm (don't ask) and a...
1
by: | last post by:
Hi Guys, I am generating dynamic server controls using XML and XSLT. With this I am generating two buttons which when clicked redirect the page to a different page. Here is my "checkout"...
7
by: moondaddy | last post by:
I have a user control being used instead of a frame page. when the user clicks on a menu item I need to send the ID (integer value) of that menu as a parameter in the postback of the user control...
6
by: Steve Caliendo | last post by:
Hi, I'm creating 5 ImageButton controls in the panel control, and I have a unique ID specified for each one. When I click on any one of them, the Page_Load executes (Of course), but how do I...
0
by: RSB | last post by:
Hi Every one, i am trying to create a UserControl and i am passing a Array of strings to it. Now based on the Array elements i am creating the LinkButtons Dynamically. I am also passing a Event to...
1
by: Gerald Baeck | last post by:
I want to realize a dynamic hyperlink, which changes onclick it's text and it's navigateurl. Unfortunately there is no event for a hyperlink like onclick for buttons. Is there another way to...
5
by: vcuankitdotnet | last post by:
Hi. I have tried to search for this online, but can't find what I'm looking for so I decided to come here. I have a ASP.NET 2.0 website with a masterpage. My .aspx page uses the masterpage. Here is...
2
by: ChrisCicc | last post by:
Hi All, I got a real doozy here. I have read hundreds upon hundreds of forum posts and found numerous others who have replicated this problem, but have yet to find a solution. Through testing I have...
13
by: tommymo | last post by:
Hi everyone I'm new to this site and the world of ASP.Net C# programming. I have been learning controls and integrating them with a SQL database. So far I have been able to move along and understand...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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.