在ASP.NET 2.0 下如何实现DataList的编辑功能

前台:

 

     < form id = " form1 "  runat = " server " >
    
< div >
        
< asp:DataList ID = " dlEditItem "  runat = " server "  DataKeyField = " EmployeeID "  OnCancelCommand = " dlEditItem_CancelCommand "  OnEditCommand = " dlEditItem_EditCommand "  OnUpdateCommand = " dlEditItem_UpdateCommand " >
        
< HeaderTemplate > 人员信息 </ HeaderTemplate >
        
< ItemTemplate >
        
< asp:Button ID = " edit "  runat = " server "  Text = " Edit "  CommandName = " Edit " /><% # Eval( " LastName " ) %><% # Eval( " FirstName " ) %>
        
</ ItemTemplate >
        
< EditItemTemplate >
        
< asp:Label ID = " lastname "  runat = " server " ><% # Eval( " LastName " ) %></ asp:Label >
        
< asp:Label ID = " firstname "  runat = " server " ><% #Eval( " FirstName " %></ asp:Label >
        
< asp:TextBox ID = " title "  runat = " server "  Text = ' <%#Eval("Title") %> ' ></ asp:TextBox >
        
< asp:Button ID = " update "  CommandName = " update "  runat = " server "  Text = " Update " />
        
< asp:Button ID = " cancel "  runat = " server "  CommandName = " cancel "  Text = " Cancel " />
        
</ EditItemTemplate >
        
< FooterTemplate >< hr ></ FooterTemplate >
        
</ asp:DataList ></ div >
    
</ form >

 后台:

 

using  System;
using  System.Data;
using  System.Configuration;
using  System.Collections;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Web.UI.HtmlControls;
using  System.Data.SqlClient;

public   partial   class  DataListEditItem : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
if (!IsPostBack)
            DataListDataBind();
    }


    
private void DataListDataBind()
    
{
        
string connStr = ConfigurationManager.ConnectionStrings["northwind"].ConnectionString;
        SqlConnection conn 
= new SqlConnection(connStr);
        SqlDataAdapter Adapter 
= new SqlDataAdapter("select employeeid,lastname, firstname, title from employees", conn);
        conn.Open();
        DataSet ds 
= new DataSet();
        
try
        
{
            Adapter.Fill(ds, 
"testTable");
            dlEditItem.DataSource 
= ds.Tables["testTable"].DefaultView;
            dlEditItem.DataBind();
        }

        
catch (Exception error)
        
{
            Response.Write(error.ToString());
        }

        
finally
        
{
            conn.Close();
        }

    }

    
protected void dlEditItem_CancelCommand(object source, DataListCommandEventArgs e)
    
{
        dlEditItem.EditItemIndex 
= -1;
        DataListDataBind();
    }

    
protected void dlEditItem_EditCommand(object source, DataListCommandEventArgs e)
    
{
        dlEditItem.EditItemIndex 
= e.Item.ItemIndex;
        DataListDataBind();
    }

    
protected void dlEditItem_UpdateCommand(object source, DataListCommandEventArgs e)
    
{
        
int empID = (int)dlEditItem.DataKeys[e.Item.ItemIndex];
        TextBox newTitle 
= (TextBox)e.Item.FindControl("title");
        
string strUpt = "update employees set title ='" + newTitle.Text + "' where employeeid=" + empID.ToString();
        
string connStr = ConfigurationManager.ConnectionStrings["northwind"].ConnectionString;
        SqlConnection conn 
= new SqlConnection(connStr);
        SqlCommand cmd 
= new SqlCommand(strUpt, conn);
        conn.Open();
        
try
        
{
            cmd.ExecuteNonQuery();
            dlEditItem.EditItemIndex 
= -1;
            DataListDataBind();
        }

        
catch(Exception error)
        
{
            Response.Write(error.ToString());
        }

        
finally
        
{
            conn.Close();
        }

    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值