0

我试图使我的第一个 linq 连接到 gridview,(我没有使用 linqdatasource)并且我的记录很好到我的 gridview。但是我的 AddNewTask、UpdateTask 和 DeleteTask 给了我关于我的 Me.txtxxxx.text 或 Me.lblxxxx.text 的错误不是我的 gridview 的一部分。但标签和文本框是我的网格视图的一部分。

有人可以帮助/指导我,该怎么做,我是这个 linq 的新手,但是已经阅读了很多教程,但找不到任何解决我的问题的方法。

Code_Behind

Imports System.Data.Linq

Partial Class gridview
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 linqdb()
    End If
End Sub

Protected Sub linqdb()
    Using db As New ThedatabaseconnectionDataContext()
        Dim tblTest As Table(Of testtable) = db.GetTable(Of testtable)()
        Me.GridView1.DataSource = tblTest
        Me.GridView1.DataBind()
    End Using
End Sub

Protected Sub AddNewTask(ByVal sender As Object, ByVal e As EventArgs)
    Using db As New ThedatabaseconnectionDataContext()
        Try
            Dim tbltest As Table(Of testtable) = db.GetTable(Of testtable)()
            Dim rtest As testtable = New testtable With {.test_cat = Me.txttestcat.Text, .test_info = Me.txttestinfo.Text, .test_number = Me.txttestnumber.Text, .test_datetime = Me.txttestdate.Text}
            tbltest.InsertOnSubmit(rtest)
            db.SubmitChanges()
            'Me.lblMsg.Text = "Added Successfully"
        Catch ex As Exception
            'Me.lblMsg.Text = ex.Message
        End Try
    End Using
End Sub

Protected Sub EditTask(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
    GridView1.EditIndex = e.NewEditIndex
    linqdb()
End Sub

Protected Sub CancelEdit(ByVal sender As Object, ByVal e As GridViewCancelEditEventArgs)
    GridView1.EditIndex = -1
    linqdb()
End Sub

Protected Sub UpdateTask(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
    Using db As New ThedatabaseconnectionDataContext()
        Try
            Dim tbltest As Table(Of testtable) = db.GetTable(Of testtable)()
            Dim objtest As testtable = tbltest.SingleOrDefault(Function(p) p.test_id = Me.txttestid.Text)
            If objtest IsNot Nothing Then
                objtest.test_cat = Me.txttestcat.Text
                objtest.test_info = Me.txttestinfo.Text
                objtest.test_number = Me.txttestnumber.Text
                objtest.test_datetime = Me.txttestdate.Text
                db.SubmitChanges()
                ' Me.lblMsg.Text = "Updated Successfully"
            Else
                'Me.lblMsg.Text = "Employee not found"
            End If
        Catch ex As Exception
            'Me.lblMsg.Text = ex.Message
        End Try
    End Using
End Sub

Protected Sub DeleteTask(ByVal sender As Object, ByVal e As EventArgs)
    Using db As New ThedatabaseconnectionDataContext()
        Try
            Dim tbltest As Table(Of testtable) = db.GetTable(Of testtable)()
            Dim objtest As testtable = tbltest.SingleOrDefault(Function(p) p.test_id = Me.lbltestid.text)
            If objtest IsNot Nothing Then
                tbltest.DeleteOnSubmit(objtest)
                db.SubmitChanges()
                'Me.lblMsg.Text = "Deleted Successfully"
            Else
                'Me.lblMsg.Text = "Employee not found"
            End If
        Catch ex As Exception
            'Me.lblMsg.Text = ex.Message
        End Try
    End Using
End Sub
End Class

Main_Code

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

<!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:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate> 
<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" ShowFooter="true" 
        onrowediting="EditTask" onrowupdating="UpdateTask"  onrowcancelingedit="CancelEdit">
    <Columns>
        <asp:TemplateField ItemStyle-Width="30px" HeaderText="test_id">
            <ItemTemplate>
                <asp:Label ID="lbltestid" runat="server" Text='<%# Eval("test_id")%>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField ItemStyle-Width="100px" HeaderText="test_cat">
            <ItemTemplate>
                <asp:Label ID="lbltestcat" runat="server" Text='<%# Eval("test_cat")%>'></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="txttestcat" runat="server" Text='<%# Eval("test_cat")%>'></asp:TextBox>
            </EditItemTemplate> 
            <FooterTemplate>
                <asp:TextBox ID="txttestcat" runat="server"></asp:TextBox>
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField ItemStyle-Width="150px" HeaderText="test_info">
            <ItemTemplate>
                <asp:Label ID="lbltestinfo" runat="server" Text='<%# Eval("test_info")%>'></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="txttestinfo" runat="server" Text='<%# Eval("test_info")%>'></asp:TextBox>
            </EditItemTemplate> 
            <FooterTemplate>
                <asp:TextBox ID="txttestinfo" runat="server"></asp:TextBox>
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField ItemStyle-Width="150px" HeaderText="test_number">
            <ItemTemplate>
                <asp:Label ID="lbltestnumber" runat="server" Text='<%# Eval("test_number")%>'></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="txttestnumber" runat="server" Text='<%# Eval("test_number")%>'></asp:TextBox>
            </EditItemTemplate> 
            <FooterTemplate>
                <asp:TextBox ID="txttestnumber" runat="server"></asp:TextBox>
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField ItemStyle-Width="150px" HeaderText="test_date">
            <ItemTemplate>
                <asp:Label ID="lbltestdate" runat="server" Text='<%# Eval("test_datetime")%>'></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="txttestdate" runat="server" Text='<%# Eval("test_datetime")%>'></asp:TextBox>
            </EditItemTemplate> 
            <FooterTemplate>
                <asp:TextBox ID="txttestdate" runat="server"></asp:TextBox>
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:LinkButton ID="lnkRemove" runat="server" CommandArgument='<%# Eval("test_id")%>' OnClientClick="return confirm('Do you want to delete?')" Text="Delete" OnClick="DeleteTask"></asp:LinkButton>
            </ItemTemplate>
            <FooterTemplate>
                <asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="AddNewTask" />
            </FooterTemplate>
        </asp:TemplateField>
        <asp:CommandField ShowEditButton="True" />
    </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>
</ContentTemplate>
<Triggers>
    <asp:AsyncPostBackTrigger ControlID="GridView1" />
</Triggers>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>

测试表记录.vb

Imports Microsoft.VisualBasic
Imports System.Web
Imports System.Data.Linq.Mapping

<Table(Name:="testtablerecords")> _
Public Class testtableRecords
'DB Fieldname and type = new name _name..
Private _testId As Integer
<Column(IsPrimaryKey:=True)> _
Public Property test_id() As Integer
    Get
        Return _testId
    End Get

    Set(ByVal value As Integer)
        _testId = value
    End Set
End Property

'DB Fieldname and type = new name _name..
Private _testCat As String
<Column()> _
Public Property test_cat() As String
    Get
        Return _testCat
    End Get

    Set(ByVal value As String)
        _testCat = value
    End Set
End Property

'DB Fieldname and type = new name _name..
Private _testInfo As String
<Column()> _
Public Property test_info() As String
    Get
        Return _testInfo
    End Get

    Set(ByVal value As String)
        _testInfo = value
    End Set
End Property

'DB Fieldname and type = new name _name..
Private _testNumbers As Integer
<Column()> _
Public Property test_numbers() As Integer
    Get
        Return _testNumbers
    End Get

    Set(ByVal value As Integer)
        _testNumbers = value
    End Set
End Property

'DB Fieldname and type = new name _name..
Private _testDate As Date
<Column()> _
Public Property test_datetime() As Date
    Get
        Return _testDate
    End Get

    Set(ByVal value As Date)
        _testDate = value
    End Set
End Property

End Class
4

1 回答 1

1

问题是txttestcattxttestinfo等。不是你的类的属性gridview。它们是页面对象层次结构深处的对象的 ID,特别是GridView1.

要访问它们,您需要使用FindControlGridViewRow 的方法。我正在记忆中工作,在这里,但如果你替换Me.txttestcat.Text

CType(e.Row.FindControl("txttestcat"),TextBox).Text

...并类似地转换其他行,你应该没问题。

于 2012-09-20T21:56:18.777 回答