attach an event on a created button

  • Thread starter Thread starter Aymer
  • Start date Start date
A

Aymer

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
 
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
 
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.
 
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>
 
Back
Top