.NET Logo
Welcome Guest Search | Active Topics | Members | Log In | Register

ขอเรื่อง DataList Options · View
zerozaaa
Posted: Wednesday, August 06, 2008 3:13:20 PM
Rank: มือพระกาฬ
Groups: Member

Joined: 6/13/2008
Posts: 80

ผมต้องการเขียนให้กด ที่ button แล้วลบ คับ ผมไม่รู้ว่าเขียนส่วนไหนของ โค๊ด คับ แล้ว เงีอนไขลบ ให้ลบ ตรง ฟิลด์ นั้นด้วยคับช่วยเขียนโค๊ดออกมาให้ดูทีคับ

paedotnet
Posted: Wednesday, August 13, 2008 3:50:55 PM

Rank: มือเทพ
Groups: Member

Joined: 12/6/2007
Posts: 354
Location: bkk
1. ลาก sqldatasource มาวาง จากนั้นก็ทำการ binding ข้อมูลกับฐานข้อมูล
2  ลาก DataList มาวาง เลือก datasource เป็น SqlDataSource1 แล้วตรง Edit Template (คลิกตรงลูกศรมุมขวาบนของ datalist)
   ให้ลากปุ่ม button มาวาง 1 ปุ่ม จากนั้นคลิก end template editing
3. ไปที่ หน้า source เขียนคำสั่งเพิ่มตรงปุ่มดังนี้ CommandName="delete"
     <asp:Button ID="Button1" runat="server" CommandName="delete" Text="Delete" />
4. ตรง property ของ datatalist ให้เขียน DataKeyField ="id" ( id เป็นชื่อ field ที่ใช้อ้างอิง row ในการลบข้อมูลว่าต้องการ
   ลบที่ข้อมูลไหน อาจจะเป็นชื่ออื่นๆก็ได้
5. คลิกที่ datalist แล้วใส่ event ที่ชื่อ DeleteCommand แล้วใน event นี้เขียนโค้ดดังนี้
  protected void DataList1_DeleteCommand(object source, DataListCommandEventArgs e)
    {
        int id = (int)DataList1.DataKeys[e.Item.ItemIndex];
        SqlDataSource1.DeleteParameters["id"].DefaultValue = id.ToString();
        SqlDataSource1.Delete();
    }


จากนั้นทดสอบผลลัพธ์ดู

ตัวอย่างโค้ดทั้งหมด

Default.aspx

<%@ 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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DataList ID="DataList1" runat="server" BorderWidth="2px" CellPadding="5" CellSpacing="5"
            DataKeyField="id"  DataSourceID="SqlDataSource1" RepeatDirection="Horizontal"
            Width="98px" OnDeleteCommand="DataList1_DeleteCommand">
            <ItemTemplate>
                id: &nbsp; &nbsp;&nbsp;
                <asp:Label ID="idLabel" runat="server" Text='<%# Eval("id") %>'></asp:Label><br />
                name:
                <asp:Label ID="nameLabel" runat="server" Text='<%# Eval("name") %>'></asp:Label><br />
                age: &nbsp;&nbsp;
                <asp:Label ID="ageLabel" runat="server" Text='<%# Eval("age") %>'></asp:Label><br />
                <br />
                <asp:Button ID="Button1" runat="server" CommandName="delete" Text="Delete" />
            </ItemTemplate>
        </asp:DataList></div>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:testdatalistConnectionString %>"
            SelectCommand="SELECT * FROM [table1]" DeleteCommand="DELETE FROM [table1] WHERE [id] = @id">
            <DeleteParameters>
                <asp:Parameter Name="id" Type="Int32" />
            </DeleteParameters>
         
        </asp:SqlDataSource>
    </form>
</body>
</html>


 

หน้า Default.aspx.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;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
  
    protected void DataList1_DeleteCommand(object source, DataListCommandEventArgs e)
    {
        int id = (int)DataList1.DataKeys[e.Item.ItemIndex];
        SqlDataSource1.DeleteParameters["id"].DefaultValue = id.ToString();
        SqlDataSource1.Delete();
    }
}
 


[With great power comes great responsibility]
zerozaaa
Posted: Tuesday, September 02, 2008 12:05:14 AM
Rank: มือพระกาฬ
Groups: Member

Joined: 6/13/2008
Posts: 80

ขอบคุณคับ

Users browsing this topic
Guest


Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Main Forum RSS : RSS

YAFVision Theme Created by Jaben Cargman (Tiny Gecko)
Powered by Yet Another Forum.net version 1.9.1.8 (NET v2.0) - 3/29/2008
Copyright © 2003-2008 Yet Another Forum.net. All rights reserved.


Sponsored by