⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 select-edit-datalist.aspx

📁 Professional ASP.NET source code
💻 ASPX
字号:
<%@Page Language="C#"%>

<%@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" />
      <%# DataBinder.Eval(Container.DataItem, "Title") %>
    </ItemTemplate>

    <SelectedItemTemplate>
      Title: <b><%# DataBinder.Eval(Container.DataItem, "Title") %></b><br />
      <ASP:Button CommandName="Edit" Text="Edit" runat="server" />
      ISBN: <%# DataBinder.Eval(Container.DataItem, "ISBN") %> &nbsp;
      Published:
      <%# DataBinder.Eval(Container.DataItem, "PublicationDate", "{0:D}") %>
    </SelectedItemTemplate>

    <EditItemTemplate>
      <b>ISBN: <%# DataBinder.Eval(Container.DataItem, "ISBN") %></b> &nbsp;
      <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='<%# DataBinder.Eval(Container.DataItem, "Title") %>'
                   size="46" runat="server" /><br />
      PublicationDate:
      <ASP:TextBox id="txtPubDate" size="20" runat="server"
                   Text='<%# DataBinder.Eval(Container.DataItem, "PublicationDate") %>'/>
    </EditItemTemplate>

  </ASP:DataList>

</form>

<!--------------------------------------------------------------------------->

<script language="C#" runat="server">

	void Page_Load(Object sender, EventArgs e)
	{
		if (!Page.IsPostBack)
			BindDataGrid();		// create data set and bind to list control
	}


	void DoItemSelect(Object objSource, DataListCommandEventArgs objArgs )
	{
		lblSQL.Text = "";		// clear any content from SQL statement Label

		// see if it was the Select button that was clicked
		if (objArgs.CommandName == "Select")
		{
			// set the SelectedIndex property of the list to this item// s index
			MyDataList.SelectedIndex = objArgs.Item.ItemIndex;
			BindDataGrid();		// bind the data and display it
		}
	}


	void DoItemEdit(Object objSource, DataListCommandEventArgs objArgs)
	{
		// 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
	}


	void DoItemUpdate(Object objSource, DataListCommandEventArgs objArgs)
	{
		// get a reference to the title and publication date text boxes
		TextBox objTitleCtrl = (TextBox)objArgs.Item.FindControl("txtTitle");
		TextBox objPubDateCtrl = (TextBox)objArgs.Item.FindControl("txtPubDate");
		
		// create a suitable SQL statement and execute it
		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
	}
	

	void DoItemDelete(Object objSource, DataListCommandEventArgs objArgs)
	{
		// create a suitable SQL statement and execute it
		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
	}


	void DoItemCancel(Object objSource, DataListCommandEventArgs objArgs)
	{
		// set EditItemIndex property of grid to -1 to switch out of Edit mode
		MyDataList.EditItemIndex = -1;
		BindDataGrid();		// bind the data and display it
	}


	void ExecuteSQLStatement(string 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;
	}


	void BindDataGrid()
	{
		// get connection string from ..\global\connect-strings.ascx user control
		string strConnect = ctlConnectStrings.OLEDBConnectionString;

		// create a SQL statement to select some rows from the database
		string strSelect = "SELECT * FROM BookList WHERE ISBN LIKE '%18610025%'";

		// create a variable to hold an instance of a DataReader object
		OleDbDataReader objDataReader;

		try
		{
			// create a new Connection object using the connection string
			OleDbConnection objConnect = new OleDbConnection(strConnect);

			// open the connection to the database
			objConnect.Open();

			// create a new Command using the connection object and select statement
			OleDbCommand objCommand = new OleDbCommand(strSelect, objConnect);

			// execute the SQL statement against the command to get the DataReader
			objDataReader = objCommand.ExecuteReader();
		}
		catch (Exception objError)
		{
			// display error details
			outError.InnerHtml = "<b>* Error while accessing data</b>.<br />"
								+ objError.Message + "<br />" + objError.Source + "<p />";
			return;		//  and stop execution
		}

		// set the DataSource property and bind the list
		MyDataList.DataSource = objDataReader;
		MyDataList.DataBind();
	}

</script>

<!--------------------------------------------------------------------------->
<hr /><span class="cite">[<a href="../global/viewsource.aspx">view source</a>] &nbsp; &nbsp; &nbsp; &nbsp;
+copy;2001 <a class="cite" href="http://www.wrox.com/">Wrox Press</a> -
<a class="cite" href="http://www.wrox.com/Books/Book_Details.asp?isbn=1861004885">Professional ASP.NET</a> (ISBN: 1-861004-88-5)</span>
</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -