Results 1 to 4 of 4

Thread: repeated dynamic buttons with event handler

  1. #1
    majicmonk is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.5 (VS 2008)
    Join Date
    Jul 2009
    Posts
    8
    Reputation
    0

    repeated dynamic buttons with event handler

    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.


    Code:
     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.

  2. #2
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.5
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    15,090
    Reputation
    2804
    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)
    How to format posts with code blocks etc - present the problem/post properly

    Visual Studio Community 2017

  3. #3
    majicmonk is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.5 (VS 2008)
    Join Date
    Jul 2009
    Posts
    8
    Reputation
    0
    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.

  4. #4
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.5
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    15,090
    Reputation
    2804
    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".
    How to format posts with code blocks etc - present the problem/post properly

    Visual Studio Community 2017

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •