📄 datalist.aspx
字号:
<%@ Page Debug="true" Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<head>
<title>Modify and Delete Data</title>
<script language="C#" runat="server" >
// Public variable
SqlConnection connection;
SqlCommand command;
SqlDataAdapter adapter;
SqlCommandBuilder commandBuilder;
DataTable dataTable;
private void DataBind()
{
list1.DataSource = dataTable;
list1.DataBind();
}
private void PrepareConnection()
{
string dataSource = "Data Source=localhost;";
string security = "user id=sa; password=;";
string initialCatalog = "initial catalog=pubs;";
string cnnString = dataSource + security + initialCatalog;
connection = new SqlConnection(cnnString);
// Create Data Command
string strSql = "select * from [authors]";
command = new SqlCommand(strSql, connection);
// Create Data Adapter and its commands
adapter = new SqlDataAdapter();
adapter.SelectCommand = command;
commandBuilder = new SqlCommandBuilder(adapter);
adapter.UpdateCommand = commandBuilder.GetUpdateCommand();
adapter.DeleteCommand = commandBuilder.GetDeleteCommand();
adapter.InsertCommand = commandBuilder.GetInsertCommand();
}
public void Page_Load()
{
PrepareConnection();
dataTable = new DataTable();
try
{
connection.Open();
adapter.FillSchema(dataTable, SchemaType.Mapped);
adapter.Fill(dataTable);
}
catch(SqlException e)
{
Response.Write(e.ToString());
return;
}
finally
{
connection.Close();
}
if(!IsPostBack)
DataBind();
}
void DataList_ItemCommand(object sender, DataListCommandEventArgs arg)
{
DataRow row;
switch( ((LinkButton)arg.CommandSource).CommandName )
{
case "Select":
list1.SelectedIndex = arg.Item.ItemIndex;
list1.EditItemIndex = -1;
DataBind();
break;
case "Cancel":
list1.EditItemIndex = -1;
if( ((LinkButton)arg.CommandSource).ID == "b1" )
list1.SelectedIndex = -1;
else
list1.SelectedIndex = arg.Item.ItemIndex;
DataBind();
break;
case "Edit":
list1.SelectedIndex = -1;
list1.EditItemIndex = arg.Item.ItemIndex;
DataBind();
break;
case "Update":
//Modify dataset
row = dataTable.Rows.Find(list1.DataKeys[arg.Item.ItemIndex]);
row["au_fname"] = ((TextBox)arg.Item.FindControl("txtau_fname")).Text;
row["au_lname"] = ((TextBox)arg.Item.FindControl("txtau_lname")).Text;
row["phone"] = ((TextBox)arg.Item.FindControl("txtphone")).Text;
row["city"] = ((TextBox)arg.Item.FindControl("txtcity")).Text;
// Update Database
PrepareConnection();
try
{
connection.Open();
adapter.Update(dataTable);
}
catch(SqlException e)
{
Response.Write(e.ToString());
return;
}
finally
{
connection.Close();
}
dataTable.AcceptChanges();
//Update Display
list1.EditItemIndex = -1;
list1.SelectedIndex = arg.Item.ItemIndex;
DataBind();
break;
case "Delete":
//Modify dataset
row = dataTable.Rows.Find(list1.DataKeys[arg.Item.ItemIndex]);
row.Delete();
// Update database
PrepareConnection();
try
{
connection.Open();
adapter.Update(dataTable);
}
catch(SqlException e)
{
Response.Write(e.ToString());
return;
}
finally
{
connection.Close();
}
dataTable.AcceptChanges();
//Update Display
list1.EditItemIndex = -1;
list1.SelectedIndex = -1;
DataBind();
break;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<h3>使用DataList显示数据</h3>
<asp:DataList id="list1" runat="server" DataKeyField="au_id" RepeatLayout="Flow"
OnItemCommand="DataList_ItemCommand" >
<HeaderTemplate>
<ul>作者列表
</HeaderTemplate>
<ItemTemplate>
<li><asp:LinkButton CommandName="Select" runat="server">
<%# ((DataRowView)Container.DataItem)["au_fname"] %>
<%# ((DataRowView)Container.DataItem)["au_lname"] %>
</asp:LinkButton></li>
</ItemTemplate>
<SelectedItemTemplate>
<li>
名: <%# ((DataRowView)Container.DataItem)["au_fname"] %>
姓: <%# ((DataRowView)Container.DataItem)["au_lname"] %>
城市:<%# ((DataRowView)Container.DataItem)["city"] %>
电话:<%# ((DataRowView)Container.DataItem)["phone"] %>
<br/>
<asp:LinkButton CommandName="Edit" runat="server" Text="修改" />
<asp:LinkButton CommandName="Delete" runat="server" Text="删除" />
<asp:LinkButton CommandName="Cancel" runat="server" Text="取消" id="b1"/>
</li>
</SelectedItemTemplate>
<EditItemTemplate>
<li>
名: <asp:TextBox runat="server" id="txtau_fname"
Text='<%# ((DataRowView)Container.DataItem)["au_fname"] %>' /><br/>
姓: <asp:TextBox runat="server" id="txtau_lname"
Text='<%# ((DataRowView)Container.DataItem)["au_lname"] %>' /><br/>
城市:<asp:TextBox runat="server" id="txtcity"
Text='<%# ((DataRowView)Container.DataItem)["city"] %>' /><br/>
电话:<asp:TextBox runat="server" id="txtphone"
Text='<%# ((DataRowView)Container.DataItem)["phone"] %>' /><br/>
<asp:LinkButton CommandName="Update" runat="server" Text="更新" />
<asp:LinkButton CommandName="Cancel" runat="server" Text="取消" id="b2"/>
</li>
</EditItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:DataList>
</form>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -