Hi micah,
I have created a sample based on your previous thread as everything will be same just the coding part will be changed so refer your previous thread and below sample code and make the changes according to your need.
HTML
<div>
<asp:HiddenField ID="hfUserName" runat="server" />
<asp:DataList ID="dlBooks" runat="server" RepeatLayout="Table" OnItemDataBound="dlBooks_ItemDataBound">
<HeaderTemplate>
<tr>
<th>
<asp:Label Text="Book Pics" runat="server" />
</th>
<th>
<asp:Label ID="Label1" Text="BookName" runat="server" />
</th>
<th>
<asp:Label ID="Label2" Text="Favorite Book" runat="server" />
</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label ID="FavoriteId" Style="display: none;" runat="server" />
<asp:Label ID="lblId" Text='<%#Eval("Id") %>' Visible="false" runat="server" />
<asp:ImageButton ID="Imagebooks" ImageUrl='<%# "Images/" + Eval("Name") %>'
Width="80px" Height="50px" runat="server" />
</td>
<td>
<asp:Label ID="lblbookName" Text='<%#Eval("Name")%>' runat="server" />
</td>
<td>
<asp:LinkButton ID="btnAdd" CssClass="glyphicon glyphicon-usd" ForeColor="#003366"
Font-Size="Large" runat="server" />
<asp:Label ID="lblBOOKCOUNT" runat="server" />
</td>
</tr>
</ItemTemplate>
</asp:DataList>
<br />
<br />
<asp:LinkButton ID="LinkButton1" Text="Logout" OnClick="OnLogOut" runat="server" />
</div>
<div>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/json2/20130526/json2.min.js"></script>
<script type="text/javascript">
$(function () {
$('[id*=btnAdd]').click(function () {
var obj = {};
$('[id*=hfUserName]').val('<%= Session["UserName"].ToString() %>');
obj.userName = $('[id*=hfUserName]').val();
obj.icon = $(this)[0].className;
obj.bookName = $(this).closest('tr').find('[id*=lblbookName]').html();
obj.favouriteId = $(this).closest('tr').find('[id*=FavoriteId]').html() != "" ? $(this).closest('tr').find('[id*=FavoriteId]').html() : "0";
var rowIndex = $(this).closest('tr')[0].rowIndex;
$.ajax({
type: "POST",
url: "Default2.aspx/OnfavoriteBook",
data: JSON.stringify(obj),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
var className = response.d[0];
var bookCount = response.d[1];
$('[id*=dlBooks] tr').eq(rowIndex).find('[id*=lblBOOKCOUNT]').html(bookCount);
$('[id*=dlBooks] tr').eq(rowIndex).find('[id*=btnAdd]')[0].className = className;
},
failure: function (response) {
alert(response.d);
},
error: function (response) {
alert(response.d);
}
});
return false;
});
});
</script>
</div>
C#
private static string constring = ConfigurationManager.ConnectionStrings["constring"].ToString();
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.Populatebooks();
}
}
private void Populatebooks()
{
string userId = Session["UserName"].ToString();
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM [dbo].[Files]", con))
{
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
dlBooks.DataSource = dt;
dlBooks.DataBind();
}
}
}
[System.Web.Services.WebMethod]
public static List<string> OnFavoriteBook(string icon, string bookName, int favouriteId, string userName)
{
string bookIcon = "glyphicon glyphicon-save-file";
List<string> values = new List<string>();
string lblbookCount = string.Empty;
string message = string.Empty;
using (SqlConnection con = new SqlConnection(constring))
{
if (icon.ToUpper() == "GLYPHICON GLYPHICON-USD")
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO Dim_favorite values(@UserName,@BookName,@BookCount)", con))
{
cmd.Parameters.AddWithValue("@UserName", userName);
cmd.Parameters.AddWithValue("@BookName", bookName);
cmd.Parameters.AddWithValue("@BookCount", 1);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
lblbookCount = GetBookCounts(bookName, lblbookCount, con);
bookIcon = "glyphicon glyphicon-save-file";
}
else
{
using (SqlCommand cmd = new SqlCommand("DELETE FROM Dim_favorite WHERE FavoriteId=@FavoriteId", con))
{
cmd.Parameters.AddWithValue("@FavoriteId", favouriteId);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
lblbookCount = GetBookCounts(bookName, lblbookCount, con);
bookIcon = "glyphicon glyphicon-usd";
}
}
values.Add(bookIcon);
values.Add(lblbookCount);
return values;
}
private static string GetBookCounts(string bookName, string lblbookCount, SqlConnection con)
{
using (SqlCommand cmd = new SqlCommand("SELECT COUNT(bookName) as BookCount FROM Dim_favorite WHERE BookName='" + bookName + "'", con))
{
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
if (dt.Rows.Count > 0)
{
lblbookCount = dt.Rows[0]["BookCount"].ToString() != "0" ? dt.Rows[0]["BookCount"].ToString() : "";
}
}
return lblbookCount;
}
protected void dlBooks_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
string bookName = (e.Item.FindControl("lblbookName") as Label).Text;
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Dim_favorite WHERE UserName='" + Session["UserName"] + "'", con))
{
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
if (dt.Rows.Count > 0)
{
foreach (DataRow row in dt.Rows)
{
if (bookName == row["BookName"].ToString())
{
(e.Item.FindControl("btnAdd") as LinkButton).CssClass = "glyphicon glyphicon-save-file";
(e.Item.FindControl("FavoriteId") as Label).Text = !string.IsNullOrEmpty(row["FavoriteId"].ToString()) ? row["FavoriteId"].ToString() : "0";
}
}
}
}
using (SqlCommand cmd = new SqlCommand("SELECT COUNT(bookName) as BookCount FROM Dim_favorite WHERE BookName='" + bookName + "'", con))
{
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
if (dt.Rows.Count > 0)
{
(e.Item.FindControl("lblBOOKCOUNT") as Label).Text = dt.Rows[0]["BookCount"].ToString() != "0" ? dt.Rows[0]["BookCount"].ToString() : "";
}
}
}
}
}
VB.Net
Private Shared constring As String = ConfigurationManager.ConnectionStrings("constring").ToString()
Protected Sub Page_Load(sender As Object, e As EventArgs)
If Not Me.IsPostBack Then
Me.Populatebooks()
End If
End Sub
Private Sub Populatebooks()
Dim userId As String = Session("UserName").ToString()
Using con As New SqlConnection(constring)
Using cmd As New SqlCommand("SELECT * FROM [dbo].[Files]", con)
Dim dt As New DataTable()
Dim sda As New SqlDataAdapter(cmd)
sda.Fill(dt)
dlBooks.DataSource = dt
dlBooks.DataBind()
End Using
End Using
End Sub
<System.Web.Services.WebMethod> _
Public Shared Function OnFavoriteBook(icon As String, bookName As String, favouriteId As Integer, userName As String) As List(Of String)
Dim bookIcon As String = "glyphicon glyphicon-save-file"
Dim values As New List(Of String)()
Dim lblbookCount As String = String.Empty
Dim message As String = String.Empty
Using con As New SqlConnection(constring)
If icon.ToUpper() = "GLYPHICON GLYPHICON-USD" Then
Using cmd As New SqlCommand("INSERT INTO Dim_favorite values(@UserName,@BookName,@BookCount)", con)
cmd.Parameters.AddWithValue("@UserName", userName)
cmd.Parameters.AddWithValue("@BookName", bookName)
cmd.Parameters.AddWithValue("@BookCount", 1)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
lblbookCount = GetBookCounts(bookName, lblbookCount, con)
bookIcon = "glyphicon glyphicon-save-file"
Else
Using cmd As New SqlCommand("DELETE FROM Dim_favorite WHERE FavoriteId=@FavoriteId", con)
cmd.Parameters.AddWithValue("@FavoriteId", favouriteId)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
lblbookCount = GetBookCounts(bookName, lblbookCount, con)
bookIcon = "glyphicon glyphicon-usd"
End If
End Using
values.Add(bookIcon)
values.Add(lblbookCount)
Return values
End Function
Private Shared Function GetBookCounts(bookName As String, lblbookCount As String, con As SqlConnection) As String
Using cmd As New SqlCommand((Convert.ToString("SELECT COUNT(bookName) as BookCount FROM Dim_favorite WHERE BookName='") & bookName) + "'", con)
Dim dt As New DataTable()
Dim sda As New SqlDataAdapter(cmd)
sda.Fill(dt)
If dt.Rows.Count > 0 Then
lblbookCount = If(dt.Rows(0)("BookCount").ToString() <> "0", dt.Rows(0)("BookCount").ToString(), "")
End If
End Using
Return lblbookCount
End Function
Protected Sub dlBooks_ItemDataBound(sender As Object, e As DataListItemEventArgs)
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
Dim bookName As String = TryCast(e.Item.FindControl("lblbookName"), Label).Text
Using con As New SqlConnection(constring)
Using cmd As New SqlCommand("SELECT * FROM Dim_favorite WHERE UserName='" + Session("UserName") + "'", con)
Dim dt As New DataTable()
Dim sda As New SqlDataAdapter(cmd)
sda.Fill(dt)
If dt.Rows.Count > 0 Then
For Each row As DataRow In dt.Rows
If bookName = row("BookName").ToString() Then
TryCast(e.Item.FindControl("btnAdd"), LinkButton).CssClass = "glyphicon glyphicon-save-file"
TryCast(e.Item.FindControl("FavoriteId"), Label).Text = If(Not String.IsNullOrEmpty(row("FavoriteId").ToString()), row("FavoriteId").ToString(), "0")
End If
Next
End If
End Using
Using cmd As New SqlCommand((Convert.ToString("SELECT COUNT(bookName) as BookCount FROM Dim_favorite WHERE BookName='") & bookName) + "'", con)
Dim dt As New DataTable()
Dim sda As New SqlDataAdapter(cmd)
sda.Fill(dt)
If dt.Rows.Count > 0 Then
TryCast(e.Item.FindControl("lblBOOKCOUNT"), Label).Text = If(dt.Rows(0)("BookCount").ToString() <> "0", dt.Rows(0)("BookCount").ToString(), "")
End If
End Using
End Using
End If
End Sub
ScreenShot