Dynamic control and onClick event Server-side

R

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"
 
A

Alex

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) {
...
}
 
R

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.
 
A

Alex

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
 
R

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
 
G

Guest

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 said:
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
 

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,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top