View Full Version : Please help:Hyperlinkcolumn in datagrid
justnew
04-12-2005, 08:23 PM
Hi All
Can somebody help me. I have a datagrid with hyperlink-column. the aim of this column is to retrieve the information about the product in the datagrid based on the ProductID. That works great. The problem I am having is that the datagrid have pages 1 2 3 4 now when I am in page 2 or 3 and I click on the hyperlink-column to display the information, it display correctly as well but it will automatically takes me to the first page that's page 1.
Any help welcome
kwilson81
04-14-2005, 05:11 PM
Are you using the PageDataSource (System.Web.UI.WebControls) object to bind your datagrid to?
If so, you can store your current page to a session variable or in the viewstate and use that to set the current page when the page reloads.
Also, are you re-binding your datagrid each time the page loads? If you keep the viewstate switched on for it, you'll maybe only need to bind on the first load (using Not IsPostback in your page load). Just be careful the viewstate doesn't grow too big.
Hope that's of some help.
justnew
04-14-2005, 05:34 PM
Thank you kwilson81 for the reply. Here is my comeplete Code
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Not Page.IsPostBack Then
BindSQL()
End If
ProductInfo()
Sub ProductInfo()
Dim connString As String = "Server="Server=mysqlserver; Initial Catalog=mydb; UID=myUID; Password=mypwd; Network Library=dbmssocn;"
ProductId = Request.QueryString("ProductId")
ds = GetProductID(ProductId)
'BindSQL()
If ds.Tables(0).Rows.Count > 0 Then
datarow = ds.Tables(0).Rows(0)
lblfname.Text =datarow .Item(0)
lblphone.Text = datarow .Item(1)
lbltown.Text = datarow .Item(3)
lblcomputer.Text = datarow .Item(4)
lbldescription.Text = datarow .Item(6)
lblprice.Text = datarow .Item(7)
End If
End Sub
Sub Repage(ByVal sender As Object, ByVal e As EventArgs)
dgImages.CurrentPageIndex = 0
BindSQL()
End Sub
Sub BindSQL()
Dim MyConnection As SqlConnection
Dim DS As DataSet
Dim MyCommand As SqlDataAdapter
Dim sqlStr As String = "SELECT Product.CategoryID, Product.Product,Product.ProductID, Product.SellerID, Product.Price, Product.Descriptions,Product_Image.ImageID,Product_Image.ProductID, Product_Image.LGimgname, Product_Image.LGimgdata,Product_Image.LGimgcontenttype, Product_Image.SMimgname, Product_Image.SMimgdata, Product_Image.SMimgcontenttype, Product_Image.UploadDate FROM Product INNER JOIN Product_Image ON Product.ProductID = Product_Image.ProductID where CategoryID = 2 Order by Price asc"
Dim strConn As String = "Server="Server=mysqlserver; Initial Catalog=mydb; UID=myUID; Password=mypwd; Network Library=dbmssocn;"
Dim RcdCount As Integer
'Open up our connection with our connection object
MyConnection = New SqlConnection(strConn)
'To execute our Sql Statement and provide out active connection
MyCommand = New SqlDataAdapter(sqlStr, MyConnection)
'Create instance of dataset object and fill our predetermined datagrid with it and we name it
DS = New DataSet
MyCommand.Fill(DS, "Product")
RcdCount = DS.Tables("Product").Rows.Count.ToString()
'dgImages.PageSize = CInt(ps.SelectedItem.Value)
If Not Page.IsPostBack Then
dgImages.CurrentPageIndex = 0
End If
ResultCount = RcdCount
RecordCount.Text = "<b><font color=red>" & RcdCount & "</font> records found"
'Now we assign the dataview to the datasource of the datagrid and we send it right on it
Try
dgImages.DataSource = DS
dgImages.DataBind()
Catch e As Exception
dgImages.CurrentPageIndex = 0
End Try
lblPageCount.Text = "Page " & dgImages.CurrentPageIndex + 1 & " of " & dgImages.PageCount
End Sub
Private Sub dgImages_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles dgImages.PageIndexChanged
dgImages.CurrentPageIndex = e.NewPageIndex
BindSQL()
End Sub
Sub ShowStats()
lblCurrentIndex.Text = "CurrentPageIndex is " & dgImages.CurrentPageIndex
lblPageCount.Text = "PageCount is " & dgImages.PageCount
End Sub
Function FormatURL(ByVal strArgument) As String
Return ("ReadImages.aspx?id=" & strArgument)
End Function
Public Function GetProductID(ByVal ProductId As Integer) As DataSet
Dim sql As String
sql = "exec ProductSellerInfo " & ProductId
Dim connString As String = "Server=mysqlserver; Initial Catalog=mydb; UID=myUID; Password=mypwd; Network Library=dbmssocn;"
Dim conn As SqlConnection = New SqlConnection(connString)
Dim da As SqlDataAdapter = New SqlDataAdapter(sql, conn)
Dim ds As DataSet = New DataSet
conn.Open()
Try
da.Fill(ds, "Product")
Return ds
Catch e As SqlException
Throw (e)
Finally
'rydd opp
conn.Close()
End Try
End Function
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.