Vb.net EF 6和ListView选择方法
我在VS2013中有一个Listview,我正在尝试将SelectMethod与EF 6和VB结合使用。listview的第一部分是:Vb.net EF 6和ListView选择方法,vb.net,entity-framework,listview,Vb.net,Entity Framework,Listview,我在VS2013中有一个Listview,我正在尝试将SelectMethod与EF 6和VB结合使用。listview的第一部分是: <asp:ListView ID="ListView1" runat="server" selectmethod="ListView1_GetData" InsertItemPosition="LastItem" DataKeyNames="SectorId" UpdateMethod="ListView1_UpdateIte
<asp:ListView ID="ListView1" runat="server" selectmethod="ListView1_GetData"
InsertItemPosition="LastItem"
DataKeyNames="SectorId"
UpdateMethod="ListView1_UpdateItem" >
该列表按预期显示,并带有listview此部分的“编辑”按钮:
<EditItemTemplate>
<tr style="background-color:#008A8C;color: #FFFFFF;">
<td>
<asp:Button ID="UpdateButton" runat="server" CommandName="Update"
Text="Update" CausesValidation="False" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel"
Text="Cancel" CausesValidation="False" />
</td>
<td>
<asp:Label ID="SectorIdLabel1" runat="server" style= "width:40px" Text='<%#Eval "SectorId") %>' />
</td>
<td>
<asp:TextBox ID="SectorTitleTextBox" runat="server"
Text='<%# Bind("SectorTitle") %>' />
</td>
<td>
<asp:TextBox ID="SectorDescriptionTextBox" runat="server"
Text='<%# Bind("SectorDescription") %>' />
</td>
</tr>
</EditItemTemplate>
VS2013创建sub,我用我的“Sectorid”替换了“id”。当我单击列表中的不同行并单击“更新”按钮时,我被带到这个子节点,正确的ID出现在MsgBox中。从这一点上,我完全被困在如何继续下去的问题上。我找不到一个VB教程,它与我要做的事情有关。这个SelectMethod似乎是个好主意,但似乎没有太多关于如何使用它的信息。任何有关VB教程的建议或指针都将不胜感激
更新-以下代码有效
多亏了Imar,我现在已经向前迈进了,我想我会为其他可能有同样需求的人分享更新程序。请注意,我不是一个专业的程序员,当然其他人可以改进这段代码-但它对我来说很有用,并且为我的站点中的其他页面提供了我想要的灵活性。这个页面只是一个简单的地方来测试这个模型
Imports System.Linq
Imports RoutesEntities
Partial Class Management_SectorMaint
Inherits System.Web.UI.Page
Private myentity As New RoutesEntities()
Public Function ListView1_GetData() As IQueryable(Of Sector)
Return From myuserlist In myentity.Sectors Select myuserlist
End Function
Protected Sub Page_Unload(sender As Object, e As EventArgs) Handles Me.Unload
If myentity IsNot Nothing Then
myentity.Dispose()
End If
End Sub
Public Sub ListView1_UpdateItem(ByVal sectorid As Integer, ByVal sectortitle As String, ByVal sectordescription As String)
Dim items As Sector = Nothing
Dim sectoridnumber As Integer
Dim mysectortitle As String
Dim mysectordescription As String
sectoridnumber = sectorid
mysectortitle = sectortitle
mysectordescription = sectordescription
items = (From s In myentity.Sectors
Where s.SectorId = sectoridnumber
Select s).Single()
If items Is Nothing Then
MsgBox("Bad sectorID")
Return
End If
items.SectorTitle = mysectortitle
items.SectorDescription = mysectordescription
TryUpdateModel(items)
If ModelState.IsValid Then
myentity.SaveChanges()
End If
End Sub
末级
以下是上述代码的“仅管理员”页面的部分屏幕截图:
改进的代码
根据Imar,这里是一个已清理的版本:
Public Sub ListView1_UpdateItem(sector As Sector)
Dim items As Sector = Nothing
items = (From s In myentity.Sectors
Where s.SectorId = sector.SectorId
Select s).Single()
If items Is Nothing Then
msglabel.Text = " Failed" 'jquery handles hide-show Div and using a client registered "isPostBack" Var
Return
End If
items.SectorTitle = sector.SectorTitle
items.SectorDescription = sector.SectorDescription
TryUpdateModel(items)
If ModelState.IsValid Then
myentity.SaveChanges()
msglabel.Text = " Successful"
End If
End Sub
最终版本
非常感谢Imar的指导-此版本非常适合:
<asp:ListView ID="ListView1" runat="server" selectmethod="ListView1_GetData"
ItemType="Sector"
DataKeyNames="SectorId"
UpdateMethod="ListView1_UpdateItem">
<AlternatingItemTemplate>
<tr style="background-color:#FFF8DC;">
<td>.............
Imports System.Linq
Imports RoutesEntities
Partial Class Management_SectorMaint
Inherits System.Web.UI.Page
Private myContext As New RoutesEntities()
Public Function ListView1_GetData() As IQueryable(Of Sector)
Return From myuserlist In myContext.Sectors Select myuserlist
End Function
Public Sub ListView1_UpdateItem(sector As Sector)
Dim item As Sector = (From s In myContext.Sectors
Where s.SectorId = sector.SectorId
Select s).SingleOrDefault()
If item Is Nothing Then
msglabel.Text = " Failed"
Return
End If
TryUpdateModel(item)
item.SectorTitle = String.Concat(sector.SectorTitle, " test") ' A change to "item" here goes to DB
If ModelState.IsValid Then
myContext.SaveChanges()
msglabel.Text = " Successful"
Else
msglabel.Text = " Fail"
End If
End Sub
.............
导入系统
导入路由实体
部分类管理\u SectorMaint
继承System.Web.UI.Page
私有myContext作为新路由实体()
公共函数ListView1_GetData()作为IQueryable(扇区的)
从myContext中的myuserlist返回。选择myuserlist
端函数
公共子列表视图1_UpdateItem(扇区作为扇区)
作为扇区的Dim项=(来自myContext.Sectors中的s)
其中s.SectorId=sector.SectorId
选择s).SingleOrDefault()
如果项目不算什么,那么
msglabel.Text=“失败”
返回
如果结束
TryUpdateModel(项目)
item.SectorTitle=String.Concat(sector.SectorTitle,“test”)'此处对“item”的更改转到DB
如果ModelState.IsValid,则
myContext.SaveChanges()
msglabel.Text=“成功”
其他的
msglabel.Text=“失败”
如果结束
端接头
您可以将ListView的ItemType属性设置为实体的类型。这使控件具有强类型。然后,您可以将UpdateItem方法中的Sectorid属性从int更改为您的类型(扇区、用户、您拥有的内容)。然后,ASP.NET将使用表单中的数据填充实例
作为替代方案,您还可以调用TryUpdateModel并传入模型类的新实例,然后填充该实例
更多详情可在此找到。非常感谢!这篇教程正是我想要的。尽管我环顾四周,却从未发现这个。在返回我的网站之前,我将完成本教程。虽然这样做有效,但在实体类型的UpdateItem中创建单个参数通常更容易。例如:
Public Sub-ListView1\u UpdateItem(扇区作为扇区)
假设扇区是您的项目类型,并且您将列表控件上的ItemType设置为sector。顺便说一句:您不应该使用MsgBox。虽然它看起来可以工作,但实际上它是一个服务器端消息框,如果客户端与服务器不是同一台机器,它将无法工作。改用JavaScript的警报或其他方式。
Public Sub ListView1_UpdateItem(sector As Sector)
Dim items As Sector = Nothing
items = (From s In myentity.Sectors
Where s.SectorId = sector.SectorId
Select s).Single()
If items Is Nothing Then
msglabel.Text = " Failed" 'jquery handles hide-show Div and using a client registered "isPostBack" Var
Return
End If
items.SectorTitle = sector.SectorTitle
items.SectorDescription = sector.SectorDescription
TryUpdateModel(items)
If ModelState.IsValid Then
myentity.SaveChanges()
msglabel.Text = " Successful"
End If
End Sub
<asp:ListView ID="ListView1" runat="server" selectmethod="ListView1_GetData"
ItemType="Sector"
DataKeyNames="SectorId"
UpdateMethod="ListView1_UpdateItem">
<AlternatingItemTemplate>
<tr style="background-color:#FFF8DC;">
<td>.............
Imports System.Linq
Imports RoutesEntities
Partial Class Management_SectorMaint
Inherits System.Web.UI.Page
Private myContext As New RoutesEntities()
Public Function ListView1_GetData() As IQueryable(Of Sector)
Return From myuserlist In myContext.Sectors Select myuserlist
End Function
Public Sub ListView1_UpdateItem(sector As Sector)
Dim item As Sector = (From s In myContext.Sectors
Where s.SectorId = sector.SectorId
Select s).SingleOrDefault()
If item Is Nothing Then
msglabel.Text = " Failed"
Return
End If
TryUpdateModel(item)
item.SectorTitle = String.Concat(sector.SectorTitle, " test") ' A change to "item" here goes to DB
If ModelState.IsValid Then
myContext.SaveChanges()
msglabel.Text = " Successful"
Else
msglabel.Text = " Fail"
End If
End Sub