开发者

How to get record value from LinqDataSource via Code Behind

I've got a LinqDataSource that retrieves a single record.

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

I'd like to be able to retrieve the values of said DataSource using the Page_Load function.

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

But obviously the code above doesn't work... I'm just wondering if there's a way to make it work.

Here is the full markup

<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 I开发者_开发百科D="ValuPro1" runat="server" Visible="false" />
    </form>
</body>
</html>

Basically what happens is the appropriate usercontrol is enabled and that usercontrol inherits the LinqDataSource and displays the appropriate information.

EDIT: It's working now with the code below, however, since I'm really not into hitting the database multiple times for the same info, I'd prefer to get the value from the 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

EDIT 2: This appears to be a work around, but I'd like to know if there's a cleaner way

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


Well I'm not sure if there's a better answer out there, but since no one answered, I figured this as a work around and it seems to work, though it's not as pretty as I thought it should be.

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜