ASP.Net 의 Datalist 삭제 기능 추가 코드

8577 단어 Datalist삭제기능.
.aspx 인터페이스

<html xmlns="http://www.w3.org/1999/xhtml">
 <head runat="server">
     <title>DataList ( )</title>
     <script type="text/javascript">
         function CheckAll(Obj) {
             var AllObj = document.all;
             if (Obj.checked)//
             {
                 for (var i = 0; i < AllObj.length; i++) {
                     if (AllObj[i].type == "checkbox") {
                         AllObj[i].checked = true;
                     }
                 }
             }
             else//
             {
                 for (var i = 0; i < AllObj.length; i++) {
                     if (AllObj[i].type == "checkbox") {
                         AllObj[i].checked = false;
                     }
                 }
             }
         }

     </script>
 </head>
 <body>
     <form id="form1" runat="server">
     <div>
     <fieldset style="text-align: center; width: 540px;">
     <legend style=" text-align:center; "> Datalist ( )</legend>

        <asp:DataList ID="DataList1" runat="server"
             onitemcommand="DataList1_ItemCommand" DataKeyField="id">
        <HeaderTemplate>
        <div style="text-align:center">
        <table border = "1" cellpadding="0" cellspacing="0"  style=" font-size:12; width:500px"  >
         <tr>
             <td style="width:100px"> / <input id="Checkbox1" type="checkbox" name=" " value=" " onclick="return CheckAll(this)" title=" " /></td>
             <td style="width:100px"> </td>
             <td style="width:100px"> </td>
             <td style="width:100px"> </td>
             <td style="width:100px"> </td>
         </tr>
        </table>
        </div>
        </HeaderTemplate>

            <ItemTemplate>
            <div style="text-align:center">
            <table border = "1" cellpadding="0" cellspacing="0"  style=" font-size:12; width:500px"  >
                 <tr>
                 <td style="width:100px"> <asp:CheckBox ID="CheckBox2" runat="server" /></td>
                 <td style="width:100px"><asp:Label ID="Label1" runat="server" Text='<%# Eval("id") %>'></asp:Label></td>
                 <td style="width:100px"><asp:Label ID="Label2" runat="server" Text='<%# Eval("bg_name") %>'></asp:Label></td>
                 <td style="width:100px"><asp:Label ID="Label3" runat="server" Text='<%# Eval("bg_p_autograph") %>'></asp:Label></td>
                 <td style="width:100px"><asp:Button ID="btnDelete" runat="server" Text=" "  CommandName="delete"
                        BorderStyle="None" onclientclick="return confirm(&quot; ?&quot;);" /></td><%-- CommandName --%>
                </tr>
             </table>
             </div>
            </ItemTemplate>
            <FooterTemplate>
                 <div style="text-align:center">
                     <table border="1" cellpadding="0" cellspacing="0" style="font-size:12px; width:100%">
                         <tr>
                         <td style="width:100%; text-align:center">
                             <asp:Button ID="btnPLDelete" runat="server" Text=" "  CommandName="pldelete"
                                  BorderStyle="None" onclientclick="return confirm(&quot; ?&quot;);"  /></td>
                         </tr>
                     </table>
                 </div>
            </FooterTemplate>
        </asp:DataList>
        </fieldset>
     </div>
     </form>
 </body>
 </html>
.cs 인터페이스

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class _Default : System.Web.UI.Page
{

    //// Web.config
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connStr"].ConnectionString);
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
           // ( MVC )
            BindDataList();
        }
    }
    // datelist
    private void BindDataList()
    {

       
        // , SQL SQL ( , )
        string strSql = "SELECT * FROM bg_spatial";// SQL
        SqlDataAdapter sda = new SqlDataAdapter(strSql, con);
        DataSet ds = new DataSet();
        sda.Fill(ds);//
        DataList1.DataSource = ds;
        DataList1.DataBind();

    }


    protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {
        switch (e.CommandName)
        {
            //
            case "delete":
                // Datalist
                int id = int.Parse(DataList1.DataKeys[e.Item.ItemIndex].ToString());
                string strSQL = "delete from bg_spatial where id='" + id + "'";
                if (con.State.Equals(ConnectionState.Closed))
                {
                    con.Open();//
                }
                SqlCommand cmd = new SqlCommand(strSQL, con);
                if (Convert.ToInt32(cmd.ExecuteNonQuery())>0)
                {
                    Response.Write("<script>alert(' !')</script>");
                    BindDataList();
                }
                else
                {
                    Response.Write("<script>alert(' ! ')</script>");
                }
                con.Close();//
                break;
            //
            case "pldelete":
                if (con.State.Equals(ConnectionState.Closed))
                {
                    con.Open();//
                }
                DataListItemCollection dlic = DataList1.Items;// DataList
                //
                for (int i = 0; i < dlic.Count; i++)
                {
                    if (dlic[i].ItemType == ListItemType.AlternatingItem||dlic[i].ItemType == ListItemType.Item)
                    {
                         CheckBox cbox = (CheckBox)dlic[i].FindControl("CheckBox2");
                         if (cbox.Checked)
                        {
                            int p_id = int.Parse(DataList1.DataKeys[dlic[i].ItemIndex].ToString());
                            SqlCommand p_cmd = new SqlCommand("delete from bg_spatial where id=" + p_id , con);
                            p_cmd.ExecuteNonQuery();
                        }
                    }

                }
                con.Close();
                BindDataList();
                break;
        }
    }
}

실행 효과 그림:

좋은 웹페이지 즐겨찾기