Jamie Hartnoll
Jamie Hartnoll

Reputation: 7341

Refresh User Control with jQuery

I have a User Control which renders a simple drop downList into the page.

By default, only certain values are returned depending on User Details, but the user may request a "full" list.

I'd like this full list to be generated by pressing a reload button.

Being new to .NET I am struggling to get this to work and not really understand the results I get when Googling or finding stuff on this site. Is Classic ASP I'd have made a page that renders this and called it using jQuery

What I want to do is:

    $('.loadMore').live('click', function () {
        $('#listContainer').load('/Controls/List.ascx');
    });

But this returns an error in Firebug saying "NetworkError: 403 Forbidden"

I don't particularly want to use an updatepanel.

I've found this link: http://www.codeproject.com/Articles/117475/Load-ASP-Net-User-Control-Dynamically-Using-jQuery but am unsure exactly what it is suggesting, mainly I think, because I use VB and don't completely understand how to convert that C# code there.

Using .NET 2.0, jQuery and VB, does anyone have any suggestions on the simplest way to accomplish this?

Upvotes: 1

Views: 4394

Answers (1)

Mark Schultheiss
Mark Schultheiss

Reputation: 34168

Does a conversion help you:

Public Class jQueryHandler
    Implements IHttpHandler
    Public Sub ProcessRequest(context As HttpContext)
        ' We add control in Page tree collection
        Using dummyPage = New Page()
            dummyPage.Controls.Add(GetControl(context))
            context.Server.Execute(dummyPage, context.Response.Output, True)
        End Using
    End Sub

    Private Function GetControl(context As HttpContext) As Control
        ' URL path given by load(fn) method on click of button
        Dim strPath As String = context.Request.Url.LocalPath
        Dim userctrl As UserControl = Nothing
        Using dummyPage = New Page()
            userctrl = TryCast(dummyPage.LoadControl(strPath), UserControl)
        End Using
        ' Loaded user control is returned
        Return userctrl
    End Function

    Public ReadOnly Property IsReusable() As Boolean
        Get
            Return True
        End Get
    End Property
End Class

Upvotes: 1

Related Questions