0

我试图添加按钮或链接到网格视图,以便我可以编辑/删除/更新记录,但我无法添加它,我尝试了很多事情,但我得到了错误,有人可以帮助我。!?我可以在 gridview 中显示记录/日期,但我看不到如何添加按钮事件。

我的front_code是:

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Data_Adapter_Grid.aspx.vb" Inherits="Data_Adapter_Grid" %>

<!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></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
        BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px"
        CellPadding="4" DataKeyNames="test_id" ForeColor="Black" GridLines="Horizontal">
    <Columns>
        <asp:BoundField DataField="test_id" HeaderText="test_id" InsertVisible="False"
            ReadOnly="True" SortExpression="test_id" />
        <asp:BoundField DataField="test_cat" HeaderText="test_cat"
            SortExpression="test_cat" />
        <asp:BoundField DataField="test_info" HeaderText="test_info"
            SortExpression="test_info" />
        <asp:BoundField DataField="test_number" HeaderText="test_number"
            SortExpression="test_number" />
    </Columns>
    <FooterStyle BackColor="#CCCC99" ForeColor="Black" />
    <HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" />
    <SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
    <SortedAscendingCellStyle BackColor="#F7F7F7" />
    <SortedAscendingHeaderStyle BackColor="#4B4B4B" />
    <SortedDescendingCellStyle BackColor="#E5E5E5" />
    <SortedDescendingHeaderStyle BackColor="#242121" />
    </asp:GridView>
</div>
</form>
</body>
</html>

我的背后代码是:

Imports System.Data.OleDb
Imports System.Data

Partial Class Data_Adapter_Grid
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not Page.IsPostBack Then
        Call GetRecords()
    End If
End Sub
' "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='C:\Users\Peter\Documents\Visual Studio 2010\Projects\StockIT\StockIT\bin\Debug\StockManagement.accdb';Persist Security Info=True;Jet OLEDB:Database Password="
'Get all Records

Sub GetRecords()
    Dim strSQL As String = ""
    strSQL = "" & _
    "SELECT * FROM [TableTest] " & _
    "ORDER BY [test_id] ASC"

    Dim dt As New DataTable()
    Using conn As New OleDbConnection(ConfigurationManager.ConnectionStrings("MyConnStr").ConnectionString)
        Try
            conn.Open()
            Dim cmd As New OleDbCommand(strSQL, conn)
            Dim dbAdapter As New OleDbDataAdapter(cmd)
            'Dim cb As New OleDbCommandBuilder(dbAdapter)
            dbAdapter.Fill(dt)  '(dt, "GridLoad")
            dbAdapter.Dispose()
            If dt.Rows.Count > 0 Then
                GridView1.DataSource = dt
                'GridView1.DataMember = "GridLoad"
                GridView1.DataBind()
            End If
        Catch exp As OleDbException
            If True Then
                MsgBox("Error trying to get records, maybe there is no records. " & vbCrLf & "Error: " & exp.Message & "Database Error", MsgBoxStyle.OkOnly, MsgBoxStyle.Critical)
            End If
        Catch exp As Exception
            If True Then
                MsgBox("Error the Database can be unavailable atm. " & vbCrLf & "Error: " & exp.Message & "Database Error", MsgBoxStyle.OkOnly, MsgBoxStyle.Information)
            End If
        End Try
    End Using
End Sub

Protected Sub GridView1_RowEditing(sender As Object, e As GridViewEditEventArgs)

End Sub

Protected Sub GridView1_RowCancelingEdit(sender As Object, e As GridViewCancelEditEventArgs)

End Sub

Protected Sub GridView1_RowUpdating(sender As Object, e As GridViewUpdateEventArgs)

End Sub

Protected Sub GridView1_RowDelete(sender As Object, e As GridViewDeletedEventArgs)

End Sub


End Class
4

3 回答 3

0

尝试在 gridview 上使用自定义按钮。波纹管链接为您提供更多解释示例: gridview 上的自定义按钮

于 2012-09-19T15:40:56.293 回答
0

尝试<asp:templatefield> // add button or link inside </asp:templatefield>调用按钮onrowcommand事件,阅读它以使自己清楚 http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcommand.aspx

于 2012-09-19T21:52:58.693 回答
0

在你的 gridview 属性中设置这两个

AutoGenerateDeleteButton="True" 
AutoGenerateEditButton="True"

或者

<asp:TemplateField>
   <FooterTemplate> // may be you want ItemTemplate 
     <asp:Button ID="btnUpdate" runat="server" CommandName="Update" Text="Update" ValidationGroup="validate" />
   </FooterTemplate>
</asp:TemplateField>

希望这可以帮助!

于 2012-09-20T12:04:48.417 回答