Repeater实现删改查

 今天有人找我,问Repeater怎么实现增删改查。我想了想写了一下代码,主要是完成功能,没有用什么层次架构什么的。!使用的是vs2005+SQL2005(用的pubs库,Jobs表做的实例!),效果图如下!

Repeater实现删改查_第1张图片edit

HTML代码

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>



    无标题页


   


   

       
           
               
                     job_id:' runat="server">
                     job_desc:' runat="server">
                     min_lvl:' runat="server">
                     max_lvl:' runat="server">
                   
                   
               

               
                     job_id:' runat="server">
                     job_desc:' runat="server">
                     min_lvl:' runat="server">
                     max_lvl:' runat="server">
                   
                   
               

           

       

   

   


 

.cs代码

using System;
using System.Data;
using System.Configuration;
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 _Default : System.Web.UI.Page 
{
    string StrConn = @"User Id=sa;Password=perishryu;Initial Catalog=pubs;Data Source=oathryu/sql2005";
    DataSet ds;
    SqlDataAdapter da;
    SqlConnection conn;
    SqlCommand cmd;
    public bool Make(string sql)
    {
        using (conn = new SqlConnection(StrConn))
        {
            cmd = new SqlCommand(sql, conn);
            if (conn.State != ConnectionState.Closed)
            {
                conn.Close();
            }
            try
            {
                conn.Open();
                cmd.ExecuteNonQuery();
                return false;
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();
            }
        }
    }
    public override void DataBind()
    {
        da = new SqlDataAdapter("select * from jobs",StrConn);
        ds = new DataSet();
        da.Fill(ds);
        this.Repeater1.DataSource = ds.Tables[0];
        this.Repeater1.DataBind();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataBind();
        }
    }
    protected void Repeater1_ItemCreated(object sender, RepeaterItemEventArgs e)
    {

    }
    protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "edit")
        {
            ((Panel)(e.Item.FindControl("PanelItem"))).Visible = false;
            ((Panel)(e.Item.FindControl("PanelEdit"))).Visible = true;
        }
        if (e.CommandName == "update")
        {
            string id = ((Literal)(e.Item.FindControl("LitJob_idEdit"))).Text;
            string desc = ((TextBox)(e.Item.FindControl("txtJob_desc"))).Text;
            string min = ((TextBox)(e.Item.FindControl("txtMin_lvl"))).Text;
            string max = ((TextBox)(e.Item.FindControl("txtMax_lvl"))).Text;
            string sql = "update jobs set job_desc='" + desc + "',min_lvl='" + min + "',max_lvl='" + max + "' where job_id='" + id + "'";
            Make(sql);
            ((Panel)(e.Item.FindControl("PanelItem"))).Visible = true;
            ((Panel)(e.Item.FindControl("PanelEdit"))).Visible = false;
            DataBind();
        }
        if (e.CommandName == "delete")
        {
            string id = ((Literal)(e.Item.FindControl("LitJob_id"))).Text;
            string sql = "delete from jobs where job_id='" + id + "'";
            Make(sql);
            DataBind();
        }
        if (e.CommandName == "cancel")
        {
            ((Panel)(e.Item.FindControl("PanelItem"))).Visible = true;
            ((Panel)(e.Item.FindControl("PanelEdit"))).Visible = false;
        }
    }
}

你可能感兴趣的:(Asp.Net)