Saturday, March 24, 2012

Lazy Loader In ASP.Net And AJAX

1 ) Add Java Script and Call AJAX Method defined in the ASP.net code behind File:


 <script type="text/javascript">
        var flag = true;

        window.onload = function() {
            document.getElementById("hdnPg").value = 1;   
// for restart paging from 1
            var TargetBaseControl = document.getElementById("TdContainer");
            var Inputs = TargetBaseControl.getElementsByTagName("input");
            for (var n = 0; n < Inputs.length; ++n) {
                if (Inputs[n].type == 'checkbox') {
                    Inputs[n].disabled = false;
                    Inputs[n].parentElement.disabled = false;
                    Inputs[n].checked = false;
                    Inputs[n].removeAttribute("checked");
                }
            }
        }
        function AppendResult() {
            //debugger;          
            var getKey = document.getElementById("lblSearch").getElementsByTagName("u")
            var Key = getKey[0].innerHTML;
            var currPage = parseInt((document.getElementById("hdnPg").value)) + 1;
            var KeyID = document.getElementById("hdnSearchUID").value;
            var ResultItem = searching.AppendProduct(Key, KeyID, currPage);
            if (ResultItem.value != "" && ResultItem != null && ResultItem.value != "N") {
                document.getElementById("TdContainer").innerHTML += ResultItem.value;
                document.getElementById("hdnPg").value = parseInt((document.getElementById("hdnPg").value)) + 1;
                document.getElementById("pPage" + document.getElementById("hdnPg").value).innerHTML = "Page : " + document.getElementById("hdnPg").value;
            }
            if (ResultItem.value == "N") {              
                document.getElementById("TdLoadMore").innerHTML = "No More Products For This Keyword";
                document.getElementById("TdLoadMore").style.display = "block";
                document.getElementById("divLoadingMessage").style.display = "none";
                window.onscroll = null;
            }
            flag = true;
        }

        window.onscroll = function() {
            var tblCmp = document.getElementById("tblComparelist");
            if (document.documentElement.scrollTop != undefined) {
                if (document.documentElement.scrollTop > 175 || document.body.scrollTop > 175) {
                    tblCmp.style.top = 0;
                    tblCmp.style.position = "fixed";
                    tblCmp.style.width = 765 + "px";

                }
                else {
                    tblCmp.style.position = "relative";
                    tblCmp.style.width = "100%";

                }
            }
            else if (window.pageYOffset != undefined) {
                if (window.pageYOffset > 175) {
                    tblCmp.style.top = 0;
                    tblCmp.style.position = "fixed";
                    tblCmp.style.width = 765 + "px";

                }
                else {

                    tblCmp.style.position = "relative";
                    tblCmp.style.width = "100%";
                }
            }

            else {
                tblCmp.style.position = "relative";
                tblCmp.style.width = "100%";

            }
            /*For scroll paging work fine in ie 7+ , mozilla, chrome*/
            if (navigator.appName == "Microsoft Internet Explorer") {
                if (document.documentElement.scrollTop > document.documentElement.scrollHeight - 660 && flag == true) {
                    document.getElementById("divLoading").style.display = "";
                    flag = false;
                    AppendResult();
                    document.getElementById("divLoading").style.display = "none";
                }
            }
            else {
                if (document.documentElement.scrollTop > window.scrollMaxY - 50 && flag == true) {
                    document.getElementById("divLoading").style.display = "";
                    flag = false;
                    AppendResult();
                    document.getElementById("divLoading").style.display = "none";
                }
                else if (document.body.scrollTop >= (document.documentElement.scrollHeight - document.documentElement.clientHeight - 20) && flag == true) {
                    document.getElementById("divLoading").style.display = "";
                    flag = false;
                    AppendResult();
                    document.getElementById("divLoading").style.display = "none";
                }
            }
        }

        function AddToWishlist(obj) {

            var position = [0, 0];
            var DL = document.getElementById('<%= "dlSearch"%>')
            var tblWish = document.getElementById("tblWishList");
            if (DL.offsetParent) {
                do {
                    position[0] += DL.offsetLeft;
                    position[1] += DL.offsetTop;
                } while (DL = DL.offsetParent);
            }

            var result = searching.AddToWishList(obj.parentElement.parentElement.parentElement.getElementsByTagName("input")[0].parentElement.title);
            if (result.value == "True") {
                document.getElementById("cartCnt").innerHTML = parseInt(document.getElementById("cartCnt").innerHTML) + 1;
                tblWish.style.top = position[1] + "px";
                tblWish.style.left = position[0] + document.getElementById('<%= "dlSearch"%>').offsetWidth + "px";
                tblWish.style.position = "fixed";
                tblWish.style.display = "block";
            }
            return false;
        }
        function ClearWishList() {
            var result = searching.ClearWishList();
            var tblWish = document.getElementById("tblWishList").style.display = "none";
            document.getElementById("cartCnt").innerHTML = 0;
        }
    script>

 
2) Add Code Behind AJAX Method:
_
    Public Function AppendProduct(ByVal key As String, ByVal KeyID As String, ByVal CurrPage As String) As String
        Dim str As String
        Dim cn As New SqlConnection(ConfigurationManager.AppSettings("ConnectionString"))
        Dim da As SqlDataAdapter

        If cn.State = ConnectionState.Closed Then
            cn.Open()
        End If
        ' hdnPg.Value = (Convert.ToInt32(hdnPg.Value) + 1).ToString()
        Try

            da = New SqlDataAdapter("Usp_Searching", cn)
            da.SelectCommand.CommandType = CommandType.StoredProcedure
            da.SelectCommand.Parameters.AddWithValue("@CmdType", "Select")
            da.SelectCommand.Parameters.AddWithValue("@Mode", "Select_Product")
            da.SelectCommand.Parameters.AddWithValue("@Keyword", key)
            ' da.SelectCommand.Parameters.AddWithValue("@KeyUID", KeyID)
            da.SelectCommand.Parameters.AddWithValue("@intCurrIndex", CurrPage)
            da.SelectCommand.Parameters.AddWithValue("@intPageSize", "12")
            da.Fill(ds)
            Dim j As Integer = 0
            If (ds.Tables(0).Rows.Count > 0) Then
                str = "
"
                For i1 As Integer = 0 To Math.Ceiling(ds.Tables(0).Rows.Count / 4) - 1
                    str += "
"

                    For i As Integer = 0 To 3
                        If ds.Tables(0).Rows.Count > (i + j) Then
                            str += "
"
                        End If
                    Next
                    j = j + 4
                    str += "
"
                Next
                str += "
"
                            str += "
"
                            If Not Session("CompareProductList") Is Nothing Then
                                If CType(Session("CompareProductList"), Data.DataTable).Rows.Count >= 3 Then
                                    str += "
"
                                Else
                                    str += "
"
                                End If
                            Else
                                str += "
"
                            End If
                            str += "
"
                            str += "
"
                            str += "
"
                            str += "
  onclick='return AddToCmpLst(this);'>Add to Compare
  onclick='return AddToCmpLst(this);'>Add to Compare
  onclick='return AddToCmpLst(this);'>Add to Compare
"
                            If ds.Tables(0).Rows(j + i)("Product_MarketPrice").ToString.Trim() <> "0.00" And ds.Tables(0).Rows(j + i)("Product_MarketPrice").ToString.Trim() <> "0" And ds.Tables(0).Rows(j + i)("Product_MarketPrice").ToString.Trim() <> "Rs" And ds.Tables(0).Rows(j + i)("Product_MarketPrice").ToString.Trim() <> "0.00 Rs." And ds.Tables(0).Rows(j + i)("Product_MarketPrice").ToString.Trim() <> "" Then
                                str += "" + ds.Tables(0).Rows(j + i)("Product_MarketPrice").ToString + " " + ds.Tables(0).Rows(j + i)("Unit_Name").ToString + ""
                            Else
                            End If
                            str += "


Add to
WishList
"
                            str += "  class='U' href='" + Page.ResolveUrl("pd/" & EncodeURL(ds.Tables(0).Rows(j + i)("Product_Kid")) & "/" & ds.Tables(0).Rows(j + i)("CatName") & "/" & ds.Tables(0).Rows(j + i)("ProdName") & ".aspx") + "'Style = 'color: #163351; font-family: Verdana; font-size: 8pt; font-weight: bold;  text-transform: capitalize'>" + ds.Tables(0).Rows(j + i)("Product_Name") + ""
                            str += "
 
style='color: Black; font-family: Verdana; font-size: 7pt;'>"
+ ds.Tables(0).Rows(j + i)("ProductDescription") + "
Product Code - " + ds.Tables(0).Rows(j + i)("Product_Code").ToString + ""
                            str += "
Model No. - " + ds.Tables(0).Rows(j + i)("Product_ModelNo") + "
"
                            str += "Brand - " + ds.Tables(0).Rows(j + i)("Brand_Name") + "
"
                            str += "
"
                Return str
            Else
                Return "N"
            End If

"</span> + ds.Tables(0).Rows(j + i)(<span style="color:#A31515">"Product_Name"</span>) + <span style="color:#A31515">"        Catch ex As Exception
            Return ""
        End Try
    End Function

No comments:

ShareThis

Welcome

Welcome to Rajesh Prajapati, asp.net blog.
Here you can find some useful code and information about asp.net., c#, VB.net, SQL Server, Web Service, Web Designing etc