2013-11-26 39 views
0

我在VS 2013中有一個Listview,我試圖在EF 6和VB中使用SelectMethod。列表視圖的第一部分是這樣的:EF 6和ListView SelectMethod

<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」。當我點擊列表中的不同行並點擊更新按鈕時,我被帶到這個子文件夾,並在MsgBox中出現正確的ID。從這一點來說,我完全陷入如何繼續。我無法找到與我正在嘗試做什麼有關的VB教程。這個SelectMethod似乎是個好主意,但似乎沒有太多有關如何使用它的信息。任何建議或指針的VB教程將不勝感激。

更新 - 下面的代碼WORKS

隨着許多感謝了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 
+0

儘管如此,在實體類型的UpdateItem中創建單個參數通常更容易。例如: 'Public Sub ListView1_UpdateItem(sector As Sector)' 假設Sector是您的項目類型,並且您將List控件上的ItemType設置爲Sector。 –

+0

順便說一句:你不應該使用MsgBox。雖然它看起來可行,但它實際上是一個服務器端消息框,如果客戶機與服務器不是同一臺機器,它將不起作用。改用JavaScript的警報或其他方法。 –

回答

0

可以ListView控件的的ItemType屬性設置爲你的實體類型。這使得控件具有強類型。然後,您可以將UpdateItem方法中的Sectorid屬性從int更改爲您的類型(扇區,用戶,您有什麼)。然後,ASP.NET將使用來自表單的數據填充您的實例。

作爲一種替代方法,您也可以調用TryUpdateModel並傳入您的模型類的新實例,然後將其填充。

更多信息可以在Model Binding tutorial找到。

+0

非常感謝!本教程正是我所期待的。儘管我環顧四周,但我從未發現這一點。在我回到我的網站之前,我將通過本教程。 –