Tutorial: Introduction to AJAX in ASP.NET 4.0 and VB
Now we can simply just call the Script Manager Service or ToolKit Script Manager.
Also go into you code behind and import these 2 lines, since we will be using a database we made to pull data from, you will want to import these namespaces. We have included a database to use during the tutorial, feel free to download our source code.
We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect’s help, we were able to avoid any headaches!
In this tutorial, the only configuration step beyond that is to add the following code in the web.config file, inside the <system.web> element.
If you have Visual Studio 2008 and above the AJAX Control Toolkit manager will make these changes for you automatically.
<httphandlers> <add verb="POST,GET" path="ajax/*.ashx" type="Ajax.PageHandlerFactory, Ajax"/> </httphandlers>
Then we use GetData() even of btnGetData and display() event of DropDownList to do the work. The button btnGetData is an html button, it displays employee data without page post-back. When we choose a particular record in the Dropdownlist, the employee name and employee ID will be displayed in the label.
Below we added
A button to invoke our button click event.
A simple ASP DropDown List.
Then we added a Label to report to our user that ” No Record was Selected.”
If you’re looking for a really good web host, try Server Intellect – we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!
Now we will register ajax in the pageload function, and we will extract the data from the employee table of the database we used for the example. Use the SQLConnection Method to connect to the database.
We have included the database in our source code which is available for download.
Imports System Imports System.Data Imports System.Data.SqlClient Imports System.Configuration Imports System.Collections Imports System.Web Imports System.Web.Security Imports System.Web.UI Imports System.Web.UI.WebControls Imports System.Web.UI.WebControls.WebParts Imports System.Web.UI.HtmlControls Imports Ajax Class Ajax_CSharp Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Ajax.Utility.RegisterTypeForAjax(GetType(Ajax_CSharp)) End Sub <Ajax.AjaxMethod(HttpSessionStateRequirement.ReadWrite)> _ Public Function GetData() As String Try Dim con As New SqlConnection(ConfigurationManager.AppSettings("conn")) Dim cmd As New SqlCommand("SELECT * FROM employee", con) cmd.Connection.Open() Dim adapter As New SqlDataAdapter(cmd) Dim ds As New DataSet("DataSet") adapter.Fill(ds, "Table") If ds.Tables(0).Rows.Count <= 0 Then Return "Empty" Else Dim empID As String = "" Dim empName As String = "" Dim i As Integer For i = 0 To ds.Tables(0).Rows.Count - 1 empID += ds.Tables(0).Rows(i)(0).ToString() + "," empName += ds.Tables(0).Rows(i)(1).ToString() + "," Next i empID = empID.Substring(0, empID.Length - 1) empName = empName.Substring(0, empName.Length - 1) Return empID + "~" + empName End If Catch Return "Error" End Try End Function End Class
Just to simply explain what the code is doing, We Called our ajax functionality in our Page_load, Then connected to our Database using simple SQL commands and then returning results from our employees database. We invoked a try / catch statement to catch return a message that we specified to our label that no record was selected.
And there you have, Your first AJAX application, a simple introduction to get your feet wet.
Remember we included the source file so you may follow along and get your hands dirty with the code.
See how easy that was?
Not as easy when,
We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect’shelp, we were able to avoid any headaches!
Thank you for reading our tutorial on AJAX Controls, we aim to provide the best AJAX tutorials. Learning AJAX in ASP.NET is easy and we hope you are getting your hands dirty by trying different things and coming up with your own ideas. If you have any questions or concerns, please feel free to suggest a tutorial or you can comment below in the comments section.