Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,038

HOME > .NET Framework > Forum > การรับค่า id จากgridview โดยเลือกจาก ImageButton คือในหน้านี้จะแสดงข้อมูลที่ค้นหามาได้


 

[.NET] การรับค่า id จากgridview โดยเลือกจาก ImageButton คือในหน้านี้จะแสดงข้อมูลที่ค้นหามาได้

 
Topic : 036849



โพสกระทู้ ( 15 )
บทความ ( 0 )



สถานะออฟไลน์



คือในหน้านี้จะแสดงข้อมูลที่ค้นหามาได้ จะแสดงใน gridview โดยจะมีคอลัมน์ที่ให้คลิกดูไฟล์ได้ คือคลิกที่คอลัมน์ ไฟล์แนบ ใช้ ImageButton เป็นรูปให้คลิก

ตัวอย่าง

แต่ไหนที่นี้คือ มันเอาค่า id ลำดับสุดท้าย แสดง ไม่ใช่ตัวที่เราคลิกเลือก จะมีวิธีแก้ไขยังไงค่ะ

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

Protected Sub GVshowName_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GVshowName.RowDataBound

Dim CurrentRowView As System.Data.DataRowView = e.Row.DataItem

If e.Row.RowType = DataControlRowType.DataRow Then
Dim ibnAttachFile As ImageButton = CType(e.Row.FindControl("ibnAttachFileEmployee"), ImageButton)
Dim connString As String = System.Configuration.ConfigurationManager.ConnectionStrings("warehouseConnectionString").ConnectionString.ToString
Dim connectionString As String = connString
Dim dbConnection As System.Data.IDbConnection = New System.Data.SqlClient.SqlConnection(connectionString)

Dim dbCommand As System.Data.IDbCommand = New System.Data.SqlClient.SqlCommand
dbCommand.CommandType = System.Data.CommandType.StoredProcedure
dbCommand.CommandText = "procSRule_selectbystatusid" 'ชื่อ Store Procedureการแสดงข้อมูล

dbCommand.Connection = dbConnection

Dim dbParam_activity_project_id As System.Data.IDataParameter = New System.Data.SqlClient.SqlParameter
dbParam_activity_project_id.ParameterName = "@id"
dbParam_activity_project_id.Value = GVshowName.SelectedIndex
dbParam_activity_project_id.DbType = System.Data.DbType.StringFixedLength
dbCommand.Parameters.Add(dbParam_activity_project_id)

Dim dataAdapter As System.Data.IDbDataAdapter = New System.Data.SqlClient.SqlDataAdapter
dataAdapter.SelectCommand = dbCommand
Dim dataSet As System.Data.DataSet = New System.Data.DataSet
dataAdapter.Fill(dataSet)

Dim myurl As String = "displaypdf.aspx"
If dataSet.Tables(0).Rows.Count <> 0 Then
If Len(dataSet.Tables(0).Rows(0).Item("rule_url").ToString) > 3 Then
Session("pdfpath") = dataSet.Tables(0).Rows(0).Item("rule_url").ToString
End If
End If

If Session("usertype") = "hi_user" Or Session("usertype") = "admin" Then
myurl = "displaypdf.aspx"
ElseIf Session("usertype") = "mid_user" Then
If Session("rule_level").ToString = "1" Then
Response.Redirect("displaypdf_admin.aspx")
Else
myurl = "displaypdf.aspx"
End If
Else
myurl = "contactinfo.aspx"
End If
ibnAttachFile.PostBackUrl = String.Format(myurl)
End If

End Sub

หน้าจอที่เรียกใช้ gridview

<asp:GridView ID="GVshowName" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" CellPadding="4"
CssClass="dgAppear" DataKeyNames="rule_id" DataSourceID="SqlDataSourceAll"
ForeColor="Black" GridLines="Vertical"
PagerStyle-HorizontalAlign="Center" PageSize="20" Width="100%">
<RowStyle CssClass="dgItem" />
<Columns>
<asp:TemplateField HeaderText="ไฟล์แนบ" ShowHeader="False" >
<ItemTemplate>
<a href = "<%= myurl %>">t</a><asp:ImageButton
ID="ibnAttachFileEmployee" runat="server" CausesValidation="false"
ImageUrl="../images/icon/attach1.gif" Text="ไฟล์แนบ" CommandName = "ibnAttachFile" />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" Width="25px" />
</asp:TemplateField>



Tag : - - - -

Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2010-01-11 15:45:14 By : JuB_JanG View : 3426 Reply : 6
 

 

No. 1



โพสกระทู้ ( 3,144 )
บทความ ( 1 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์


ผมไม่ได้ดูโค้ดคุณหรอกนะ เพราะตาลาย

ไม่ทราบว่า GridView เนี่ยได้ใส่ GridView1.DataKeyNames หรือเปล่าครับ

Code (VB.NET)
1.Dim dataKeyName() As String = New String() { "ID" }
2. 
3.GridView1.DataKeyNames = dataKeyName


เวลาคลิกที่ imagebutton จะได้มี referance อ้างอิง
Date : 2010-01-11 16:28:29 By : tungman
 

 

No. 2



โพสกระทู้ ( 15 )
บทความ ( 0 )



สถานะออฟไลน์


ได้กำหนดไว้ที่หน้า แสดงกริดวิวแล้วค่ะ

<asp:GridView ID="GVshowName" runat="server" DataKeyNames="rule_id"
Date : 2010-01-11 16:37:06 By : JuB_JanG
 

 

No. 3



โพสกระทู้ ( 3,144 )
บทความ ( 1 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์


Code (VB.NET)
1.If e.Row.RowType = DataControlRowType.DataRow Then
2.Dim ibnAttachFile As ImageButton = CType(e.Row.FindControl("ibnAttachFileEmployee"), ImageButton)
3.ibnAttachFile.CommandArgument = GridView1.DataKeys(e.Rows.RowIndex).Value


แล้วเวลาคลิกก็ใช้ event OnCommand และ CommandEventHandler และ CommandEventArgs e

เวลาอ้างอิง id ก็ใช้ e.CommandArgument

Code (VB.NET)
1.Private Sub ibnAttachFile_Command(ByVal sender As Object, ByVal e As CommandEventArgs) Handles ibnAttachFile.Command
2.    Dim ID As String
3.    ID = e.CommandArgument
4.End Sub

Date : 2010-01-11 17:04:10 By : tungman
 

 

No. 4



โพสกระทู้ ( 15 )
บทความ ( 0 )



สถานะออฟไลน์


ขอบคุณนะค่ะที่มาตอบให้ แต่ก็ยังไม่ได้อยู่ดี สงสัยเราจะโง่เอง เฮ้ย!!!
Date : 2010-01-12 09:22:39 By : JuB_JanG
 

 

No. 5



โพสกระทู้ ( 3,144 )
บทความ ( 1 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์


เอาโค้ดที่เคยทำมาให้ เกี่ยวกับการใช้ CommandField ลบข้อมูล

DeleteGridView.aspx
01.<%@ Page Language="VB" AutoEventWireup="false" CodeFile="DeleteGridView.aspx.vb" Inherits="DeleteGridView" %>
02. 
03.<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
04. 
06.<head runat="server">
07.    <title>Untitled Page</title>
08.</head>
09.<body>
10.    <form id="form1" runat="server">
11.    <div>
12.        <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333"
13.            GridLines="None">
14.            <RowStyle BackColor="#EFF3FB" />
15.            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
16.            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
17.            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
18.            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
19.            <EditRowStyle BackColor="#2461BF" />
20.            <AlternatingRowStyle BackColor="White" />
21.        </asp:GridView>
22.    </div>
23.    </form>
24.</body>
25.</html>

DeleteGridView.aspx.vb
01.Imports System.Data
02. 
03.Partial Class DeleteGridView
04.    Inherits System.Web.UI.Page
05. 
06.    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
07.        Dim CustomerData As SqlDatabaseManager = New SqlDatabaseManager()
08.        Dim CustomerDataTable As DataTable = New DataTable()
09. 
10.        CustomerData.CommandString = "Select * From [Customer] Where [TypeID]=1"
11.        CustomerDataTable = CustomerData.ExecuteQuery()
12. 
13.        Dim ID As BoundField = New BoundField()
14.        Dim CustomerName As HyperLinkField = New HyperLinkField()
15.        Dim CustomerPhone As BoundField = New BoundField()
16.        Dim DeleteButton As CommandField = New CommandField()
17. 
18.        ID.HeaderText = "#"
19. 
20.        CustomerName.HeaderText = "ชื่อลูกค้า"
21.        CustomerName.DataTextField = "CustomerThaiName"
22. 
23.        CustomerPhone.HeaderText = "โทรศัพท์"
24.        CustomerPhone.DataField = "CustomerPhone"
25. 
26.        DeleteButton.HeaderText = "ลบ"
27.        DeleteButton.ButtonType = ButtonType.Image
28.        DeleteButton.ShowEditButton = False
29.        DeleteButton.ShowDeleteButton = True
30.        DeleteButton.DeleteImageUrl = "~/images/delete-16x16.png"
31. 
32.        Dim KeyNames() As String = New String() {"CustomerCode"}
33. 
34.        GridView1.AutoGenerateColumns = False
35.        GridView1.DataKeyNames = KeyNames
36. 
37.        If Not IsPostBack Then
38.            GridView1.Columns.Add(ID)
39.            GridView1.Columns.Add(CustomerName)
40.            GridView1.Columns.Add(CustomerPhone)
41.            GridView1.Columns.Add(DeleteButton)
42.            GridView1.DataSource = CustomerDataTable
43.            GridView1.DataBind()
44.        End If
45.    End Sub
46. 
47.    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
48.        If e.Row.RowType = DataControlRowType.DataRow Then
49.            Dim CustomerLink As HyperLink = CType(e.Row.Cells(1).Controls(0), HyperLink)
50.            Dim CustomerDeleteButton As ImageButton = CType(e.Row.Cells(3).Controls(0), ImageButton)
51. 
52.            e.Row.Cells(0).Text = e.Row.RowIndex + 1
53.            CustomerLink.NavigateUrl = "~/customer.aspx?id=" & GridView1.DataKeys(e.Row.RowIndex).Value
54.            CustomerDeleteButton.OnClientClick = "return confirm('คุณต้องการลบข้อมูลนี้ใช่หรือไม่?')"
55.            CustomerDeleteButton.ToolTip = "ลบ"
56.        End If
57.    End Sub
58. 
59.    Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridView1.RowDeleting
60.        Dim CustomerData As SqlDatabaseManager = New SqlDatabaseManager()
61. 
62.        CustomerData.CommandString = "Delete From [Customer] Where [CusotmerCode]=@CustomerCOde"
63.        CustomerData.AddParameter("@CustomerCode", SqlDbType.NVarChar, GridView1.DataKeys(e.RowIndex).Value)
64.        CustomerData.ExecuteNonQuery()
65. 
66.        CustomerData.CommandString = "Select * From [Customer] Where [TypeID]=1"
67.        GridView1.DataSource = CustomerData.ExecuteQuery()
68.        GridView1.DataBind()
69.    End Sub
70.End Class

Date : 2010-01-12 09:44:29 By : tungman
 

 

No. 6

Guest


ขอบคุณมากนะค่ะที่ให้คำตอบ
Date : 2010-01-12 17:23:53 By : JuB_JanG
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : การรับค่า id จากgridview โดยเลือกจาก ImageButton คือในหน้านี้จะแสดงข้อมูลที่ค้นหามาได้
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)





ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2025 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่