前台
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="sid"
DataSourceID="SqlDataSource1" Xonrowcommand="GridView1_RowCommand"
Xonrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="sid" HeaderText="学号" InsertVisible="False"
ReadOnly="True" SortExpression="sid" />
<asp:HyperLinkField DataTextField="sname" HeaderText="姓名" />
<asp:BoundField DataField="classid" HeaderText="班号" SortExpression="classid" />
<asp:BoundField DataField="sex" HeaderText="性别" SortExpression="sex" />
<asp:BoundField DataField="age" HeaderText="年龄" SortExpression="age" />
<asp:CheckBoxField DataField="isking" HeaderText="班长" SortExpression="isking" />
<asp:ButtonField HeaderText="删除" Text="删除" />
<asp:ButtonField CommandName="initialPassword" Text="初始化密码" />
</Columns>
</asp:GridView>
<br />
<br />
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConflictDetection="CompareAllValues"
ConnectionString="<%$ ConnectionStrings:studentConnectionString %>"
DeleteCommand="DELETE FROM [student] WHERE [sid] = @original_sid AND [sname] = @original_sname AND (([classid] = @original_classid) OR ([classid] IS NULL AND @original_classid IS NULL)) AND (([sex] = @original_sex) OR ([sex] IS NULL AND @original_sex IS NULL)) AND (([age] = @original_age) OR ([age] IS NULL AND @original_age IS NULL)) AND [isking] = @original_isking"
InsertCommand="INSERT INTO [student] ([sname], [classid], [sex], [age], [isking]) VALUES (@sname, @classid, @sex, @age, @isking)"
OldValuesParameterFormatString="original_{0}"
SelectCommand="SELECT [sid], [sname], [classid], [sex], [age], [isking] FROM [student]"
UpdateCommand="UPDATE [student] SET [sname] = @sname, [classid] = @classid, [sex] = @sex, [age] = @age, [isking] = @isking WHERE [sid] = @original_sid AND [sname] = @original_sname AND (([classid] = @original_classid) OR ([classid] IS NULL AND @original_classid IS NULL)) AND (([sex] = @original_sex) OR ([sex] IS NULL AND @original_sex IS NULL)) AND (([age] = @original_age) OR ([age] IS NULL AND @original_age IS NULL)) AND [isking] = @original_isking">
<DeleteParameters>
<asp:Parameter Name="original_sid" Type="Int32" />
<asp:Parameter Name="original_sname" Type="String" />
<asp:Parameter Name="original_classid" Type="Int32" />
<asp:Parameter Name="original_sex" Type="String" />
<asp:Parameter Name="original_age" Type="Byte" />
<asp:Parameter Name="original_isking" Type="Boolean" />
</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" />
</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="original_sid" Type="Int32" />
<asp:Parameter Name="original_sname" Type="String" />
<asp:Parameter Name="original_classid" Type="Int32" />
<asp:Parameter Name="original_sex" Type="String" />
<asp:Parameter Name="original_age" Type="Byte" />
<asp:Parameter Name="original_isking" Type="Boolean" />
</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_5.删除列
{
public partial class CLASS : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//处理已经完成的数据
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+"的吗?')");
}
}
}
}
2 使用模版列完成课上例子的实现,用一列显示 全部信息,同时完成修改的功能
前台
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" CellPadding="4"
DataKeyNames="sid" DataSourceID="SqlDataSource1" ForeColor="#333333"
GridLines="None" Xonrowcommand="GridView1_RowCommand"
Xonrowdatabound="GridView1_RowDataBound">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:BoundField DataField="sid" HeaderText="学号" InsertVisible="False"
ReadOnly="True" SortExpression="sid" />
<asp:HyperLinkField DataNavigateUrlFields="photo" DataTextField="sname"
DataTextFormatString="点击查看{0}" HeaderText="姓名"
NavigateUrl="~/GridView/img.aspx" Target="_blank" />
<asp:BoundField DataField="classid" HeaderText="班号" SortExpression="classid" />
<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='img.aspx?photourl=<%#Eval("photo") %> ' target="_blank">姓名:<%#Eval("sname") %></a></td>
</tr>
<tr>
<td>
<asp:Image ID="Image2" 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="初始化密码" />
<asp:CommandField HeaderText="选项" ShowDeleteButton="True" ShowEditButton="True"
ShowSelectButton="True" />
</Columns>
<EditRowStyle BackColor="#999999" />
<EmptyDataTemplate>
<table style="width:100%;">
<tr>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
</table>
</EmptyDataTemplate>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
</div>
</form>
</body>
</html>
后台
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//处理已经完成的数据
if(e.Row.RowType!=DataControlRowType.DataRow)
{
return;
}
LinkButton lbtn = e.Row.Cells[6].Controls[0] as LinkButton;
if (lbtn != null)
{
lbtn.Attributes.Add("onclick", "return confirm('确定要删除吗?')");
}
}
}
前台
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Image ID="Image1" runat="server"
ImageUrl="" />
</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_5.GridView
{
public partial class img : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.Image1.ImageUrl = Request["photourl"];
}
}
}