 |
|
การรับค่า id จากgridview โดยเลือกจาก ImageButton คือในหน้านี้จะแสดงข้อมูลที่ค้นหามาได้ |
|
 |
|
|
 |
 |
|
ผมไม่ได้ดูโค้ดคุณหรอกนะ เพราะตาลาย
ไม่ทราบว่า GridView เนี่ยได้ใส่ GridView1.DataKeyNames หรือเปล่าครับ
Code (VB.NET)
Dim dataKeyName() As String = New String() { "ID" }
GridView1.DataKeyNames = dataKeyName
เวลาคลิกที่ imagebutton จะได้มี referance อ้างอิง
|
 |
 |
 |
 |
Date :
2010-01-11 16:28:29 |
By :
tungman |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ได้กำหนดไว้ที่หน้า แสดงกริดวิวแล้วค่ะ
<asp:GridView ID="GVshowName" runat="server" DataKeyNames="rule_id"
|
 |
 |
 |
 |
Date :
2010-01-11 16:37:06 |
By :
JuB_JanG |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (VB.NET)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim ibnAttachFile As ImageButton = CType(e.Row.FindControl("ibnAttachFileEmployee"), ImageButton)
ibnAttachFile.CommandArgument = GridView1.DataKeys(e.Rows.RowIndex).Value
แล้วเวลาคลิกก็ใช้ event OnCommand และ CommandEventHandler และ CommandEventArgs e
เวลาอ้างอิง id ก็ใช้ e.CommandArgument
Code (VB.NET)
Private Sub ibnAttachFile_Command(ByVal sender As Object, ByVal e As CommandEventArgs) Handles ibnAttachFile.Command
Dim ID As String
ID = e.CommandArgument
End Sub
|
 |
 |
 |
 |
Date :
2010-01-11 17:04:10 |
By :
tungman |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณนะค่ะที่มาตอบให้ แต่ก็ยังไม่ได้อยู่ดี สงสัยเราจะโง่เอง เฮ้ย!!!
|
 |
 |
 |
 |
Date :
2010-01-12 09:22:39 |
By :
JuB_JanG |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เอาโค้ดที่เคยทำมาให้ เกี่ยวกับการใช้ CommandField ลบข้อมูล
DeleteGridView.aspx
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="DeleteGridView.aspx.vb" Inherits="DeleteGridView" %>
<!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:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333"
GridLines="None">
<RowStyle BackColor="#EFF3FB" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
</div>
</form>
</body>
</html>
DeleteGridView.aspx.vb
Imports System.Data
Partial Class DeleteGridView
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim CustomerData As SqlDatabaseManager = New SqlDatabaseManager()
Dim CustomerDataTable As DataTable = New DataTable()
CustomerData.CommandString = "Select * From [Customer] Where [TypeID]=1"
CustomerDataTable = CustomerData.ExecuteQuery()
Dim ID As BoundField = New BoundField()
Dim CustomerName As HyperLinkField = New HyperLinkField()
Dim CustomerPhone As BoundField = New BoundField()
Dim DeleteButton As CommandField = New CommandField()
ID.HeaderText = "#"
CustomerName.HeaderText = "ชื่อลูกค้า"
CustomerName.DataTextField = "CustomerThaiName"
CustomerPhone.HeaderText = "โทรศัพท์"
CustomerPhone.DataField = "CustomerPhone"
DeleteButton.HeaderText = "ลบ"
DeleteButton.ButtonType = ButtonType.Image
DeleteButton.ShowEditButton = False
DeleteButton.ShowDeleteButton = True
DeleteButton.DeleteImageUrl = "~/images/delete-16x16.png"
Dim KeyNames() As String = New String() {"CustomerCode"}
GridView1.AutoGenerateColumns = False
GridView1.DataKeyNames = KeyNames
If Not IsPostBack Then
GridView1.Columns.Add(ID)
GridView1.Columns.Add(CustomerName)
GridView1.Columns.Add(CustomerPhone)
GridView1.Columns.Add(DeleteButton)
GridView1.DataSource = CustomerDataTable
GridView1.DataBind()
End If
End Sub
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim CustomerLink As HyperLink = CType(e.Row.Cells(1).Controls(0), HyperLink)
Dim CustomerDeleteButton As ImageButton = CType(e.Row.Cells(3).Controls(0), ImageButton)
e.Row.Cells(0).Text = e.Row.RowIndex + 1
CustomerLink.NavigateUrl = "~/customer.aspx?id=" & GridView1.DataKeys(e.Row.RowIndex).Value
CustomerDeleteButton.OnClientClick = "return confirm('คุณต้องการลบข้อมูลนี้ใช่หรือไม่?')"
CustomerDeleteButton.ToolTip = "ลบ"
End If
End Sub
Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridView1.RowDeleting
Dim CustomerData As SqlDatabaseManager = New SqlDatabaseManager()
CustomerData.CommandString = "Delete From [Customer] Where [CusotmerCode]=@CustomerCOde"
CustomerData.AddParameter("@CustomerCode", SqlDbType.NVarChar, GridView1.DataKeys(e.RowIndex).Value)
CustomerData.ExecuteNonQuery()
CustomerData.CommandString = "Select * From [Customer] Where [TypeID]=1"
GridView1.DataSource = CustomerData.ExecuteQuery()
GridView1.DataBind()
End Sub
End Class
|
 |
 |
 |
 |
Date :
2010-01-12 09:44:29 |
By :
tungman |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณมากนะค่ะที่ให้คำตอบ
|
 |
 |
 |
 |
Date :
2010-01-12 17:23:53 |
By :
JuB_JanG |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|