I have a page which has only a datagrid that is populated from a stored procedure. The grid only has one column which is "FileName", basically the names of files. I have set the grid up to populate correctly and set the mouseover so it will highlight the row but I need to store the specific file the user clicks on. Whatever filename they choose I need to store and open a page that will then use that string value. I will include my code just in case. Thanks in advance!
Code:
Dim dgCache As DataView
Dim dv As DataView
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
'Check if ViewSate is already populated
If IsNothing(ViewState("SortOrder")) Then
'Assign default column sort order
GetDataReader("FileName asc")
Else
'Else retrieve sort order from ViewState
GetDataReader(ViewState("SortOrder").ToString())
End If
End If
End Sub
Sub GetDataReader(ByVal ColumnOrder As String)
'Assign viewstate to ColumnOrder
ViewState("SortOrder") = ColumnOrder
'Set up Cache Object and determine if it exists
dgCache = CType(Cache.Get("dgCache" & ColumnOrder), DataView)
If (dgCache Is Nothing) Then
'Connection Objects
Dim sql As String = "getreports"
Dim strConn As String = "server=(local);uid=Reports;pwd=;database=DP;"
Dim cn As New SqlConnection(strConn)
cn.Open()
Dim cmd As New SqlCommand(sql, cn)
Dim dr As SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
Dim dt As New DataTable("Reports")
dt.Columns.Add(New DataColumn("FileName"))
While dr.Read
Dim drow As DataRow = dt.NewRow
With drow
drow(0) = dr("Description")
End With
dt.Rows.Add(drow)
End While
'close all connections
dr.Close()
cmd.Dispose()
cn.Dispose()
'create a dataview and set it to datatable
Dim dv As DataView = dt.DefaultView
'Assign column sort order
dv.Sort = ColumnOrder
Cache.Insert("dgCache", dv)
testGrid1.DataSource = dv
Else
'Assign Cached DataView new sort order
dgCache.Sort = ViewState("SortOrder").ToString()
'Set webgrid's DataSource to cached object
testGrid1.DataSource = dgCache
End If
testGrid1.DataBind()
addAttrib()
Dim s As Object = testGrid1.SelectedItem
End Sub
Function SortOrder(ByVal Field As String) As String
If Field = ViewState("SortOrder").ToString() Then
SortOrder = Replace(Field, "asc", "desc")
Else
SortOrder = Replace(Field, "desc", "asc")
End If
End Function
Private Sub addAttrib()
Dim strColor As String = "white"
Dim UserAgent As String = Request.ServerVariables("HTTP_USER_AGENT")
For Each i As DataGridItem In testGrid1.Items
i.Attributes.Add("onmouseover", "style.backgroundColor='yellow';style.cursor='hand';")
i.Attributes.Add("onmouseout", "style.backgroundColor='" & strColor & "';style.cursor='default';")
'If InStr(1, UserAgent, "Macintosh") > 0 Or InStr(1, UserAgent, "Mac_PowerPC") > 0 Then
' i.Attributes.Add("onclick", "window.open('/reports/" & Request.QueryString("coid") & "/" & dv.Table.Rows(i.DataSetIndex)("FileName") & ".rpt?init=html_frame&apsuser=administrator&apspassword=&apsauthtype=secenterprise')")
'Else
' i.Attributes.Add("onclick", "style.color='purple';window.open('/reports/" & Request.QueryString("coid") & "/" & dv.Table.Rows(i.DataSetIndex)("FileName") & ".rpt?init=actx&apsuser=administrator&apspassword=&apsauthtype=secenterprise')")
'End If
If strColor = "white" Then
strColor = "#cccccc"
Else
strColor = "white"
End If
Next
End Sub