Question repeated dynamic buttons with event handler

majicmonk

Member
Joined
Jul 21, 2009
Messages
8
Programming Experience
Beginner
Hi I'm adding image buttons to each day in a calendar control in the calendars dayrender event. I can add the buttons no problem, but I'm struggling to get a subroutine to handle their click. Please see the code below. I want each button to point to the same sub.


VB.NET:
 Protected Sub Calendar1_DayRender(sender As Object, e As System.Web.UI.WebControls.DayRenderEventArgs) Handles Calendar1.DayRender

        'create the buttons
        Dim pm As New ImageButton


       
        pm.ID = "PMButton" & e.Day.DayNumberText
        pm.ImageUrl = "Images/Calendar/HalfDay.jpg"
        pm.CommandName = "HalfDayPM"
        pm.CommandArgument = e.Day.Date

          
        AddHandler pm.Click, AddressOf DayBtn_Click

           
        e.Cell.Controls.Add(pm)
       
    End Sub



    Private Sub DayBtn_Click(sender As Object, e As CommandEventArgs)
        LabelCalendar.Text &= e.CommandName.ToString()

    End Sub




I get this error in visual studio:

Method 'Private Sub DayBtn_Click(sender As Object, e As System.Web.UI.WebControls.CommandEventArgs)' does not have a signature compatible with delegate 'Delegate Sub ImageClickEventHandler(sender As Object, e As System.Web.UI.ImageClickEventArgs)'.


Thanks

Mik.
 
Method 'Private Sub DayBtn_Click(sender As Object, e As System.Web.UI.WebControls.CommandEventArgs)' does not have a signature compatible with delegate 'Delegate Sub ImageClickEventHandler(sender As Object, e As System.Web.UI.ImageClickEventArgs)'.
It means ImageButton Click event handler has this signature:
Sub MethodName(sender As Object, e As System.Web.UI.ImageClickEventArgs)
and not this that you tried to assign:
Sub MethodName(sender As Object, e As System.Web.UI.WebControls.CommandEventArgs)
 
Thanks for your response. This fixed the error but on debugging the sub is never called, what am I missing? I also notice that in the sub when trying to access the e.commandname its not available as an e method, why is this?

Thanks.
 
I also notice that in the sub when trying to access the e.commandname its not available as an e method, why is this?
Perhaps you're thinking about the Command event, rather than the Click event?
the sub is never called, what am I missing?
DayRender event is too late in page life cycle to add controls with event handlers. This, and a workaround, is discussed in this article: Create Dynamic Buttons in an ASP.NET Calendar
Perhaps you could find other workarounds also if you search something like "DayRender dynamic controls".
 
Back
Top