📄 trash.aspx
字号:
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<head>
<title>DataView RowStateFilter Demo</title>
<script language="C#" runat="server" >
// Public variables
DataTable dataTable;
private void BindGrid()
{
DataView dataView1 = new DataView(dataTable);
DataView dataView2 = new DataView(dataTable);
dataView1.RowStateFilter = DataViewRowState.CurrentRows;
dataView2.RowStateFilter = DataViewRowState.Deleted;
grid1.DataSource=dataView1;
grid2.DataSource=dataView2;
grid1.DataBind();
grid2.DataBind();
}
public void Page_Load()
{
if(!IsPostBack)
{
string dataSource = "Data Source=localhost;";
string security = "user id=sa; password=;";
string initialCatalog = "initial catalog=pubs;";
string cnnString = dataSource + security + initialCatalog;
SqlConnection connection = new SqlConnection(cnnString);
string strSql = "select au_id,au_fname,au_lname,phone,state from [authors]";
SqlCommand command = new SqlCommand(strSql, connection);
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = command;
dataTable = new DataTable();
try
{
connection.Open();
adapter.FillSchema(dataTable, SchemaType.Mapped);
adapter.Fill(dataTable);
}
catch(SqlException e)
{
Response.Write(e.ToString());
}
finally
{
connection.Close();
}
Session["dataTable"] = dataTable;
BindGrid();
}
else
dataTable = (DataTable)Session["dataTable"];
}
void OnSubmit(object sender, EventArgs arg)
{
string dataSource = "Data Source=localhost;";
string security = "user id=sa; password=;";
string initialCatalog = "initial catalog=pubs;";
string cnnString = dataSource + security + initialCatalog;
SqlConnection connection = new SqlConnection(cnnString);
string strSql = "select * from [authors]";
SqlCommand command = new SqlCommand(strSql, connection);
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = command;
SqlCommandBuilder cb = new SqlCommandBuilder(adapter);
adapter.UpdateCommand = cb.GetUpdateCommand();
adapter.DeleteCommand = cb.GetDeleteCommand();
adapter.InsertCommand = cb.GetInsertCommand();
try
{
connection.Open();
adapter.Update(dataTable);
}
catch(SqlException e)
{
Response.Write(e.ToString());
}
finally
{
connection.Close();
}
dataTable.AcceptChanges();
Session["dataTable"] = dataTable;
BindGrid();
}
void OnReset(object sender, EventArgs arg)
{
dataTable.RejectChanges();
Session["dataTable"] = dataTable;
BindGrid();
}
void OnItemCommand(object sender, DataGridCommandEventArgs arg)
{
DataRow row = dataTable.Rows.Find( grid1.DataKeys[arg.Item.ItemIndex] );
row.Delete();
Session["dataTable"] = dataTable;
BindGrid();
}
</script>
</head>
<body>
<form id="form1" runat="server">
<h3>DataView RowStateFilter Demo</h3>
<asp:DataGrid id="grid1" runat="server" OnItemCommand="OnItemCommand"
DataKeyField="au_id" >
<HeaderStyle BackColor="lightblue" Font-Bold="true" />
<ItemStyle BackColor="lightyellow"/>
<Columns>
<asp:ButtonColumn HeaderText="command" Text="删除" CommandName="Remove"/>
</Columns>
</asp:DataGrid>
<asp:DataGrid id="grid2" runat="server" OnItemCommand="OnItemCommand"
DataKeyField="au_id" >
<HeaderStyle BackColor="lightblue" Font-Bold="true" />
<ItemStyle ForeColor="red"/>
</asp:DataGrid><hr/>
<asp:Button id="Button1" runat="server" Text="提交" OnClick="OnSubmit"/>
<asp:Button id="Button2" runat="server" Text="复位" OnClick="OnReset"/>
</form>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -