一点学习体会,留着自己看。
直接拷贝到vs 2005下面,数据绑定后就可以运行了,[T_DB_Class] 是班级表,含有4个字段,分别为:
BJID:班级编号 TID:老师编号 StID:学生编号 班级名称:BJName,其中BJID主键,TID和 StID
是外键。
<%@ Page Language="C#" %>
<script runat="server">
void CustomersGridView_RowDeleting(Object sender, GridViewDeleteEventArgs e)
{
// Cancel the delete operation if the user attempts to remove
// the last record from the GridView control.
if (GridView1.Rows.Count <= 1)
{
e.Cancel = true;
Message.Text = "You must keep at least one record.";
}
}
</script>
<html>
<body>
<form id="Form1" runat="server">
<h3>
GridView RowDeleting Example</h3>
<asp:Label ID="Message" ForeColor="Red" runat="server" />
<br />
<!-- The GridView control automatically sets the columns -->
<!-- specified in the datakeynames property as read-only. -->
<!-- No input controls are rendered for these columns in -->
<!-- edit mode. -->
<asp:GridView ID="GridView1" DataSourceID="tree1" AutoGenerateColumns="False"
AutoGenerateDeleteButton="True" DataKeyNames="BJID" OnRowDeleting="CustomersGridView_RowDeleting"
runat="server" AllowPaging="True" >
<Columns>
<asp:BoundField DataField="StID" HeaderText="StID" SortExpression="StID" />
<asp:BoundField DataField="BJID" HeaderText="BJID" ReadOnly="True" SortExpression="BJID" />
<asp:BoundField DataField="TID" HeaderText="TID" SortExpression="TID" />
<asp:BoundField DataField="BJName" HeaderText="BJName" SortExpression="BJName" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="tree1" runat="server" ConnectionString="<%$ ConnectionStrings:classDBConnectionString2 %>"
SelectCommand="SELECT * FROM [T_DB_Class]"
DeleteCommand="Delete from [T_DB_Class] where BJID = @BJID">
</asp:SqlDataSource>
<!-- This example uses Microsoft SQL Server and connects --><!-- to the Northwind sample database. Use an ASP.NET --><!-- expression to retrieve the connection string value --><!-- from the Web.config file. -->
</form>
</body>
</html>