前台:
<body>
<form id="form1" runat="server">
<div>
<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>
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1"
AutoGenerateColumns="False" BackColor="LightGoldenrodYellow" BorderColor="Tan"
BorderWidth="1px" CellPadding="2" DataKeyNames="sid" ForeColor="Black"
GridLines="None" Height="202px" Xonrowcommand="GridView1_RowCommand"
Xonrowdatabound="GridView1_RowDataBound" style="margin-right: 0px" Width="602px">
<AlternatingRowStyle BackColor="PaleGoldenrod" />
<Columns>
<asp:BoundField DataField="sid" HeaderText="编号" InsertVisible="False"
ReadOnly="True" SortExpression="sid" />
<asp:BoundField DataField="sname" HeaderText="姓名" SortExpression="sname" />
<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:ImageField DataImageUrlField="photo" DataImageUrlFormatString="{0}"
HeaderText="照片">
</asp:ImageField>
<asp:ButtonField CausesValidation="True" CommandName="delete" HeaderText="删除"
Text="删除" />
</div>
</form>
</body>
后台:
public partial class WebForm1 : 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 id = e.Row.Cells[0].Text;
string name = e.Row.Cells[1].Text;
LinkButton btn1 = e.Row.Cells[7].Controls[0] as LinkButton;
if (btn1 != null)
{
btn1.Attributes.Add("onclick","return confirm('确信要删除编号为:"+id+"的"+name+"吗?')");
}
}
}