手动绑定数据到DatList并实现编辑,删除,取消···

效果图:

前台代码:
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    
<title>无标题页</title>
            
<style type="text/css">
                      .style1
                
{
                    width: 600px;
                }

                    .style3
                
{
                    width: 150px;
                }

        
</style>

    
</head>
<body>
    
<p>
        
<br />
    
</p>
    
<form id="form1" runat="server">
    
<div align="center">
    
        
<asp:DataList ID="DataList1" runat="server" Width="600px" BackColor="White" 
            BorderColor
="#E7E7FF" BorderStyle="None" BorderWidth="1px" CellPadding="3" 
            GridLines
="Both" Height="421px" oncancelcommand="DataList1_CancelCommand" 
            ondeletecommand
="DataList1_DeleteCommand" oneditcommand="DataList1_EditCommand" 
            onitemcommand
="DataList1_ItemCommand" onupdatecommand="DataList1_UpdateCommand" 
            RepeatColumns
="4" DataKeyField="id">
            
<FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
            
<AlternatingItemStyle BackColor="#F7F7F7" />
            
<ItemStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
            
<EditItemTemplate>
  
                
<table class="style1" border="1">
                    
<tr>
                        
<td class="style3">
                            学号:
</td>
                        
<td>
                           
<asp:TextBox ID="TextBox1" 
                                runat
="server" Text='<%# Eval("id") %>'></asp:TextBox>
                        
</td>
                    
</tr>
                    
<tr>
                        
<td class="style3">
                            姓名:
</td>
                        
<td>
                             
<asp:TextBox ID="TextBox2" 
                                 runat
="server" Text='<%# Eval("name") %>'></asp:TextBox>
                        
</td>
                    
</tr>
                    
<tr>
                        
<td class="style3">
                            性别:
</td>
                        
<td>
                             
<asp:TextBox ID="TextBox3" 
                                 runat
="server" Text='<%# Eval("sex") %>'></asp:TextBox>
                        
</td>
                    
</tr>
                    
<tr>
                        
<td class="style3">
                            年龄:
</td>
                        
<td>
                      
<asp:TextBox ID="TextBox4" 
                                 runat
="server" Text='<%# Eval("age") %>'></asp:TextBox>
                        
</td>
                    
</tr>
                    
<tr>
                        
<td class="style3">
                            专业:
</td>
                        
<td>
                           
<asp:TextBox 
                                ID
="TextBox5" runat="server" Text='<%#Eval("department") %>'></asp:TextBox>
                        
</td>
                    
</tr>
                    
<tr>
                        
<td class="style3">
                            班级:
</td>
                        
<td>
                             
<asp:TextBox ID="TextBox6" 
                                 runat
="server" Text='<%# Eval("grade") %>'></asp:TextBox>
                        
</td>
                    
</tr>
                    
<tr>
                        
<td class="style2" colspan="2">
                            
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="update">保存</asp:LinkButton>
                            
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                            
<asp:LinkButton ID="LinkButton2" runat="server" CommandName="cancel">取消</asp:LinkButton>
                        
</td>
                    
</tr>
                
</table>
            
</EditItemTemplate>
            
<SelectedItemStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
               
            
<HeaderTemplate>
                学生基本情况
            
</HeaderTemplate>
               
            
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
            
<ItemTemplate>
                
                
<table class="style1" border="1">
                    
<tr>
                        
<td class="style3">
                            学号:
</td>
                        
<td>
                            
<%#DataBinder.Eval(Container.DataItem,"id"%></td>
                    
</tr>
                    
<tr>
                        
<td class="style3">
                            姓名:
</td>
                        
<td>
                             
<%#DataBinder.Eval(Container.DataItem,"name"%></td>
                    
</tr>
                    
<tr>
                        
<td class="style3">
                            性别:
</td>
                        
<td>
                             
<%#DataBinder.Eval(Container.DataItem,"sex"%></td>
                    
</tr>
                    
<tr>
                        
<td class="style3">
                            年龄:
</td>
                        
<td>
                             
<%#DataBinder.Eval(Container.DataItem,"age"%></td>
                    
</tr>
                    
<tr>
                        
<td class="style3">
                            专业:
</td>
                        
<td>
                            
<%#DataBinder.Eval(Container.DataItem,"department"%></td>
                    
</tr>
                    
<tr>
                        
<td class="style3">
                            班级:
</td>
                        
<td>
                             
<%#DataBinder.Eval(Container.DataItem,"grade"%></td>
                    
</tr>
                    
<tr>
                        
<td class="style2" colspan="2">
                            
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="edit">修改</asp:LinkButton>
                            
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                            
<asp:LinkButton ID="LinkButton2" runat="server" CommandName="delete">删除</asp:LinkButton>
                        
</td>
                    
</tr>
                
</table>
                
            
</ItemTemplate>
        
</asp:DataList>
    
    
</div>
    
</form>
</body>
</html>
后台代码:
using System;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;

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

    }


    
private void Bind()
    
{
        SqlConnection conn 
= new SqlConnection(ConfigurationManager.ConnectionStrings["Personal"].ConnectionString);
        SqlDataAdapter adp 
= new SqlDataAdapter("select * from information",conn);
        DataSet dataset 
= new DataSet();
        conn.Open();
        
try
        
{
            adp.Fill(dataset, 
"information");
            DataList1.DataSource 
= dataset;
            DataList1.DataKeyField 
= "id";
            DataList1.DataBind();
        }

        
catch (Exception erro)
        
{
            Response.Write(
"错误" + erro);
        }

        
finally
        
{
            conn.Close();
        }

    }

    
protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)
    
{
        DataList1.EditItemIndex 
= -1;
        Bind();
    }

    
protected void DataList1_DeleteCommand(object source, DataListCommandEventArgs e)
    
{
        SqlConnection conn 
= new SqlConnection(ConfigurationManager.ConnectionStrings["Personal"].ConnectionString);
        SqlCommand comm
=new SqlCommand("delete from information where id='"+DataList1.DataKeys[e.Item.ItemIndex]+"'",conn);
        conn.Open();
        
int i=Convert.ToInt32( comm.ExecuteNonQuery());
        
if (i > 0)
        
{
            Response.Write(
"<script>return alert('删除成功')");
            DataList1.EditItemIndex 
= -1;
            Bind();
        }

        
else
        
{
            Response.Write(
"<script>return alert('删出失败')");
        }

        conn.Close();
    }

    
protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
    
{
        DataList1.EditItemIndex 
= e.Item.ItemIndex;
        Bind();
    }

    
protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
    
{
        
string ID = DataList1.DataKeys[e.Item.ItemIndex].ToString();
        
string id = ((TextBox)e.Item.FindControl("TextBox1")).Text;
        
string name=((TextBox)e.Item.FindControl("TextBox2")).Text;
        
string sex= ((TextBox)e.Item.FindControl("TextBox3")).Text;
        
string age=((TextBox)e.Item.FindControl("TextBox4")).Text;
        
string department=((TextBox)e.Item.FindControl("TextBox5")).Text;
        
string grade=((TextBox)e.Item.FindControl("TextBox6")).Text;
        SqlConnection conn 
= new SqlConnection(ConfigurationManager.ConnectionStrings["Personal"].ConnectionString);
        SqlCommand comm 
= new SqlCommand("update information set  id='" + id + "' , name='" + name + "' , sex='" + sex + "' , age='"+age+"' , department='" + department + "' , grade='" + grade + "' where id='" + ID + "'", conn);
        conn.Open();
        
try
        
{
            
int i = Convert.ToInt32(comm.ExecuteNonQuery());
            
if (i > 0)
            
{
                Response.Write(
"<script language=javascript>alert('保存成功!')</script>");
            }

            
else
            
{
                Response.Write(
"<script language=javascript>alert('保存失败!')</script>");
            }

            DataList1.EditItemIndex 
= -1;
            Bind();
        }

        
catch (Exception erro)
        
{
            Response.Write(
"错误信息:" + erro.Message);
        }

        
finally
        
{
            conn.Close();
        }

    }

    
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    
{

    }

}

posted @ 2008-03-24 22:42  MicroCoder  阅读(820)  评论(0编辑  收藏  举报