前台代码:
<%@ Page Xlanguage="C#" AutoEventWireup="true" CodeBehind="GradView1.aspx.cs" Inherits="_12_05GridView.GradView1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.style10
{
height: 17px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" CellPadding="4"
DataKeyNames="sid" DataSourceID="SqlDataSource1" EmptyDataText="没有数据"
Font-Bold="True" Font-Size="Small" ForeColor="#333333" GridLines="None"
Height="40px" ShowFooter="True" Width="712px"
Xonrowcommand="GridView1_RowCommand" Xonrowdatabound="GridView1_RowDataBound"
style="margin-right: 81px">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="sid" HeaderText="编号" InsertVisible="False"
ReadOnly="True" SortExpression="sid" />
<asp:HyperLinkField DataNavigateUrlFields="photo"
DataNavigateUrlFormatString="viewPhoto.aspx?photourl={0}" DataTextField="sname"
DataTextFormatString="点击查看{0}" HeaderText="姓名" Target="_blank"
NavigateUrl="~/viewPhoto.aspx" />
<asp:BoundField DataField="sex" HeaderText="性别" SortExpression="sex" />
<asp:BoundField DataField="age" HeaderText="年龄" SortExpression="age" />
<asp:CheckBoxField DataField="isking" HeaderText="是否班长"
SortExpression="isking" />
<asp:TemplateField HeaderText="照片">
<ItemTemplate>
<table style="width:100%;">
<tr>
<td><a href='viewPhoto.aspx?photourl=<%#Eval("photo") %>' target="_blank">姓名:<%#Eval("sname") %></a></td>
</tr>
<tr>
<td class="style10">
<asp:Image ID="Image1" runat="server" ImageUrl='<%#Eval("photo") %>' Width="60px" Height="60px" ToolTip='<%#"这是:"+Eval("sname")%>'/>
</td>
</tr>
<tr>
<td>编号:<%#Eval("sid") %> 年龄:<%#Eval("age") %> </td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
<asp:ButtonField HeaderText="删除" Text="删除" />
<asp:ButtonField CommandName="initialPassword" Text="初始化密码" />
</Columns>
<EditRowStyle BackColor="#7C6F57" />
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center"
VerticalAlign="Middle" />
<RowStyle BackColor="#E3EAEB" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F8FAFA" />
<SortedAscendingHeaderStyle BackColor="#246B61" />
<SortedDescendingCellStyle BackColor="#D4DFE1" />
<SortedDescendingHeaderStyle BackColor="#15524A" />
</asp:GridView>
<div>
<br />
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:studentConnectionString %>"
DeleteCommand="DELETE FROM [student] WHERE [sid] = @sid"
InsertCommand="INSERT INTO [student] ([sname], [classid], [sex], [age], [isking], [photo]) VALUES (@sname, @classid, @sex, @age, @isking, @photo)"
SelectCommand="SELECT [sid], [sname], [classid], [sex], [age], [isking], [photo] FROM [student]"
UpdateCommand="UPDATE [student] SET [sname] = @sname, [classid] = @classid, [sex] = @sex, [age] = @age, [isking] = @isking, [photo] = @photo WHERE [sid] = @sid">
<DeleteParameters>
<asp:Parameter Name="sid" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="sname" Type="String" />
<asp:Parameter Name="classid" Type="Int32" />
<asp:Parameter Name="sex" Type="String" />
<asp:Parameter Name="age" Type="Byte" />
<asp:Parameter Name="isking" Type="Boolean" />
<asp:Parameter Name="photo" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="sname" Type="String" />
<asp:Parameter Name="classid" Type="Int32" />
<asp:Parameter Name="sex" Type="String" />
<asp:Parameter Name="age" Type="Byte" />
<asp:Parameter Name="isking" Type="Boolean" />
<asp:Parameter Name="photo" Type="String" />
<asp:Parameter Name="sid" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
</div>
</form>
</body>
</html>
后台代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace _12_05GridView
{
public partial class GradView1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "initialPassword")//获取用户是点击了哪个按钮
{
//在这里写初始化的密码代码
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//可以在这个事件中处理已经完成的数据
//LinkButton lbtn = e.Row.Cells[6].Controls[0] as LinkButton;
if (e.Row.RowType != DataControlRowType.DataRow)
{
return;
}
string sid=e.Row.Cells[0].Text;
HyperLink link = e.Row.Cells[1].Controls[0] as HyperLink;
LinkButton lbtn = e.Row.Cells[6].Controls[0] as LinkButton;
if (lbtn != null)
{
lbtn.Attributes.Add("onclick","return confirm('确定要删除编号为"+sid+"的"+link.Text+"吗?');");
}
}
}
}
2 使用模版列完成课上例子的实现,用一列显示 全部信息,同时完成修改的功能
<%@ Page Xlanguage="C#" AutoEventWireup="true" CodeBehind="GradView1.aspx.cs" Inherits="_12_05GridView.GradView1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.style10
{
height: 17px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" CellPadding="4"
DataKeyNames="sid" DataSourceID="SqlDataSource1" EmptyDataText="没有数据"
Font-Bold="True" Font-Size="Small" ForeColor="#333333" GridLines="None"
Height="40px" ShowFooter="True" Width="712px"
Xonrowcommand="GridView1_RowCommand" Xonrowdatabound="GridView1_RowDataBound"
style="margin-right: 81px">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="sid" HeaderText="编号" InsertVisible="False"
ReadOnly="True" SortExpression="sid" />
<asp:HyperLinkField DataNavigateUrlFields="photo"
DataNavigateUrlFormatString="viewPhoto.aspx?photourl={0}" DataTextField="sname"
DataTextFormatString="点击查看{0}" HeaderText="姓名" Target="_blank"
NavigateUrl="~/viewPhoto.aspx" />
<asp:BoundField DataField="sex" HeaderText="性别" SortExpression="sex" />
<asp:BoundField DataField="age" HeaderText="年龄" SortExpression="age" />
<asp:CheckBoxField DataField="isking" HeaderText="是否班长"
SortExpression="isking" />
<asp:TemplateField HeaderText="照片">
<ItemTemplate>
<table style="width:100%;">
<tr>
<td><a href='viewPhoto.aspx?photourl=<%#Eval("photo") %>' target="_blank">姓名:<%#Eval("sname") %></a></td>
</tr>
<tr>
<td class="style10">
<asp:Image ID="Image1" runat="server" ImageUrl='<%#Eval("photo") %>' Width="60px" Height="60px" ToolTip='<%#"这是:"+Eval("sname")%>'/>
</td>
</tr>
<tr>
<td>编号:<%#Eval("sid") %> 年龄:<%#Eval("age") %> </td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
<asp:ButtonField HeaderText="删除" Text="删除" />
<asp:ButtonField CommandName="initialPassword" Text="初始化密码" />
</Columns>
<EditRowStyle BackColor="#7C6F57" />
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center"
VerticalAlign="Middle" />
<RowStyle BackColor="#E3EAEB" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F8FAFA" />
<SortedAscendingHeaderStyle BackColor="#246B61" />
<SortedDescendingCellStyle BackColor="#D4DFE1" />
<SortedDescendingHeaderStyle BackColor="#15524A" />
</asp:GridView>
<div>
<br />
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:studentConnectionString %>"
DeleteCommand="DELETE FROM [student] WHERE [sid] = @sid"
InsertCommand="INSERT INTO [student] ([sname], [classid], [sex], [age], [isking], [photo]) VALUES (@sname, @classid, @sex, @age, @isking, @photo)"
SelectCommand="SELECT [sid], [sname], [classid], [sex], [age], [isking], [photo] FROM [student]"
UpdateCommand="UPDATE [student] SET [sname] = @sname, [classid] = @classid, [sex] = @sex, [age] = @age, [isking] = @isking, [photo] = @photo WHERE [sid] = @sid">
<DeleteParameters>
<asp:Parameter Name="sid" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="sname" Type="String" />
<asp:Parameter Name="classid" Type="Int32" />
<asp:Parameter Name="sex" Type="String" />
<asp:Parameter Name="age" Type="Byte" />
<asp:Parameter Name="isking" Type="Boolean" />
<asp:Parameter Name="photo" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="sname" Type="String" />
<asp:Parameter Name="classid" Type="Int32" />
<asp:Parameter Name="sex" Type="String" />
<asp:Parameter Name="age" Type="Byte" />
<asp:Parameter Name="isking" Type="Boolean" />
<asp:Parameter Name="photo" Type="String" />
<asp:Parameter Name="sid" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
</div>
</form>
</body>
</html>
后台代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace _12_05GridView
{
public partial class GradView1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "initialPassword")//获取用户是点击了哪个按钮
{
//在这里写初始化的密码代码
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//可以在这个事件中处理已经完成的数据
//LinkButton lbtn = e.Row.Cells[6].Controls[0] as LinkButton;
if (e.Row.RowType != DataControlRowType.DataRow)
{
return;
}
string sid=e.Row.Cells[0].Text;
HyperLink link = e.Row.Cells[1].Controls[0] as HyperLink;
LinkButton lbtn = e.Row.Cells[6].Controls[0] as LinkButton;
if (lbtn != null)
{
lbtn.Attributes.Add("onclick","return confirm('确定要删除编号为"+sid+"的"+link.Text+"吗?');");
}
}
}
}