1. 程式人生 > >DataList控制元件的使用

DataList控制元件的使用


<%@ Page Language="C#"%><%@ Import Namespace="System.Data.SqlClient"%><script runat=server>void Page_Load(Object sender , EventArgs e) 



if (! IsPostBack ) { 

BindDataList(); 





void BindDataList() { 

SqlConnection conn; 

SqlCommand cmd; 

SqlDataReader dr; 

conn 
=new SqlConnection("Server=localhost; Database=Pubs;uid=cdd;psw=123
" ); 

cmd 
=new SqlCommand( "Select au_id, au_lname, phone From Authors Order by au_lname", conn ); 

conn.Open(); 

dr 
= cmd.ExecuteReader(); 

DataList1.DataSource 
= dr; 

DataList1.DataBind(); 

dr.Close(); 

conn.Close(); 



void DataList1_EditCommand( object s, DataListCommandEventArgs e ) { 

DataList1.EditItemIndex 
= e.Item.ItemIndex; 

BindDataList(); 



void DataList1_CancelCommand( object s, DataListCommandEventArgs e ) { 

DataList1.EditItemIndex 
=-1

BindDataList(); 



void DataList1_DeleteCommand( object s, DataListCommandEventArgs e ) { 

SqlConnection conn; 

string strDelete; 

SqlCommand cmdDelete; 

string strAuthorID; 

strAuthorID 
= DataList1.DataKeys[(int)e.Item.ItemIndex].ToString(); 

conn 
=new SqlConnection("Server=localhost; Database=Pubs;uid=cdd;psw=123" ); 

strDelete 
="Delete Authors Where [email protected]"

cmdDelete 
=new SqlCommand( strDelete, conn ); 

cmdDelete.Parameters.Add( 
"@authorID", strAuthorID ); 

conn.Open(); 

cmdDelete.ExecuteNonQuery(); 

conn.Close(); 

DataList1.EditItemIndex 
=-1

BindDataList(); 



void DataList1_UpdateCommand( object s, DataListCommandEventArgs e ) { 

SqlConnection conn; 

string strUpdate; 

SqlCommand cmdUpdate; 

string strAuthorID; 



strAuthorID 
= DataList1.DataKeys[(int)e.Item.ItemIndex].ToString(); 

TextBox txtLastName 
= (TextBox)e.Item.FindControl( "txtLastName" ); 

TextBox txtPhone 
= (TextBox)e.Item.FindControl( "txtPhone" ); 

conn 
=new SqlConnection("Server=localhost; Database=Pubs;uid=cdd;psw=123" ); 

strUpdate 
="Update Authors set [email protected][email protected] Where [email protected]"

cmdUpdate 
=new SqlCommand( strUpdate, conn ); 

cmdUpdate.Parameters.Add( 
"@authorID", strAuthorID ); 

cmdUpdate.Parameters.Add( 
"@lastname", txtLastName.Text ); 

cmdUpdate.Parameters.Add( 
"@phone", txtPhone.Text ); 

conn.Open(); 

cmdUpdate.ExecuteNonQuery(); 

conn.Close(); 

DataList1.EditItemIndex 
=-1

BindDataList(); 


</Script><html><head><title>DataListEdit.aspx</title></head><body><form Runat="Server"><asp:DataList 

ID="DataList1" 

DataKeyField
="au_id" 

OnEditCommand
="DataList1_EditCommand" 

OnCancelCommand
="DataList1_CancelCommand" 

OnDeleteCommand
="DataList1_DeleteCommand" 

OnUpdateCommand
="DataList1_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>