Hi smile,
Check this example. Now please take its reference and correct your code.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
HTML
<asp:DataList ID="rptCustomers" runat="server" RepeatDirection="Horizontal">
<ItemTemplate>
<div style="border: 1px solid #ccc; width: 20px; text-align: center;">
<asp:LinkButton ID="lnkId" Text='<%# Eval("EmployeeId") %>' runat="server" />
</div>
</ItemTemplate>
</asp:DataList>
<br />
<div id="dvDetails"></div>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$('[id*=lnkId]').on('mouseover', function () {
$('#dvDetails').html("");
var id = $(this).html();
$.ajax({
type: "POST",
url: "Default.aspx/GetName",
data: '{id: "' + id + '"}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
$('#dvDetails').html(data.d);
},
failure: function (response) {
alert(response.d);
},
error: function (response) {
alert(response.d);
}
});
});
$('[id*=lnkId]').on('mouseout', function () {
$('#dvDetails').html("");
});
});
</script>
Namespaces
C#
using System.Data.SqlClient;
using System.Configuration;
using System.Web.Services;
VB.Net
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Web.Services
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindRepeater();
}
}
private void BindRepeater()
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand("SELECT EmployeeId FROM Employees ORDER BY EmployeeId", con))
{
con.Open();
this.rptCustomers.DataSource = cmd.ExecuteReader();
this.rptCustomers.DataBind();
con.Close();
}
}
}
[WebMethod]
public static string GetName(string id)
{
string name = string.Empty;
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand("SELECT FirstName FROM Employees WHERE EmployeeId=@Id", con))
{
cmd.Parameters.AddWithValue("@Id", id);
con.Open();
name = Convert.ToString(cmd.ExecuteScalar());
con.Close();
}
}
return name;
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindRepeater()
End If
End Sub
Private Sub BindRepeater()
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conString)
Using cmd As SqlCommand = New SqlCommand("SELECT EmployeeId FROM Employees ORDER BY EmployeeId", con)
con.Open()
Me.rptCustomers.DataSource = cmd.ExecuteReader()
Me.rptCustomers.DataBind()
con.Close()
End Using
End Using
End Sub
<WebMethod>
Public Shared Function GetName(ByVal id As String) As String
Dim name As String = String.Empty
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conString)
Using cmd As SqlCommand = New SqlCommand("SELECT FirstName FROM Employees WHERE EmployeeId=@Id", con)
cmd.Parameters.AddWithValue("@Id", id)
con.Open()
name = Convert.ToString(cmd.ExecuteScalar())
con.Close()
End Using
End Using
Return name
End Function
Screenshot