如何通过Code Behind从LinqDataSource获取记录值

时间:2010-03-29 03:13:45

标签: linq code-behind linqdatasource

我有一个LinqDataSource,可以检索单个记录。

Protected Sub LinqDataSource1_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.LinqDataSourceSelectEventArgs) Handles LinqDataSource1.Selecting
    Dim BizForSaleDC As New DAL.BizForSaleDataContext
    e.Result = BizForSaleDC.bt_BizForSale_GetByID(e.WhereParameters("ID")).FirstOrDefault
End Sub

我希望能够使用Page_Load函数检索所述DataSource的值。

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    'Get the right usercontrol'
    Dim ctrl As UserControl
    Select Case DataBinder.Eval(LINQDATASOURCE_SOMETHING.DataItem, "AdType")
        Case 1 : ctrl = DirectCast(Me.FindControl("Default1"), UserControl)
        Case 2 : ctrl = DirectCast(Me.FindControl("Default1"), UserControl)
        Case 3 : ctrl = DirectCast(Me.FindControl("ValuPro1"), UserControl)
        Case 4 : ctrl = DirectCast(Me.FindControl("ValuPro1"), UserControl)
        Case Else : ctrl = Nothing
    End Select

    'set the control to visible'
    ctrl.Visible = True
End Sub

但显然上面的代码不起作用......我只是想知道是否有办法让它发挥作用。

这是完整标记

<body>
    <form id="form1" runat="server">
    <asp:LinqDataSource ID="LinqDataSource1" runat="server">
        <WhereParameters>
            <asp:QueryStringParameter ConvertEmptyStringToNull="true" Name="ID" QueryStringField="ID"
                Type="Int32" />
        </WhereParameters>
    </asp:LinqDataSource>
    <uc:Default ID="Default1" runat="server" Visible="false" />
    <uc:ValuPro ID="ValuPro1" runat="server" Visible="false" />
    </form>
</body>
</html>

基本上会发生什么是启用了相应的usercontrol,usercontrol继承了LinqDataSource并显示了相应的信息。

编辑:现在正在使用下面的代码,因为我真的没有多次访问数据库以获取相同的信息,我更愿意从DataSource获取值

'Query the database'
Dim _ID As Integer = Convert.ToInt32(Request.QueryString("ID"))
Dim BizForSaleDC As New BizForSaleDataContext
Dim results = BizForSaleDC.bt_BizForSale_GetByID(_ID).FirstOrDefault

'Get the right usercontrol'
Dim ctrl As UserControl
Select Case results.AdType
    Case 1 : ctrl = DirectCast(Me.FindControl("Default1"), UserControl)
    Case 2 : ctrl = DirectCast(Me.FindControl("Default1"), UserControl)
    Case 3 : ctrl = DirectCast(Me.FindControl("ValuPro1"), UserControl)
    Case 4 : ctrl = DirectCast(Me.FindControl("ValuPro1"), UserControl)
    Case Else : ctrl = Nothing
End Select

编辑2:这似乎是一种解决方法,但我想知道是否有更清洁的方式

Private AdType As String
Private isSold As Boolean

Protected Sub LinqDataSource1_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.LinqDataSourceSelectEventArgs) Handles LinqDataSource1.Selecting
    Dim BizForSaleDC As New DAL.BizForSaleDataContext
    e.Result = BizForSaleDC.bt_BizForSale_GetByID(e.WhereParameters("ID")).FirstOrDefault

    AdType = e.Result.AdType
    isSold = e.Result.isSold
End Sub


Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load

    'Get the right usercontrol'
    Dim ctrl As UserControl
    Select Case AdType
        Case 1 : ctrl = DirectCast(Me.FindControl("Default1"), UserControl)
        Case 2 : ctrl = DirectCast(Me.FindControl("Default1"), UserControl)
        Case 3 : ctrl = DirectCast(Me.FindControl("ValuPro1"), UserControl)
        Case 4 : ctrl = DirectCast(Me.FindControl("ValuPro1"), UserControl)
        Case Else : ctrl = Nothing
    End Select

    ctrl.Visible = True
    'Display SOLD if item is sold'
    ctrl.FindControl("SoldDiv").Visible = isSold

End Sub

1 个答案:

答案 0 :(得分:0)

嗯,我不确定那里是否有更好的答案,但由于没有人回答,我认为这是一种解决方法,它似乎有效,但它并不像我想象的那样漂亮。

Private AdType As String ''# a property to store the Ad Type'
Private isSold As Boolean ''# a property to store whether or not the Ad is sold'

''# when the linq datasource is activated, it pulls the data out of the db'
''# and sets the two properties'
Protected Sub LinqDataSource1_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.LinqDataSourceSelectEventArgs) Handles LinqDataSource1.Selecting
    Dim BizForSaleDC As New DAL.BizForSaleDataContext
    e.Result = BizForSaleDC.bt_BizForSale_GetByID(e.WhereParameters("ID")).FirstOrDefault

    AdType = e.Result.AdType
    isSold = e.Result.isSold
End Sub

''# when the page loads, the properties are already set'
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load

    ''# Get the right usercontrol'
    Dim ctrl As UserControl
    Select Case AdType ''# using the AdType property from above to decide which UserControl to enable'
        Case 1 : ctrl = DirectCast(Me.FindControl("Default1"), UserControl)
        Case 2 : ctrl = DirectCast(Me.FindControl("Default1"), UserControl)
        Case 3 : ctrl = DirectCast(Me.FindControl("ValuPro1"), UserControl)
        Case 4 : ctrl = DirectCast(Me.FindControl("ValuPro1"), UserControl)
        Case Else : ctrl = Nothing
    End Select

    ctrl.Visible = True
    ''# Display SOLD if item is sold'
    ctrl.FindControl("SoldDiv").Visible = isSold ''# using the isSold property from above to display the "SOLD" overlay'

End Sub