📄 datalistedit.aspx
字号:
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<script runat=server>
void Page_Load(Object sender , EventArgs e)
{
if (! IsPostBack ) {
BindDataList();
}
}
void BindDataList() {
SqlConnection conPubs;
SqlCommand cmdSelect;
SqlDataReader dtrAuthors;
conPubs = new SqlConnection( @"Server=localhost;Integrated Security=SSPI;Database=Pubs" );
cmdSelect = new SqlCommand( "Select au_id, au_lname, phone From Authors Order by au_lname", conPubs );
conPubs.Open();
dtrAuthors = cmdSelect.ExecuteReader();
dlstAuthors.DataSource = dtrAuthors;
dlstAuthors.DataBind();
dtrAuthors.Close();
conPubs.Close();
}
void dlstAuthors_EditCommand( object s, DataListCommandEventArgs e ) {
dlstAuthors.EditItemIndex = e.Item.ItemIndex;
BindDataList();
}
void dlstAuthors_CancelCommand( object s, DataListCommandEventArgs e ) {
dlstAuthors.EditItemIndex = -1;
BindDataList();
}
void dlstAuthors_DeleteCommand( object s, DataListCommandEventArgs e ) {
SqlConnection conPubs;
string strDelete;
SqlCommand cmdDelete;
string strAuthorID;
strAuthorID = dlstAuthors.DataKeys[(int)e.Item.ItemIndex].ToString();
conPubs = new SqlConnection( @"Server=localhost;Integrated Security=SSPI;Database=Pubs" );
strDelete = "Delete Authors Where au_id=@authorID";
cmdDelete = new SqlCommand( strDelete, conPubs );
cmdDelete.Parameters.Add( "@authorID", strAuthorID );
conPubs.Open();
cmdDelete.ExecuteNonQuery();
conPubs.Close();
dlstAuthors.EditItemIndex = -1;
BindDataList();
}
void dlstAuthors_UpdateCommand( object s, DataListCommandEventArgs e ) {
SqlConnection conPubs;
string strUpdate;
SqlCommand cmdUpdate;
string strAuthorID;
strAuthorID = dlstAuthors.DataKeys[(int)e.Item.ItemIndex].ToString();
TextBox txtLastName = (TextBox)e.Item.FindControl( "txtLastName" );
TextBox txtPhone = (TextBox)e.Item.FindControl( "txtPhone" );
conPubs = new SqlConnection( @"Server=localhost;Integrated Security=SSPI;Database=Pubs" );
strUpdate = "Update Authors set au_lname=@lastname, phone=@phone Where au_id=@authorID";
cmdUpdate = new SqlCommand( strUpdate, conPubs );
cmdUpdate.Parameters.Add( "@authorID", strAuthorID );
cmdUpdate.Parameters.Add( "@lastname", txtLastName.Text );
cmdUpdate.Parameters.Add( "@phone", txtPhone.Text );
conPubs.Open();
cmdUpdate.ExecuteNonQuery();
conPubs.Close();
dlstAuthors.EditItemIndex = -1;
BindDataList();
}
</Script>
<html>
<head><title>DataListEdit.aspx</title></head>
<body>
<form Runat="Server">
<asp:DataList
ID="dlstAuthors"
DataKeyField="au_id"
OnEditCommand="dlstAuthors_EditCommand"
OnCancelCommand="dlstAuthors_CancelCommand"
OnDeleteCommand="dlstAuthors_DeleteCommand"
OnUpdateCommand="dlstAuthors_UpdateCommand"
RepeatColumns="4"
GridLines="Both"
CellPadding="10"
EditItemStyle-BackColor="lightgrey"
Runat="Server">
<ItemTemplate>
<%#DataBinder.Eval(Container.DataItem, "au_lname" )%>
- <%#DataBinder.Eval(Container.DataItem, "phone" )%>
<br>
<asp:LinkButton
Text="Edit!"
CommandName="edit"
Runat="Server" />
</ItemTemplate>
<EditItemTemplate>
<b>Last Name:</b>
<br>
<asp:TextBox
ID="txtLastName"
Text='<%#DataBinder.Eval(Container.DataItem, "au_lname" )%>'
Runat="Server" />
<p>
<b>Phone:</b>
<br>
<asp:TextBox
ID="txtPhone"
Text='<%#DataBinder.Eval(Container.DataItem, "phone" )%>'
Runat="Server" />
<p>
<asp:LinkButton
Text="Update!"
CommandName="update"
Runat="Server" />
<asp:LinkButton
Text="Delete!"
CommandName="delete"
Runat="Server" />
<asp:LinkButton
Text="Cancel!"
CommandName="cancel"
Runat="Server" />
</EditItemTemplate>
</asp:DataList>
</form>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -