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,028

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



 

การรับค่า 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 : 3337 Reply : 6
 

 

No. 1



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

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

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


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

ไม่ทราบว่า 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
 


 

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)
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
 


 

No. 4



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



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


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


 

No. 5



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

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

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


เอาโค้ดที่เคยทำมาให้ เกี่ยวกับการใช้ 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
 


 

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 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

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