Integrating ASP.NET Ajax WebPart with SharePoint 3.0
page 2 of 8
by Abdulla Hussein AbdelHaq
Feedback
Average Rating: 
Views (Total / Last 10 Days): 49898/ 120

Building Webpart DLL Project

To create a web part class, open your visual studio, create a new Class Library Project and name it as "TestAbdelHaqWebpart." Then add a new class and name it "MyAjaxWebPart."

First of all, you have to add some references to the project which are listed below:

·         System.Web

·         System.Web.Extensions

·         System.Web.Extensions.Design

In addition, you need to inherit the WebPart class.

Take a look at listing 1; it is our Ajax WebPart class.

Listing 1

Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Text
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Data
Imports System.Data.SqlClient
 
Public Class MyAjaxWebPart
    Inherits WebPart
 
    Private _grdSearch As GridView
    Private _txtSearch As TextBox
    Private _btnSearch As Button
    Private _lblMsg As Label
    Private _updatePanel As UpdatePanel
    Private _updateProgress As UpdateProgress
    Private Shared ReadOnly EventSubmitKey As New Object()
    Public NewAjaxWebPart12()
 
    Public Sub New()
        Me.ExportMode = WebPartExportMode.All
    End Sub
 
    <WebBrowsable(True), _
        Personalizable(PersonalizationScope.Shared), _
        Category("AbdHaq Property") _
        > _
        Public Property LoadingImageURL() As String
        Get
            Dim _obj As Object = ViewState("LoadingImageURL")
            If _obj Is Nothing Then
                Return String.Empty
            Else
                Return DirectCast(ViewState("LoadingImageURL"), String)
            End If
 
        End Get
        Set(ByVal value As String)
            ViewState("LoadingImageURL"= value
        End Set
    End Property
    Public Custom Event Submit As EventHandler
        AddHandler(ByVal value As EventHandler)
            Events.AddHandler(EventSubmitKey, value)
        End AddHandler
        RemoveHandler(ByVal value As EventHandler)
            Events.RemoveHandler(EventSubmitKey, value)
        End RemoveHandler
        RaiseEvent(ByVal sender As Object, _
            ByVal e As System.EventArgs)
            CType(Events(EventSubmitKey), _
                EventHandler).Invoke(sender, e)
        End RaiseEvent
    End Event
 
    Private Sub _btnSearch_Click(ByVal source As ObjectByVal e As EventArgs)
 
        _grdSearch.Visible = False
 
        System.Threading.Thread.Sleep(1500)
 
        '//Create DataTable Structure
        Dim Dtable = New DataTable("tmpTable")
        '//User_ID Col
        Dtable.Columns.Add("User_ID", GetType(Integer))
        Dtable.Columns("User_ID").AutoIncrement = True
        Dtable.Columns("User_ID").AutoIncrementSeed = 1
        '//User_Name Col
        Dtable.Columns.Add("User_Name", GetType(String))
        '//User_Department Col
        Dtable.Columns.Add("User_Department"GetType(String))
 
 
        Dim dr As DataRow
 
        dr = Dtable.NewRow
        dr("User_Name"= "Abdulla Abdelhaq"
        dr("User_Department"= "Technical"
        Dtable.Rows.Add(dr)
 
        dr = Dtable.NewRow
        dr("User_Name"= "Noura Ahmad"
        dr("User_Department"= "Technical"
        Dtable.Rows.Add(dr)
 
        dr = Dtable.NewRow
        dr("User_Name"= "Oday Mohammad"
        dr("User_Department"= "Production"
        Dtable.Rows.Add(dr)
 
        Dim _searchFor As String = "%"
 
        If _txtSearch.Text <> String.Empty Then
            _searchFor = _txtSearch.Text
        End If
 
        Dim dtView As New DataView(Dtable)
        dtView.RowFilter = "User_Name LIKE '%'+'" & _searchFor & "'+'%'"
 
        If dtView.Count > 0 Then
            _grdSearch.Visible = True
            _grdSearch.AutoGenerateColumns = True
            _grdSearch.DataSource = dtView
            _grdSearch.DataBind()
            _lblMsg.Text = String.Empty
        Else
            _lblMsg.Text = "<font color='red'>Sorry, No Data Found!.</font>"
            _grdSearch.Visible = False
        End If
 
    End Sub
 
    Protected Overrides Sub CreateChildControls()
        Controls.Clear()
 
        _updatePanel = New UpdatePanel
        With _updatePanel
            .ID = "UpdatePanel1"
            .ChildrenAsTriggers = True
            .UpdateMode = UpdatePanelUpdateMode.Conditional
 
        End With
 
        _updateProgress = New UpdateProgress
        _updateProgress.ID = "UpdateProgress1"
 
        Dim _templateHTML As String
        If LoadingImageURL = String.Empty Then
            _templateHTML = "<span>Loading ...</span>"
        Else
            _templateHTML = "<div><img alt='Loading...' src='" & _
              LoadingImageURL.Trim & "'/></div>"
        End If
 
        _updateProgress.ProgressTemplate = New ProgressTemplate(_templateHTML)
 
        _updateProgress.AssociatedUpdatePanelID = _updatePanel.ClientID
 
        Me.Controls.Add(_updatePanel)
 
        _grdSearch = New GridView
        _grdSearch.ID = "GrdSearch"
 
        _txtSearch = New TextBox
        _txtSearch.ID = "txtSearch"
 
        _btnSearch = New Button
        _btnSearch.Text = "Search"
        _btnSearch.ID = "BtnSearch"
 
        _lblMsg = New Label
        _lblMsg.ID = "lblMsgError"
 
        AddHandler _btnSearch.Click, AddressOf _btnSearch_Click
 
        _updatePanel.ContentTemplateContainer.Controls.Add(_txtSearch)
        _updatePanel.ContentTemplateContainer.Controls.Add(_btnSearch)
        _updatePanel.ContentTemplateContainer.Controls.Add(_grdSearch)
        _updatePanel.ContentTemplateContainer.Controls.Add(_lblMsg)
        _updatePanel.ContentTemplateContainer.Controls.Add(_updateProgress)
 
    End Sub
 
    Protected Overrides Sub OnInit(ByVal e As EventArgs)
 
        MyBase.OnInit(e)
 
        'get the existing ScriptManager if it exists on the page
        Dim _AjaxManager As ScriptManager = ScriptManager.GetCurrent(Me.Page)
        If _AjaxManager Is Nothing Then
 
            'create new ScriptManager and EnablePartialRendering
            _AjaxManager = New ScriptManager()
            _AjaxManager.EnablePartialRendering = True
 
            'Fix problem with postbacks and form actions (DevDiv 55525)
            Page.ClientScript.RegisterStartupScript(Me.GetType, Me.ID, _
              "_spOriginalFormAction = document.forms[0].action;"True)
 
            'tag:"form" att:"onsubmit" val:"return _spFormOnSubmitWrapper()"
            'blocks async postbacks after the first one
            'not calling "_spFormOnSubmitWrapper()" breaks all postbacks
            'returning true all the time, somewhat defeats the purpose of the
            '_spFormOnSubmitWrapper() which is to block repetitive postbacks, 
            'but it allows MS AJAX Extensions to work properly
 
            If Not Me.Page.Form Is Nothing Then
                Dim formOnSubmitAtt As String = Me.Page.Form.Attributes("onsubmit")
                If Not String.IsNullOrEmpty(formOnSubmitAtt) And _
                  formOnSubmitAtt = "return _spFormOnSubmitWrapper();" Then
                    Me.Page.Form.Attributes("onsubmit") =_
                      "_spFormOnSubmitWrapper();"
                End If
                'add the ScriptManager as the first control in the Page.Form
                Me.Page.Form.Controls.AddAt(0, _AjaxManager)
 
            End If
        End If
 
    End Sub
 
    Protected Overrides Sub RenderContents(ByVal writer As _
      System.Web.UI.HtmlTextWriter)
        _updatePanel.RenderControl(writer)
    End Sub
End Class
'Class for Building progress tempales 
Public Class ProgressTemplate : Implements ITemplate
    Private template As String
 
    Public Sub New(ByVal temp As String)
        template = temp
    End Sub
 
    Public Sub InstantiateIn(ByVal container As Control) Implements _
      Web.UI.ITemplate.InstantiateIn
        Dim ltr As New LiteralControl(Me.template)
        container.Controls.Add(ltr)
    End Sub
 
End Class

View Entire Article

User Comments

Title: Mr   
Name: Dinesh
Date: 2010-11-15 5:48:03 AM
Comment:
Awesome code and I feel it is the best one for understanding Ajax in Sharepoint. But one small request can u write/give the code in C#. I will be very happy if u do this favor for me. Thanks a lot.
Title: Mr   
Name: James
Date: 2010-10-02 9:57:49 AM
Comment:
Awesome code, and great references too. } Really thumbs up for the code, this is the best tutorial so far!
Will study how all of this works... =)
Title: Mr   
Name: Hiep
Date: 2010-06-18 12:47:50 AM
Comment:
This article is useful. thanks much
Title: Mr   
Name: Jafari
Date: 2009-10-19 8:47:26 AM
Comment:
Thanks Abdulla,
could you explain how to use ajax with multiple updatepanels?
Title: Mr   
Name: ashwin
Date: 2009-02-06 12:54:08 PM
Comment:
Cool stuff
Title: Mr   
Name: Daniel Partridge
Date: 2009-01-21 5:02:39 AM
Comment:
This looks really good!!!!
Title: Dan   
Name: Dan
Date: 2008-12-27 9:45:50 AM
Comment:
Really Liked the oninit code.
Title: mrs   
Name: Madhuri
Date: 2008-12-15 4:32:54 PM
Comment:
This is really very useful. Thanks






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2021 ASPAlliance.com  |  Page Processed at 2021-03-09 6:14:58 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search