473,405 Members | 2,185 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.

attach an event on a created button

i created a new button object. how do i attach an event
on it? thanx for your help.

Dim cc As ColorConverter = new ColorConverter()
Dim b = new button()
b.ID = "SelectedDate"

b.BorderStyle = 1
b.forecolor = cc.ConvertFromString("red")
b.BackColor = cc.ConvertFromString("white")
b.BorderColor = cc.ConvertFromString("white")
b.text = "hello"
b.click = Test

Sub Test
...do something
End Sub
Jul 19 '05 #1
3 4043
Hi,

Declare the instance as follows

Friend WitEvents b as Button

Then when you create the button, remeber to add it to the Controls
collection of the form

b = New Button
b.Text = "Hello"
b.Location = new Point(0,0)

Controls.Add( b )

Then the event handler should be declared as follows

Sub Test( ByVal sender As Object, ByVal e as EventArgs) Handles b.Click
MsgBox("Hey you clicked me!")
End Sub

Hope this helps

Chris Taylor

"Aymer" <ay****@yahoo.com> wrote in message
news:03****************************@phx.gbl...
i created a new button object. how do i attach an event
on it? thanx for your help.

Dim cc As ColorConverter = new ColorConverter()
Dim b = new button()
b.ID = "SelectedDate"

b.BorderStyle = 1
b.forecolor = cc.ConvertFromString("red")
b.BackColor = cc.ConvertFromString("white")
b.BorderColor = cc.ConvertFromString("white")
b.text = "hello"
b.click = Test

Sub Test
...do something
End Sub

Jul 19 '05 #2
thanx for the replay. but i still have a problem. there
is nothing wrong with ur code. i tried and tested ur code
and it works fine. however, i am creating the button
inside the content of an asp:calendar. look at the code
below:

<%@ Page Language="vb" Debug="true" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Web.UI.WebControls" %>

<html>
<head>
<script language="vb" runat="server">

Sub Page_Load(sender As Object, e As EventArgs)
pnlUserInfo.Visible = False
pnlCalendar.Visible = True
End Sub

Sub Calendar1_DayRender(sender As Object, e As
DayRenderEventArgs)
Dim d As CalendarDay
Dim c As TableCell

d = e.Day
c = e.Cell

If d.IsOtherMonth Then
c.Controls.Clear
Else
Try
If e.Day.Date = "8/25/2003" Then
Dim b As New Button
b.text = "hello"
AddHandler b.Click, AddressOf Text
e.Cell.Controls.Add(b)
e.Cell.Controls.Add(New LiteralControl("hello"))
End If
Catch exc As Exception
Response.Write(exc.ToString())
End Try
End If
End Sub

Sub Date_Selected(sender As Object, e As EventArgs)
pnlCalendar.Visible = False
pnlUserInfo.Visible = True
End Sub

Sub Text(sender As Object, e As EventArgs)
If txt1.text = "" Or txt1.text = "mornin" Then
txt1.text = "hello to you too."
Else
txt1.text = "mornin"
End If
End Sub

</script>
</head>
<body>

<form id="Form1" runat="server">
<asp:panel id="pnlCalendar" runat="server">
<asp:textbox id="txt1" runat="server" />
<asp:calendar id="calendar1" runat="server"
cellpadding="0"
width="600px"
daystyle-height="65px"
borderwidth="1px"
onselectionchanged="Date_Selected"
ondayrender="Calendar1_DayRender"
showgridlines="true"
/>
</asp:calendar>
</asp:panel>

<asp:panel id="pnlUserInfo" runat="server">
<asp:label runat="server" id="label1" text="Blah Blah" />
<asp:button id="btn1" runat="server" text="calendar"
onclick="Calendar"/>
</asp:panel>
</form>

</body>
</html>

if i add the new button into the form, the attach event
works. but if i add the new button inside the calendar,
the attach event does not work. please help if u can.
Jul 19 '05 #3
Hi,

Ok, first let me say that I do very little ASP.NET, mostly WinForms. So this
might not be the best solution. But after some investigation into your
problem this is what I have concluded.

When adding the controls dynamically the events must be wired in the
Page_Load since the events are fired then. If it is only done when your Cell
is rendered, the ASP.NET event firing has already been done so it is to
late. Additionally the control must belong to a control collection before
the events are fired. With this in mind, my solution entailed placing a
PlaceHolder on the page. Then in the Page_Load event I created the button,
added it to the PlaceHolder controls collection and wired the event. After
this the events Fire. Then in the Page_PreRender event I clear the
PlaceHolder controls collection, this prevents the control actually showing
in the PlaceHolder. In the DayRender I added the Button to the Cells control
collection causing the Button to appear in the correct place. I hope this
helps and I really hope there is a better solution!

See Code Below

NB: I changed the date format to work on my locale, so you will have to
change that!!!

Regards

Chris Taylor
<%@ Page Language="vb" Debug="true" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Web.UI.WebControls" %>
<HTML>
<HEAD>
<script language="vb" runat="server">

Friend WithEvents b as Button

Sub Page_Load(sender As Object, e As EventArgs)
pnlUserInfo.Visible = False
pnlCalendar.Visible = True

AddHandler button1.Click, AddressOf b_click

b = new Button()
b.text = "hello"
b.ID = "b"

PlaceHolder1.controls.Add(b)

AddHandler b.Click, AddressOf b_click

End Sub

sub Page_PreRender(sender As Object, e As EventArgs)
placeholder1.controls.clear()
end sub

Sub Calendar1_DayRender(sender As Object, e As DayRenderEventArgs)
Dim d As CalendarDay
Dim c As TableCell

d = e.Day
c = e.Cell

If d.IsOtherMonth Then
c.Controls.Clear
Else
Try
If e.Day.Date = DateTime.Parse("25/8/2003") Then
e.Cell.Controls.Add(b)
End If
Catch exc As Exception
Response.Write(exc.ToString())
End Try
End If
End Sub

Sub Date_Selected(sender As Object, e As EventArgs)
pnlCalendar.Visible = False
pnlUserInfo.Visible = True
End Sub

Sub b_click(sender As Object, e As EventArgs)
If txt1.text = "" Or txt1.text = "mornin" Then
txt1.text = "hello to you too."
Else
txt1.text = "mornin"
End If
End Sub

</script>
</HEAD>
<body>
<form id="Form1" runat="server">
<asp:panel id="pnlCalendar" runat="server">
<asp:textbox id="txt1" runat="server"></asp:textbox>
<asp:Button id="Button1" runat="server" Text="Button"></asp:Button>
<asp:PlaceHolder id="PlaceHolder1" runat="server"></asp:PlaceHolder>
<asp:calendar id="calendar1" runat="server" cellpadding="0"
width="600px" daystyle-height="65px"
borderwidth="1px" onselectionchanged="Date_Selected"
ondayrender="Calendar1_DayRender" showgridlines="true"></asp:calendar>
</asp:panel><asp:panel id="pnlUserInfo" runat="server">
<asp:label id="label1" runat="server" text="Blah Blah"></asp:label>
</asp:panel></form>
</body>
</HTML>
Jul 19 '05 #4

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

Similar topics

10
by: tony kulik | last post by:
This code works fine in ie and opera but not at all in Mozilla. Anybody got a clue as to how to get it right? <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <script...
4
by: bhatiaajay | last post by:
I am new to JavaScript objects. The object creates a button with no events attached. Later I want to attach an event to the button created by this object. The event attaches a function in the...
3
by: Big D | last post by:
I have a class that inherits from System.Web.Ui.Usercontrol... all is basically is is a button which is created progammatically such as: public abstract class BaseFrontEndEditor :...
2
by: Ashish | last post by:
Hello, I am sick and tired of ochestrating server and client side scripts. Basically I am using toolbar from IE web controls and want to attach javascript to one of the buttons' click event. The...
1
by: Nirmalkumar | last post by:
How to attach an event to dynamically created control in VB.NET I have dynamically created dropdown list in code behind (VB.NET). For this control I want to attach an event for action...
3
by: Aymer | last post by:
i created a new button object. how do i attach an event on it? thanx for your help. Dim cc As ColorConverter = new ColorConverter() Dim b = new button() b.ID = "SelectedDate" b.BorderStyle...
8
by: Ken Sturgeon | last post by:
I have a button inside a panel control. Apparently I can't expect VB to respond to the button's _Click event. How do I capture the click event? Thanks Ken
2
by: calebmichaud | last post by:
Hi I am having trouble understanding how to attach a file or files to a record. I want to (in form view0 allow the user to attach one or more than one file to the associated record, and then...
1
by: sindhu | last post by:
Hello, I am able to attach event to dynamically created element.as shown below. for(var i=0;i<10;i++) {for(var j=0;j<15;j++) { var div=document.createElement('div'); ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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.