返回首页
当前位置: 主页 > 网络编程 > .Net实例教程 >

如何得到DataGrid中点击的当前行方法详解

时间:2011-05-31 21:20来源:betway必威官网www.etsupport.net 编辑:麦田守望者

下面的代码实现了如何得到点击的当前行的数据

GetCurrentClickRow.aspx
<%@ Page Language="vb" EnableViewState="False" AutoEventWireup="false"
Codebehind="GetCurrentClickRow.aspx.vb" Inherits="aspxWeb.mengxianhui.com.GetCurrentClickRow"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>GetCurrentClickRow</title>
<meta content="Microsoft Visual Studio.NET 7.0" name="GENERATOR">
<meta content="Visual Basic 7.0" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body style="FONT-SIZE: 9pt" MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:Panel id="Panel1" runat="server">
<asp:Label id="label1" Runat="server"></asp:Label>
<asp:DataGrid id="DataGrid1" runat="server" AutoGenerateColumns="False" CellPadding="4"
BackColor="White" BorderWidth="1px" BorderStyle="None" BorderColor="#CC9966">
<ItemStyle ForeColor="#330099" BackColor="White" Font-Size="9pt"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="#FFFFCC" BackColor="#990000" Font-Size="9pt"></HeaderStyle>
<Columns>
<asp:BoundColumn DataField="Title"></asp:BoundColumn>
<asp:BoundColumn DataField="CreateDate"></asp:BoundColumn>
</Columns>
</asp:DataGrid>
</asp:Panel>
</form>
</body>
</HTML>

GetCurrentClickRow.aspx.vb
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 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("Test.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(",", "\'") + "')")
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

End Class

 

顶一下
(1)
100%
踩一下
(0)
0%
标签(Tag):C# C#实例教程 c#基础教程 C#源代码
------分隔线----------------------------
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
验证码:点击我更换图片