Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,

I am using url rewriting with asp.net project having Ajax on page. but on post back when I am checking value of ScriptManager1.IsInAsyncPostBack and Page.IsPostBack its always false. After detail analysis I found that all values stored in ViewState lost during postback.
Code in Global.asax
VB
Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
        ' Fires at the beginning of each request
        'MsgBox(Request.ServerVariables("HTTP_HOST"))
        Dim CurrentURL_Path As String = Request.Path.ToLower
        If CurrentURL_Path.ToLower.Contains("/banner/") = True Then
            Dim mycontext As HttpContext = HttpContext.Current
            mycontext.RewritePath(Request.ApplicationPath.TrimEnd("/") & "/Image.aspx?Banner=" & CurrentURL_Path.Substring(CurrentURL_Path.LastIndexOf("/") + 1))
        ElseIf CurrentURL_Path.ToLower.Contains("/image/") = True Then
            Dim mycontext As HttpContext = HttpContext.Current
            mycontext.RewritePath(Request.ApplicationPath.TrimEnd("/") & "/Image.aspx?Image=" & CurrentURL_Path.Substring(CurrentURL_Path.LastIndexOf("/") + 1))
        ElseIf CurrentURL_Path.ToLower.Contains("/logo/") = True Then
            Dim mycontext As HttpContext = HttpContext.Current
            mycontext.RewritePath(Request.ApplicationPath.TrimEnd("/") & "/Image.aspx?Logo=" & CurrentURL_Path.Substring(CurrentURL_Path.LastIndexOf("/") + 1))
        ElseIf CurrentURL_Path.ToLower.Contains("/cat-") = True Then
            Dim mycontext As HttpContext = HttpContext.Current
            mycontext.RewritePath(Request.ApplicationPath.TrimEnd("/") & "/Default.aspx?CategoryId=" & CurrentURL_Path.Substring(CurrentURL_Path.LastIndexOf("-") + 1))
        ElseIf CurrentURL_Path.ToLower.EndsWith("sitemap.xml") Then
            Dim mycontext As HttpContext = HttpContext.Current
            mycontext.RewritePath(Request.ApplicationPath.TrimEnd("/") & "/Sitemap.aspx")
        End If
    End Sub


Code in Default.aspx.vb
If Page.IsPostBack = False Then

                dt = conn.retriveDataTable("Select a.ProductId,ProductCode,ProductName,'prod-' + Replace(CategoryName,' ','-') + '-' + Replace(ProductName,' ','-') + '-' + Cast(a.ProductId As Varchar(20)) As Url,Price,Images From ProductMaster a,CategoryMaster b,GetFirstImageProductWise c where a.ProductId=c.ProductId and a.CategoryId=b.CategoryId And b.CategoryId='" & Replace(Request.QueryString("CategoryId"), "'", "''") & "'")
                Dim pgList As New PagedDataSource
                pgList.AllowPaging = True
                pgList.PageSize = 12
                pgList.DataSource = dt.DefaultView
                pgList.CurrentPageIndex = CurrentPage
                If pgList.IsFirstPage Then
                    butPrevious.CssClass = "jp-previous jp-disabled"
                    butPrevious.Enabled = False
                Else
                    butPrevious.CssClass = "jp-previous"
                    butPrevious.Enabled = True
                End If
                If pgList.IsLastPage Then
                    butNext.CssClass = "jp-next jp-disabled"
                    butNext.Enabled = False
                Else
                    butNext.CssClass = "jp-next"
                    butNext.Enabled = True
                End If
                DataList1.DataSource = pgList
                DataList1.DataBind()
            Else
                dt = conn.retriveDataTable("Select a.ProductId,ProductCode,ProductName,'prod-' + Replace(CategoryName,' ','-') + '-' + Replace(ProductName,' ','-') + '-' + Cast(a.ProductId As Varchar(20)) As Url,Price,Images From ProductMaster a,CategoryMaster b,GetFirstImageProductWise c where a.ProductId=c.ProductId and a.CategoryId=b.CategoryId")
                Dim pgList As New PagedDataSource
                pgList.AllowPaging = True
                pgList.PageSize = 12
                pgList.DataSource = dt.DefaultView
                DataList1.DataSource = pgList
                pgList.CurrentPageIndex = CurrentPage
                If pgList.IsFirstPage Then
                    butPrevious.CssClass = "jp-previous jp-disabled"
                    butPrevious.Enabled = False
                Else
                    butPrevious.CssClass = "jp-previous"
                    butPrevious.Enabled = True
                End If
                If pgList.IsLastPage Then
                    butNext.CssClass = "jp-next jp-disabled"
                    butNext.Enabled = False
                Else
                    butNext.CssClass = "jp-next"
                    butNext.Enabled = True
                End If
                DataList1.DataBind()
            End If
            conn.CloseConn()
            conn1.CloseConn()
        End If
    End Sub


    Private Property CurrentPage As Integer
        Get
            Dim objPage As Object = ViewState("_CurrentPage")
            Dim _CurrentPage As Integer = 0
            If (objPage Is Nothing) Then
                _CurrentPage = 0
            Else
                _CurrentPage = CType(objPage, Integer)
            End If
            Return _CurrentPage
        End Get
        Set(value As Integer)
            ViewState("_CurrentPage") = value
        End Set
    End Property


    Private Sub butPrevious_Click(sender As Object, e As System.EventArgs) Handles butPrevious.Click
        If ScriptManager1.IsInAsyncPostBack = True Then
            CurrentPage = CurrentPage - 1
            If Request.QueryString("CategoryId") <> vbNullString Then
                dt = conn.retriveDataTable("Select a.ProductId,ProductCode,ProductName,'prod-' + Replace(CategoryName,' ','-') + '-' + Replace(ProductName,' ','-') + '-' + Cast(a.ProductId As Varchar(20)) As Url,Price,Images From ProductMaster a,CategoryMaster b,GetFirstImageProductWise c where a.ProductId=c.ProductId and a.CategoryId=b.CategoryId And b.CategoryId='" & Replace(Request.QueryString("CategoryId"), "'", "''") & "'")
                Dim pgList As New PagedDataSource
                pgList.AllowPaging = True
                pgList.PageSize = 12
                pgList.DataSource = dt.DefaultView
                pgList.CurrentPageIndex = CurrentPage
                If pgList.IsFirstPage Then
                    butPrevious.CssClass = "jp-previous jp-disabled"
                    butPrevious.Enabled = False
                Else
                    butPrevious.CssClass = "jp-previous"
                    butPrevious.Enabled = True
                End If
                If pgList.IsLastPage Then
                    butNext.CssClass = "jp-next jp-disabled"
                    butNext.Enabled = False
                Else
                    butNext.CssClass = "jp-next"
                    butNext.Enabled = True
                End If
                DataList1.DataSource = pgList
                DataList1.DataBind()
            Else
                dt = conn.retriveDataTable("Select a.ProductId,ProductCode,ProductName,'prod-' + Replace(CategoryName,' ','-') + '-' + Replace(ProductName,' ','-') + '-' + Cast(a.ProductId As Varchar(20)) As Url,Price,Images From ProductMaster a,CategoryMaster b,GetFirstImageProductWise c where a.ProductId=c.ProductId and a.CategoryId=b.CategoryId")
                Dim pgList As New PagedDataSource
                pgList.AllowPaging = True
                pgList.PageSize = 12
                pgList.DataSource = dt.DefaultView
                DataList1.DataSource = pgList
                pgList.CurrentPageIndex = CurrentPage
                If pgList.IsFirstPage Then
                    butPrevious.CssClass = "jp-previous jp-disabled"
                    butPrevious.Enabled = False
                Else
                    butPrevious.CssClass = "jp-previous"
                    butPrevious.Enabled = True
                End If
                If pgList.IsLastPage Then
                    butNext.CssClass = "jp-next jp-disabled"
                    butNext.Enabled = False
                Else
                    butNext.CssClass = "jp-next"
                    butNext.Enabled = True
                End If
                DataList1.DataBind()
            End If
        End If
    End Sub

    Private Sub butNext_Click(sender As Object, e As System.EventArgs) Handles butNext.Click
        If ScriptManager1.IsInAsyncPostBack = True Then
            CurrentPage = CurrentPage + 1
            If Request.QueryString("CategoryId") <> vbNullString Then
                dt = conn.retriveDataTable("Select a.ProductId,ProductCode,ProductName,'prod-' + Replace(CategoryName,' ','-') + '-' + Replace(ProductName,' ','-') + '-' + Cast(a.ProductId As Varchar(20)) As Url,Price,Images From ProductMaster a,CategoryMaster b,GetFirstImageProductWise c where a.ProductId=c.ProductId and a.CategoryId=b.CategoryId And b.CategoryId='" & Replace(Request.QueryString("CategoryId"), "'", "''") & "'")
                Dim pgList As New PagedDataSource
                pgList.AllowPaging = True
                pgList.PageSize = 12
                pgList.DataSource = dt.DefaultView
                pgList.CurrentPageIndex = CurrentPage
                If pgList.IsFirstPage Then
                    butPrevious.CssClass = "jp-previous jp-disabled"
                    butPrevious.Enabled = False
                Else
                    butPrevious.CssClass = "jp-previous"
                    butPrevious.Enabled = True
                End If
                If pgList.IsLastPage Then
                    butNext.CssClass = "jp-next jp-disabled"
                    butNext.Enabled = False
                Else
                    butNext.CssClass = "jp-next"
                    butNext.Enabled = True
                End If
                DataList1.DataSource = pgList
                DataList1.DataBind()
            Else
                dt = conn.retriveDataTable("Select a.ProductId,ProductCode,ProductName,'prod-' + Replace(CategoryName,' ','-') + '-' + Replace(ProductName,' ','-') + '-' + Cast(a.ProductId As Varchar(20)) As Url,Price,Images From ProductMaster a,CategoryMaster b,GetFirstImageProductWise c where a.ProductId=c.ProductId and a.CategoryId=b.CategoryId")
                Dim pgList As New PagedDataSource
                pgList.AllowPaging = True
                pgList.PageSize = 12
                pgList.DataSource = dt.DefaultView
                DataList1.DataSource = pgList
                pgList.CurrentPageIndex = CurrentPage
                If pgList.IsFirstPage Then
                    butPrevious.CssClass = "jp-previous jp-disabled"
                    butPrevious.Enabled = False
                Else
                    butPrevious.CssClass = "jp-previous"
                    butPrevious.Enabled = True
                End If
                If pgList.IsLastPage Then
                    butNext.CssClass = "jp-next jp-disabled"
                    butNext.Enabled = False
                Else
                    butNext.CssClass = "jp-next"
                    butNext.Enabled = True
                End If
                DataList1.DataBind()
            End If
        End If
    End Sub

    Private Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        form1.Action = Request.RawUrl
    End Sub


I had tried solution provided in below url but not working for me.
http://weblogs.asp.net/scottgu/archive/2007/02/26/tip-trick-url-rewriting-with-asp-net.aspx

If anyone understand what problem exist in my code please let me know.
Thanks in advance
Posted
Updated 27-Mar-13 6:01am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900