Private Sub DataGrid1_ItemCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles
if e.item.datasetindex>=0 then
dim Dt as new datatable
dt=ctype(datagrid1.datasource,datatable)
if not isdbnull(dt.rows(e.item.datasetindex).item(0)) then
text1.text=dt.rows(e.item.datasetindex).item(0)
text2.text=dt.rows(e.item.datasetindex).item(1)
end if
end if
end sub
Imports System
Imports System.Data
Imports System.Data.OleDb
Public Class GetCurrentClickRow
Inherits System.Web.UI.Page
Protected WithEvents Panel1 As System.Web.UI.WebControls.Panel
Protected WithEvents label1 As System.Web.UI.WebControls.Label
Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox
Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
#Region " Web Form Designer Generated Code "
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
label1.Text = "获得所点击行的例子"
label1.Font.Bold = True
Panel1.HorizontalAlign = HorizontalAlign.Center
DataGrid1.Columns(0).HeaderText = "文章标题"
DataGrid1.Columns(1).HeaderText = "发布时间"
DataGrid1.HeaderStyle.HorizontalAlign = HorizontalAlign.Center
DataGrid1.AlternatingItemStyle.BackColor = System.Drawing.Color.Ivory
DataGrid1.HorizontalAlign = HorizontalAlign.Center
Dim cnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("../aspxWeb.mdb")
Dim cn As New OleDbConnection(cnString)
cn.Open()
Dim strSQL As String = "SELECT TOP 10 Title,CreateDate FROM Document ORDER BY CreateDate DESC"
Dim cmd As New OleDbCommand(strSQL, cn)
DataGrid1.DataSource = cmd.ExecuteReader
DataGrid1.DataBind()
cn.Close()
cn.Dispose()
cn = Nothing
End Sub
Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemDataBound
If e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType = ListItemType.Item Then
e.Item.Attributes.Add("onclick", "this.style.backgroundColor='#FFCC66';alert('您点击的是:\n\n第" + e.Item.ItemIndex.ToString() + "行\n\n文章标题是:" + e.Item.Cells(0).Text.Replace(",", "\'") + "');TextBox1.value='" + e.Item.Cells(0).Text.Replace(",", "\'") + "'")
e.Item.Cells(1).Text = Format(System.Convert.ToDateTime(e.Item.Cells(1).Text.ToString()), "yyyy年M月d日")
If e.Item.Cells(0).Text.Length > 30 Then
e.Item.Attributes.Add("Title", e.Item.Cells(0).Text)
e.Item.Cells(0).Text = e.Item.Cells(0).Text.Substring(0, 28) + "…"
End If
End If
End Sub