Posted by: -xox- on: พฤศจิกายน 24, 2008
<asp:GridView ID=”GridView1″ CssClass=”tblForm1″ runat=”server” AutoGenerateColumns=”False” DataSourceID=”SqlDataSource1″ AllowPaging=”True” BackColor=”LightGoldenrodYellow” BorderColor=”Tan” BorderWidth=”1px” CellPadding=”2″ ForeColor=”Black” GridLines=”None” BorderStyle=”Dotted” PageSize=”20″ Width=”699px” DataKeyNames=”empid”>
ใส่ในหน้า code behide ก็ได้(สำหรับหน้าที่รับค่ามา)
SqlDataSource1.DeleteCommand = “DELETE FROM tblEmployee WHERE [empid] = @empid”
SqlDataSource1.SelectCommand = “SELECT empid, name_thai, sname_thai, name_eng, sname_eng, positions, branch, zone, FileData1 FROM tblEmployee”
SqlDataSource1.UpdateCommand = “UPDATE tblEmployee SET name_thai = @name_thai, sname_thai = @sname_thai WHERE (empid = @empid)”
เิพิ่ม CommandField ชนิด delete มาก่อน แล้วดูใน source code จะเห็นเป็น
<asp:CommandField ShowDeleteButton=”True” DeleteText=”ลบ” />
แต่รูปแบบนี้จะใส่ javascript ไม่ได้ จึงต้องไป edit ใช้คำสั่ง convert this field into a template
ซึ่งจะทำให้ source code เราเปลี่ยนไป แล้วจึงจะใส่โค๊ดที่ทำไฮไลไว้ ดังนี้
<ItemTemplate>
<asp:LinkButton ID=”LinkButton1″ runat=”server” CausesValidation=”False” CommandName=”Delete” Text=”ลบ” OnClientClick=”return confirm(‘Delete’);” ></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
เครดิต http://www.greatfriends.biz/webboards/msg.asp?b=SURREALIST&id=63505 ความคิดเห็นREPLY #12 (63634) คุณ jnithi
<asp:HyperLinkField DataNavigateUrlFields=”empid” DataNavigateUrlFormatString=”editEmpBasic.aspx?empid={0}” Text=”แก้ไข” />
โดยต้องเปิด showfooter = true ก่อน
Private Sub summa()
Dim sum1 As Integer = CType(dt.Compute(“sum(empid)”, “”), Integer)
Dim sum2 As Integer = CType(dt.Compute(“sum(bdate)”, “”), Integer)
GridView1.FooterRow.Cells(0).Text = sum1.ToString(“#0.00″)
GridView1.FooterRow.Cells(1).Text = sum2.ToString(“#0.00″)
End Sub
ขอบคุณมากค่ะ
แต่ถ้าได้โค้ดตัวอย่างสักนิดจะดีมากเลยค่ะ
เพราะพยายามทำหลายรอบแล้ว
แบบว่าเก็บข้อมูลใน dataset แล้วดึงมาแสดงใน Gridview
มันต้องใช้โค้ดอารายมั่งอ่ะคะ เด๋วมันก็ไม่แสดง เด๋วมันก็ error
แล้ว file.txt[c356pop] มีวิธีการใส่อย่างไงอ่ะคะ ใช้ไม่ถูกเด๋วเจ๊งอีก
ขอบคุณมากๆ อีกครั้งค่ะ
พอดีแวะมาอ่ะคับ ขอชมว่า K.FayJaa น่ารักดี อิ อิ นอกเรื่องเกินไปแว้วว
วิธีการนำข้อมูลใน Dataset มาแสดงใน Gridview
ลองแกะดูไม่เข้าใจ mail ถามเอาเน้อน่าจะคล้าย ๆ VB.NET2008 แค่ลองดูวันแรก
Option Strict On
Imports System.Data
Imports System.Data.SqlClient
Imports System.Globalization
Partial Public Class _Default
Inherits System.Web.UI.Page
Dim conn As SqlConnection = New SqlConnection
Dim da As SqlDataAdapter
Dim ds As DataSet = New DataSet
Dim dtfInfo As DateTimeFormatInfo
Dim IsFind As Boolean = False
Dim tr As SqlTransaction
Dim comActivity As SqlCommand
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Private Sub loaddata()
With conn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strConn
.Open()
End With
Dim sqlActivities As String = “”
sqlActivities = “SELECT * FROM tbl_Employee”
sqlActivities &= ” WHERE (emp_ID=’” & Trim(TextBox1.Text) & “‘)”
If IsFind = True Then
ds.Tables(“tbl_Employee”).Clear()
End If
da = New SqlDataAdapter(sqlActivities, strConn)
da.Fill(ds, “tbl_Employee”)
If ds.Tables(“tbl_Employee”).Rows.Count 0 Then
IsFind = True
lblEmpID.Text = CStr(ds.Tables(“tbl_Employee”).Rows(0).Item(“Emp_ID”))
lblEmpName.Text = CStr(ds.Tables(“tbl_Employee”).Rows(0).Item(“Emp_Name”))
lblIDNo.Text = CStr(ds.Tables(“tbl_Employee”).Rows(0).Item(“emp_IDNo”))
lblSocialIDNo.Text = CStr(ds.Tables(“tbl_Employee”).Rows(0).Item(“emp_SocialIDNo”))
lblBankName.Text = CStr(ds.Tables(“tbl_Employee”).Rows(0).Item(“emp_BankName”))
lblBankNo.Text = CStr(ds.Tables(“tbl_Employee”).Rows(0).Item(“emp_BankNo”))
lblEmpAddress.Text = CStr(ds.Tables(“tbl_Employee”).Rows(0).Item(“emp_Address”))
lblEmpPhone.Text = CStr(ds.Tables(“tbl_Employee”).Rows(0).Item(“emp_Phone”))
lblEmpMobile.Text = CStr(ds.Tables(“tbl_Employee”).Rows(0).Item(“emp_Mobile”))
lblEmpRef.Text = CStr(ds.Tables(“tbl_Employee”).Rows(0).Item(“emp_Ref”))
lblAddressEmpRef.Text = CStr(ds.Tables(“tbl_Employee”).Rows(0).Item(“emp_AddressRef”))
lblStatus1.Text = CStr(ds.Tables(“tbl_Employee”).Rows(0).Item(“emp_Status1″))
lblChileNo.Text = CStr(ds.Tables(“tbl_Employee”).Rows(0).Item(“emp_ChildNo”))
lblPosition.Text = CStr(ds.Tables(“tbl_Employee”).Rows(0).Item(“emp_Position”))
lblStatus2.Text = CStr(ds.Tables(“tbl_Employee”).Rows(0).Item(“emp_Status2″))
Else
IsFind = False
End If
conn.Close()
End Sub
Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSearch.Click
loaddata()
End Sub
Protected Sub btnSearch2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSearch2.Click
Dim sqlSearch As String = “”
sqlSearch = “Select * from tbl_Employee”
sqlSearch &= ” WHERE (emp_ID=’” & Trim(TextBox1.Text) & “‘)”
With conn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strConn
.Open()
End With
If IsFind = True Then
ds.Tables(“tbl_Employee”).Clear()
End If
da = New SqlDataAdapter(sqlSearch, conn)
da.Fill(ds, “tbl_Employee”)
If ds.Tables(“tbl_Employee”).Rows.Count 0 Then
IsFind = True
grdEmployee.DataSource = ds.Tables(“tbl_Employee”)
grdEmployee.Visible = True
Else
IsFind = False
End If
conn.Close()
End Sub
End Class
CommandName=”Delete” CausesValidation=”false” ขอถาม 2 คำถามจ้า
1.คอมมานเนม นอกจาก Delete แล้วมีอะไรบ้างคับใช้กับปุ่มหรืออย่างอื่นได้ไหมคับและใช้อย่างไรคับ
2.CausesValidation กำหนดเพื่อ
ขอบคุณคับ [มือใหม่หัดเขียน ASP] chakkree@amarin.co.th
มกราคม 24, 2009 ที่ 12:10 am
คือต้องการนำค่าใน Textbox ไปใส่ใน gridview แล้ววนลูปเพื่อรับค่าใหม่เรื่อยๆ ก่อนจะ save ลง database น่ะคะ
ต้องใส่โค้ดอะไรใน evev on click ค่ะ