我在VS 2013中有一个Listview,我正在尝试将SelectMethod与EF 6和VB一起使用。列表视图的第一部分是:
<asp:ListView ID="ListView1" runat="server" selectmethod="ListView1_GetData"
InsertItemPosition="LastItem"
DataKeyNames="SectorId"
UpdateMethod="ListView1_UpdateItem" >
ListView1_GetData可以完美地填充列表,该列表只有3个字段。这是VB代码:
Private myentity As New RoutesEntities()
Public Function ListView1_GetData() As IQueryable(Of Sector)
Return From myuserlist In myentity.Sectors Select myuserlist
End Function
列表按预期显示,并带有列表视图这一部分的"编辑"按钮:
<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>
以下是实际进行更新的方法的开始:
' The id parameter name should match the DataKeyNames value set on the control
Public Sub ListView1_UpdateItem(ByVal Sectorid As Integer)
Dim theID As String
theID = Sectorid
MsgBox(theID)
End Sub
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
终端类
以下是上面代码的"仅限管理员"页面的部分屏幕截图:
http://www.bikex.net/sectorMaintPix.png
改进的代码
根据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
您可以将ListView的ItemType属性设置为实体的类型。这使得控件具有强类型。然后,您可以将UpdateItem方法中的Sectorid属性从int更改为您的类型(Sector,User,what have you)。然后,ASP.NET将使用来自表单的数据填充实例。
作为替代方案,您还可以调用TryUpdateModel并传入模型类的新实例,然后填充该实例,。
更多详细信息可以在本模型绑定教程中找到。