How to update Menu when its an UpdatePanel trigger?

Last post 12-19-2006 6:46 PM by WishStar99. 10 replies.

Sort Posts:

  • How to update Menu when its an UpdatePanel trigger?

    12-14-2006, 5:42 PM
    • Member
      35 point Member
    • aspRoss18
    • Member since 07-07-2006, 6:36 PM
    • Posts 12

    I have a Menu control where its MenuItemClick event is a trigger for an UpdatePanel that contains a MultiView.  In Atlas, I just had both the Menu and MultiView inside the UpdatePanel and all was well, but this throws javascript errors in ASP.NET AJAX (as a Menu inside an UpdatePanel is not supported).

    Basically, when the user clicked a menu item, the background color would highlight - indicating which item was selected - and then the proper View inside the MultiView would be activated.  Now that the Menu has been moved outside the UpdatePanel, the selected menu item is no longer highlighted.

    Anyone know of a way to update the Menu without refreshing the whole screen?  Or, an alternative method?

    Thanks!

  • Re: How to update Menu when its an UpdatePanel trigger?

    12-15-2006, 4:57 PM
    • Participant
      1,863 point Participant
    • WishStar99
    • Member since 08-04-2006, 8:11 PM
    • VietNam
    • Posts 553

    As for the Menu color, you can just use plain JavaScript; and for the Multiview, you can leave it inside the UpdatePanel and have the trigger controls all links from the menu.

     

    WS

    //---------------------------------------------//
    //------------------- Chloé ------------------//
    //---------------------------------------------//
  • Re: How to update Menu when its an UpdatePanel trigger?

    12-18-2006, 5:57 PM
    • Member
      35 point Member
    • aspRoss18
    • Member since 07-07-2006, 6:36 PM
    • Posts 12

    Is there a way to, somehow, call MenuItemClick from javascript?

    Otherwise, as of right now, I created a user control that looks similar to the menu control... and the user control contains LinkButtons added programmatically.  However, now, no matter where I put this user control (inside the UpdatePanel or outside the UpdatePanel as a trigger) -- there is a full page refresh anytime a link button is clicked.  Ugh, now what?  Is there a reason why a user control inside an UpdatePanel always causes a full post back?

    I have tried RegisterAsyncPostBackControl of the link buttons during OnInit in my user control, but that doesn't seem to help.  Any other ideas?

    Thanks!

  • Re: How to update Menu when its an UpdatePanel trigger?

    12-18-2006, 8:23 PM
    • Participant
      1,863 point Participant
    • WishStar99
    • Member since 08-04-2006, 8:11 PM
    • VietNam
    • Posts 553

    Assume,

    Protected Sub MenuItemClick(sender as object, e as eventargs)

     ... do something

    end sub

    <asp:button id="btnMenuItemClick" Text="Click Menu" style="display: none;" runat="server" />

    JavaScript Function:

    function MenuItemClick()
    {  document.getElementById('" & Me.btnMenuItemClick.ClientID & "').click();  }


    That's the JavaScript function that you're looking for to call the MenuItemClick function.

     

    WS

    //---------------------------------------------//
    //------------------- Chloé ------------------//
    //---------------------------------------------//
  • Re: How to update Menu when its an UpdatePanel trigger?

    12-19-2006, 12:46 PM
    • Member
      35 point Member
    • aspRoss18
    • Member since 07-07-2006, 6:36 PM
    • Posts 12

    Oddly enough, this information was enough to help me solve my user control inside the update panel issue.  The problem w/ my user control was that all link buttons were added dynamically and thus not registered for async postback.  So, I added a link button (lnkClick) and hidden value control to the user control design and then converted all my dynamically added link buttons to HtmlAnchor controls where the href calls javascript that (1) sets the hidden value to the menu item index and (2) calls $get(<%= lnkClick.ClientID %>).click().  Go figure, this works - I can have this control inside an UpdatePanel now and it works exactly like my old Menu control.

    Thanks for the help, I appreciate it!

  • Re: How to update Menu when its an UpdatePanel trigger?

    12-19-2006, 1:04 PM
    • Member
      35 point Member
    • aspRoss18
    • Member since 07-07-2006, 6:36 PM
    • Posts 12
    For anyone interested - or who would like to critique Big Smile.

    My user control design:

    <asp:HiddenField ID=hvVal runat=server />
    <asp:LinkButton ID=lnkClick runat=server />
    <table id=tblView runat=server class=ViewMenu cellpadding=0 cellspacing=0>
        <tr id=thisRow runat=server>     
        </tr>
    </table>

    My user control code behind:

    <ParseChildren(True)> _
    Partial Class ucViewMenu
        Inherits System.Web.UI.UserControl
    
        Private _row As ITemplate
        Private _selectedIndex As Int16 = -1
        Private scriptName As String
    
        Public Event ItemClick(ByVal sender As Object, ByVal e As System.EventArgs)
    
        <PersistenceMode(PersistenceMode.InnerProperty), TemplateContainer(GetType(TemplateControl)), TemplateInstance(TemplateInstance.Single)> _
        Public Property RowTemplate() As ITemplate
            Get
                Return _row
            End Get
            Set(ByVal value As ITemplate)
                _row = value
            End Set
        End Property
    
        Public Property SelectedIndex() As Int16
            Get
                Return _selectedIndex
            End Get
            Set(ByVal value As Int16)
                _selectedIndex = value
                SelectItem(value)
            End Set
        End Property
    
        Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
            MyBase.OnInit(e)
    
            ' set & add "onclick" script for this user control
            scriptName = lnkClick.ClientID + "_Click"
            AddScript()
    
            ' dynamically create horizontal menu
            Dim td As HtmlTableCell
            Dim a As HtmlAnchor
            Dim c, lc As Control
            Dim i As Int16 = 0
    
            If Not IsNothing(_row) Then
                Dim ph As New PlaceHolder
                _row.InstantiateIn(ph)
    
                ' each placeholder in this template indicates a table cell/column
                ' move placeholder controls into A element (which should be image & text)
                While ph.Controls.Count > 0
                    c = ph.Controls(0)
                    If TypeOf c Is PlaceHolder Then
                        td = New HtmlTableCell
                        a = New HtmlAnchor
    
                        While c.Controls.Count > 0
                            lc = c.Controls(0)
                            a.Controls.Add(lc)
                            c.Controls.Remove(lc)
                        End While
    
                        a.HRef = "javascript:" + scriptName + "(" + i.ToString + ")"
                        td.Controls.Add(a)
    
                        thisRow.Controls.Add(td)
                        i += 1
                    End If
                    ph.Controls.Remove(c)
                End While
    
                ' set all cells to same width
                If thisRow.Cells.Count > 0 Then
                    Dim pct As Decimal = 100 / thisRow.Cells.Count
                    For Each cell As HtmlTableCell In thisRow.Cells
                        cell.Width = FormatNumber(pct, 0) + "%"
                    Next
                End If
    
                ' select default cell
                SelectItem(_selectedIndex)
            End If
    
        End Sub
    
        Private Sub SelectItem(ByVal index As Int16)
            _selectedIndex = index
            If index < thisRow.Cells.Count Then
                For i As Int16 = 0 To thisRow.Cells.Count - 1
                    If i = index Then
                        thisRow.Cells(i).BgColor = "#BAC3D6"
                    Else
                        thisRow.Cells(i).BgColor = String.Empty
                    End If
                Next
            End If
        End Sub
    
        Protected Sub lnkClick_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkClick.Click
            ' we run all clicks through a linkbutton on the actual form so that the update panel
            ' recognizes its "click" event as an async postback
            Dim idx As Int16 = -1
            Dim val As String = hvVal.Value
            If IsNumeric(val) Then
                idx = CInt(val)
            End If
            SelectItem(idx)
            RaiseEvent ItemClick(Me, e)
        End Sub
    
        Private Sub AddScript()
            Dim script As String
            script = "function " + scriptName + "(val)" + _
            "{ " + _
            "   $get('" + hvVal.ClientID + "').value = val;" + _
            "   $get('" + lnkClick.ClientID + "').click();" + _
            " }"
    
            Microsoft.Web.UI.ScriptManager.RegisterClientScriptBlock( _
                Me, _
                GetType(Page), _
                scriptName, _
                script, _
                True)
        End Sub
    
    End Class
    
    User control example:
    <uc:ViewMenu ID=mnuView2 runat=server SelectedIndex=0>
        <RowTemplate>
            <asp:PlaceHolder ID="ph1" runat=server><asp:Image ID="img1" runat=server ImageUrl="~/images/card.gif" />Access</asp:PlaceHolder>
            <asp:PlaceHolder ID="ph2" runat=server><asp:Image ID="img2" runat=server ImageUrl="~/images/debit.gif" />Debit</asp:PlaceHolder> 
            <asp:PlaceHolder ID="ph3" runat=server><asp:Image ID="img3" runat=server ImageUrl="~/images/mail.gif" />Address</asp:PlaceHolder>
            <asp:PlaceHolder ID="ph4" runat=server><asp:Image ID="img4" runat=server ImageUrl="~/images/car.gif" />Vehicle</asp:PlaceHolder>
            <asp:PlaceHolder ID="ph5" runat=server><asp:Image ID="img5" runat=server ImageUrl="~/images/contract.gif" />Contract</asp:PlaceHolder>
            <asp:PlaceHolder ID="ph6" runat=server><asp:Image ID="img6" runat=server ImageUrl="~/images/other.gif"  />Other</asp:PlaceHolder>
        </RowTemplate>
    </uc:ViewMenu>
     

  • Re: How to update Menu when its an UpdatePanel trigger?

    12-19-2006, 1:20 PM
    • Participant
      1,863 point Participant
    • WishStar99
    • Member since 08-04-2006, 8:11 PM
    • VietNam
    • Posts 553

    ***Microsoft.Web.UI.ScriptManager.RegisterClientScriptBlock***

    Works for you in Ajax 1.0 RC? For me it stops working after upgrading to RC. If it doesn't, then you might want to try replace Microsoft with System. Cheer =)

     

    WS

    //---------------------------------------------//
    //------------------- Chloé ------------------//
    //---------------------------------------------//
  • Re: How to update Menu when its an UpdatePanel trigger?

    12-19-2006, 2:04 PM
    • Member
      35 point Member
    • aspRoss18
    • Member since 07-07-2006, 6:36 PM
    • Posts 12
    Well, how about that, there's a new version.  Geez, I'm still upgrading from Atlas to Ajax...  I suppose I'll go to Ajax 1.0 RC to break it then Smile.
  • Re: How to update Menu when its an UpdatePanel trigger?

    12-19-2006, 4:58 PM
    • Member
      35 point Member
    • aspRoss18
    • Member since 07-07-2006, 6:36 PM
    • Posts 12

    Ok, all upgraded.  That was fun, switching all those "Microsoft"s to "System"s.

    While I'm here, anyone know how to create a class object that functions similar to how a BoundColumn does in the <Columns> of a GridView?  Given my code above, let's say I want to have an object only appear in my <RowTemplate> -- maybe something like <uc:ViewMenuItem> that has properties ImageUrl and Text.  Then I don't have to go through all the overhead of the Placeholder, Image, etc.

    For example, something like this might be nice to do:

    <uc:ViewMenu ID=mnuView runat=server SelectedIndex=0>
        <RowTemplate>
            <uc:ViewMenuItem ImageUrl="~/images/card.gif" Text="Cards" />
            <uc:ViewMenuItem ImageUrl="~/images/mail.gif" Text="Address" />
        </RowTemplate>
    </uc:ViewMenu>

  • Re: How to update Menu when its an UpdatePanel trigger?

    12-19-2006, 5:24 PM
    • Member
      35 point Member
    • aspRoss18
    • Member since 07-07-2006, 6:36 PM
    • Posts 12

    Nevermind, I'll just put a Menu inside the <RowTemplate> and then process each MenuItem. Sorry about the replies to myself Smile

  • Re: How to update Menu when its an UpdatePanel trigger?

    12-19-2006, 6:46 PM
    • Participant
      1,863 point Participant
    • WishStar99
    • Member since 08-04-2006, 8:11 PM
    • VietNam
    • Posts 553
    lol
    //---------------------------------------------//
    //------------------- Chloé ------------------//
    //---------------------------------------------//
Page 1 of 1 (11 items)