📄 select-edit-datalist.aspx
字号:
<%@Page Language="VB"%>
<%@Import Namespace="System.Data" %>
<%@Import Namespace="System.Data.OleDb" %>
<%@ Register TagPrefix="wrox" TagName="connect" Src="..\global\connect-strings.ascx" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head>
<title>Selecting and Editing Data in a DataList Control</title>
<style type="text/css">
body, td {font-family:Tahoma,Arial,sans-serif; font-size:10pt}
input {font-family:Tahoma,Arial,sans-serif; font-size:8pt}
.heading {font-family:Tahoma,Arial,sans-serif; font-size:14pt; font-weight:bold}
.subhead {font-family:Tahoma,Arial,sans-serif; font-size:12pt; font-weight:bold; padding-bottom:5px}
.cite {font-family:Tahoma,Arial,sans-serif; font-size:8pt}
.rHead {font-family:Lucida Handwriting,Comic Sans MS,Tahoma,Arial;
font-size:14pt; font-weight:bold; padding:8px; color:green}
.rItem {font-family:Lucida Handwriting,Comic Sans MS,Tahoma,Arial,sans-serif;
font-size:10pt}
.rFoot {font-family:Tahoma,Arial; font-size:8pt; padding:8px; color:darkgray}
</style></head>
<body bgcolor="#ffffff">
<span class="heading">Selecting and Editing Data in a DataList Control</span><hr />
<!--------------------------------------------------------------------------->
<%'-- insert connection string script --%>
<wrox:connect id="ctlConnectStrings" runat="server" />
<div id="outError" runat="server" />
<ASP:Label id="lblSQL" runat="server" /><p />
<form runat="server">
<ASP:DataList id="MyDataList" runat="server"
CellSpacing = "2"
SelectedItemStyle-BackColor="red"
SelectedItemStyle-ForeColor="white"
EditItemStyle-BackColor="yellow"
EditItemStyle-ForeColor="black"
DataKeyField="ISBN"
OnItemCommand="DoItemSelect"
OnEditCommand="DoItemEdit"
OnUpdateCommand="DoItemUpdate"
OnDeleteCommand="DoItemDelete"
OnCancelCommand="DoItemCancel">
<HeaderTemplate>
<b>Some Wrox Press Books:</b><br />
</HeaderTemplate>
<ItemTemplate>
<ASP:Button CommandName="Select" Text="Info" runat="server" />
<%# Container.DataItem("Title") %>
</ItemTemplate>
<SelectedItemTemplate>
Title: <b><%# Container.DataItem("Title") %></b><br />
<ASP:Button CommandName="Edit" Text="Edit" runat="server" />
ISBN: <%# Container.DataItem("ISBN") %>
Published:
<%# DataBinder.Eval(Container.DataItem, "PublicationDate", "{0:D}") %>
</SelectedItemTemplate>
<EditItemTemplate>
<b>ISBN: <%# Container.DataItem("ISBN") %></b>
<ASP:Button CommandName="Update" Text="Update" runat="server" />
<ASP:Button CommandName="Delete" Text="Delete" runat="server" />
<ASP:Button CommandName="Cancel" Text="Cancel" runat="server" /><br />
Title:
<ASP:TextBox id="txtTitle" Text='<%# Container.DataItem("Title") %>'
size="46" runat="server" /><br />
PublicationDate:
<ASP:TextBox id="txtPubDate" size="20" runat="server"
Text='<%# Container.DataItem("PublicationDate") %>' />
</EditItemTemplate>
</ASP:DataList>
</form>
<!--------------------------------------------------------------------------->
<script language="vb" runat="server">
Sub Page_Load()
If Not Page.IsPostback Then
BindDataGrid() 'create data set and bind to list control
End If
End Sub
Sub DoItemSelect(objSource As Object, objArgs As DataListCommandEventArgs)
lblSQL.Text = "" 'clear any content from SQL statement Label
'see if it was the Select button that was clicked
If objArgs.CommandName = "Select" Then
'set the SelectedIndex property of the list to this item's index
MyDataList.SelectedIndex = objArgs.Item.ItemIndex
BindDataGrid() 'bind the data and display it
End If
End Sub
Sub DoItemEdit(objSource As Object, objArgs As DataListCommandEventArgs)
'set the SelectedIndex property of the list to -1 to "unselect" it
MyDataList.SelectedIndex = -1
'set the EditItemIndex property of the list to this item's index
MyDataList.EditItemIndex = objArgs.Item.ItemIndex
BindDataGrid() 'bind the data and display it
End Sub
Sub DoItemUpdate(objSource As Object, objArgs As DataListCommandEventArgs)
'get a reference to the title and publication date text boxes
Dim objTitleCtrl, objPubDateCtrl As TextBox
objTitleCtrl = CType(objArgs.Item.FindControl("txtTitle"), TextBox)
objPubDateCtrl = CType(objArgs.Item.FindControl("txtPubDate"), TextBox)
'create a suitable SQL statement and execute it
Dim strSQL As String
strSQL = "UPDATE Booklist SET Title='" & objTitleCtrl.Text & "', " _
& "PublicationDate='" & objPubDateCtrl.Text & "' " _
& "WHERE ISBN='" & MyDataList.DataKeys(objArgs.Item.ItemIndex) & "'"
ExecuteSQLStatement(strSQL)
'set EditItemIndex property of grid to -1 to switch out of Edit mode
MyDataList.EditItemIndex = -1
BindDataGrid() 'bind the data and display it
End Sub
Sub DoItemDelete(objSource As Object, objArgs As DataListCommandEventArgs)
'create a suitable SQL statement and execute it
Dim strSQL As String
strSQL = "DELETE FROM Booklist WHERE ISBN='" _
& MyDataList.DataKeys(objArgs.Item.ItemIndex) & "'"
ExecuteSQLStatement(strSQL)
'set EditItemIndex property of grid to -1 to switch out of Edit mode
MyDataList.EditItemIndex = -1
BindDataGrid() 'bind the data and display it
End Sub
Sub DoItemCancel(objSource As Object, objArgs As DataListCommandEventArgs)
'set EditItemIndex property of grid to -1 to switch out of Edit mode
MyDataList.EditItemIndex = -1
BindDataGrid() 'bind the data and display it
End Sub
Sub ExecuteSQLStatement(strSQL)
'this is where the SQL statement would be executed against the
'original data source. In this example, we're simply displaying
'the statement in a Label on the page
lblSQL.Text = "<b>The SQL statement that would be executed is:</b><br />" & strSQL
End Sub
Sub BindDataGrid()
'get connection string from ..\global\connect-strings.ascx user control
Dim strConnect As String = ctlConnectStrings.OLEDBConnectionString
'create a SQL statement to select some rows from the database
Dim strSelect As String
strSelect = "SELECT * FROM BookList WHERE ISBN LIKE '18610025%'"
'create a variable to hold an instance of a DataReader object
Dim objDataReader As OleDbDataReader
Try
'create a new Connection object using the connection string
Dim objConnect As New OleDbConnection(strConnect)
'open the connection to the database
objConnect.Open()
'create a new Command using the connection object and select statement
Dim objCommand As New OleDbCommand(strSelect, objConnect)
'execute the SQL statement against the command to get the DataReader
objDataReader = objCommand.ExecuteReader()
Catch objError As Exception
'display error details
outError.InnerHtml = "<b>* Error while accessing data</b>.<br />" _
& objError.Message & "<br />" & objError.Source & "<p />"
Exit Sub ' and stop execution
End Try
'set the DataSource property and bind the list
MyDataList.DataSource = objDataReader
MyDataList.DataBind()
End Sub
</script>
<!--------------------------------------------------------------------------->
<!-- #include file="..\global\foot.inc" -->
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -