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
Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
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